在构建脚本中加载其他插件之前使用 gradle 插件

Using a gradle plugin before loading other plugins in the buildscript

我有自己的 gradle 插件,其中包含一个包含其他插件版本的文件。目前,每当我创建一个新项目时,我都必须将它们复制过来,因为我无法使用插件中的版本。

有没有加载我的插件,应用它,然后再加载其他插件的方法?目前,当我将我的插件模型命名为 buildSrc 时,我只能自己为项目执行此操作,因为它会自动将其添加到其他模块。

我想要实现的示例:

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)
}

以及当我将插件作为我的主项目中的模块时的样子:

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)
}

你应该能够通过组合不同的东西来实现你想要的:

  • 通过利用 Settings objectExtensionAware
  • 这一事实,在 Plugin<Settings> 中定义您在 settings.gradle(.kts) 中应用的版本
  • 在同一设置文件中定义您的插件类路径 using pluginManagement
  • 在您的项目中应用插件,但不指定版本 - 请参阅 this example 了解不通过 pluginManagement
  • 定义版本的更简单版本

示例:https://github.com/ljacomet/setttings-plugin-props 使用 buildSrc 中的插件,但它可以毫无问题地作为二进制插件发布和使用。