Allan W
Allan W

Reputation: 2951

Using a gradle plugin before loading other plugins in the buildscript

I have my own gradle plugin that contains a file with versions for other plugins. Currently, whenever I make a new project, I have to copy them over as I can't use the versions from the plugin.

Is there anyway to load my plugin, apply it, and then load other plugins afterwards? Currently, I can only do this for the project myself when I make my plugin model named buildSrc, as it automatically adds it to other modules.

Example of what I want to achieve:

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        maven { url "https://plugins.gradle.org/m2/" }
    }

    dependencies {
        classpath "ca.allanwang:kau:$kau_version"
    }

    // Apply plugin before other dependencies so I can use it
    apply plugin: "ca.allanwang.kau"

    dependencies {
        classpath kauPlugin.android
        classpath kauPlugin.kotlin
        classpath kauPlugin.androidMaven
        classpath kauPlugin.playPublisher
        classpath kauPlugin.dexCount
        classpath kauPlugin.gitVersion
        classpath kauPlugin.spotless
    }

    wrapper.setDistributionType(Wrapper.DistributionType.ALL)
}

and how it looks like when I have the plugin as a module in my main project:

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        maven { url "https://plugins.gradle.org/m2/" }
    }

    apply plugin: "ca.allanwang.kau"

    dependencies {
        classpath kauPlugin.android
        classpath kauPlugin.kotlin
        classpath kauPlugin.androidMaven
        classpath kauPlugin.playPublisher
        classpath kauPlugin.dexCount
        classpath kauPlugin.gitVersion
        classpath kauPlugin.spotless
    }

    wrapper.setDistributionType(Wrapper.DistributionType.ALL)
}

Upvotes: 2

Views: 1824

Answers (1)

Louis Jacomet
Louis Jacomet

Reputation: 14500

You should be able to achieve what you want by combining different things:

  • Define your versions in a Plugin<Settings> that you apply in you settings.gradle(.kts) by leveraging the fact that the Settings object is ExtensionAware
  • Define your plugin classpath in that same settings file using pluginManagement
  • Apply plugins in your projects, without specifying a version - see this example for a simpler version that does not define versions through pluginManagement

Example: https://github.com/ljacomet/setttings-plugin-props with the plugin in buildSrc but it could be published and used as a binary plugin without any problem.

Upvotes: 3

Related Questions