如何从 initscript 应用 Gradle 版本插件?

2024-06-19

我正在尝试进行设置,以便我可以使用Gradle 版本插件 https://github.com/ben-manes/gradle-versions-plugin无需将其添加到我的所有build.gradle files.

基于这个相关问题的答案 https://stackoverflow.com/a/32332914/90848,我尝试创建一个文件~/.gradle/init.d/50-ben-manes-versions.gradle:

initscript {
    repositories {
       jcenter()
    }

    dependencies {
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
    }
}

allprojects {
    apply plugin: com.github.ben-manes.versions
}

如果我然后尝试调用./gradlew dependencyUpdates在我的仓库中,我得到:

FAILURE: Build failed with an exception.

* Where:
Initialization script '~/.gradle/init.d/50-ben-manes-versions.gradle' line: 13

* What went wrong:
Could not get unknown property 'com' for root project 'project-name' of type org.gradle.api.Project.

该答案表示不要在插件名称周围使用引号,但由于这不起作用,我尝试添加引号(即:apply plugin: 'com.github.ben-manes.versions')。这样我得到:

FAILURE: Build failed with an exception.

* Where:
Initialization script '~/.gradle/init.d/50-ben-manes-versions.gradle' line: 13

* What went wrong:
Plugin with id 'com.github.ben-manes.versions' not found.

有没有办法从 initscript 应用 Gradle 版本插件?

顺便说一句,我正在使用 Gradle 4.3.1。


插件可以通过几种不同的方式应用。在问题引用的答案中,它们正在按类型申请 https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/PluginManager.html#apply-java.lang.Class-。你也可以通过插件Id申请,这是一个String https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/PluginManager.html#apply-java.lang.String-.

在第二次尝试中,当您通过 Id 申请时,您正在做正确的事情,但构建错误如下:

未找到 ID 为“com.github.ben-manes.versions”的插件。

这里的问题是你目前无法通过 init 脚本中的 ID 应用插件 (#gradle/1322) https://github.com/gradle/gradle/issues/1322.

解决方案是按类型应用插件。

幸运的是,该插件是开源的,因此发现插件的类型相对简单。插件 ID 是com.github.ben-manes.versions,这将我们引向META-INF/gradle-plugins/com.github.ben-manes.versions.properties file https://github.com/ben-manes/gradle-versions-plugin/blob/e39a84307cea47892149f576461c80fdb3b95d2f/src/main/resources/META-INF/gradle-plugins/com.github.ben-manes.versions.properties。该文件包含行implementation-class=com.github.benmanes.gradle.versions.VersionsPlugin,它告诉我们插件的类型是com.github.benmanes.gradle.versions.VersionsPlugin https://github.com/ben-manes/gradle-versions-plugin/blob/e39a84307cea47892149f576461c80fdb3b95d2f/src/main/groovy/com/github/benmanes/gradle/versions/VersionsPlugin.groovy#L10。这也可以通过将插件应用到构建(而不是通过初始化脚本)并检查plugins or pluginManager从项目中列出插件类型。

要修复此问题,请更改此行:

apply plugin: 'com.github.ben-manes.versions'

to:

apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin

所以完整的、工作的初始化脚本是:

initscript {                                                                    
    repositories {                                                              
       jcenter()                                                                
    }                                                                           

    dependencies {                                                              
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'          
    }                                                                           
}                                                                               


allprojects {                                                                   
    apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin            
}                                                                               
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 initscript 应用 Gradle 版本插件? 的相关文章

随机推荐