如何使用 init.gradle 为 settings.gradle 中配置的插件提供插件存储库

How to use init.gradle to provide plugin repositories for plugins configured in the settings.gradle

我在settings.gradle文件中配置了一个Gradle插件,如下,

buildscript {
  dependencies {
    classpath "org.test.group:gradleplugins:${pluginVersion}"
    ...
  }
}

apply plugin: 'org.test.group:gradleplugins'
....

我正在尝试使用 init.gradle 提供工件存储库,如下所示,

initscript {
    repositories {
        maven { url "https://test.repo/gradleplugins" }
        ...
    }
}

此外,我已经使用 init.gradle 文件为构建任务提供了

.... -i -I ./init.gradle'

但是构建还是出现如​​下依赖解析错误,

无法解析外部依赖性 org.test.group:gradleplugins:1.0.0-SNAPSHOT 因为没有定义存储库。

init.gradle 有另一个用途(自动检测该文件)。

void settingsEvaluated​(Settings settings) might be to only chance to manipulate these settings (but the question does not provide the least valid reason to do so, with only one repository). It rather seems that you're unnecessarily over-complicating things, where there otherwise would be no problem. And this doesn't belong into the settings.gradle either (which also has another purpose). Just add the plugin repositories into the buildscript block of the root project's build.gradle, where they belong. The userguide 显示了在静态定义插件 repositories 时看起来应该如何相似。

可以通过在 init.gradle 文件中编写 Gradle 插件来完成,如下所示,

apply plugin: EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    void apply(Gradle gradle) {
        gradle.settingsEvaluated { settings ->
            settings.pluginManagement {
                repositories {
                    maven { url "https://repo.org/gradleplugins" }
                    maven { url "https://repo.org/maven" }
                }    
            }
        }
    }
}

根据 Gradle 文档, https://docs.gradle.org/current/userguide/init_scripts.html