首页 > 解决方案 > 在构建脚本中加载其他插件之前使用 gradle 插件

问题描述

我有自己的 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)
}

标签: gradlegradle-plugin

解决方案


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

  • 通过Plugin<Settings>利用对象是_settings.gradle(.kts)SettingsExtensionAware
  • 使用在同一设置文件中定义您的插件类路径pluginManagement
  • 在您的项目中应用插件,而不指定版本 - 请参阅此示例以获取不通过以下方式定义版本的更简单版本pluginManagement

示例:带有插件的https://github.com/ljacomet/setttings-plugin-propsbuildSrc但它可以被发布并用作二进制插件而没有任何问题。


推荐阅读