【问题标题】:How can I apply the Gradle Versions Plugin from an initscript?如何从 initscript 应用 Gradle 版本插件?
【发布时间】:2018-06-28 04:01:20
【问题描述】:

我正在尝试进行设置,以便我可以使用 Gradle Versions Plugin 而无需将其添加到我的所有 build.gradle 文件中。

基于this answer to a related question,我尝试创建一个文件~/.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。

【问题讨论】:

  • 第一件事是com.github.ben-manes.versions 没有被引号包围。当您应用这样的插件时,您是通过 id 应用它。
  • @mkobit 正如问题中所述,我尝试了带引号和不带引号。
  • 对不起,完全是我的错,我躺在床上错过了。我明天看看这个。
  • @mkobit 没问题!我认为无引号版本看起来也很重要,但其他答案似乎暗示缺少引号很重要。无论如何,如果你明天能再看一眼,我将不胜感激。谢谢!

标签: gradle


【解决方案1】:

可以通过几种不同的方式应用插件。在问题的参考答案中,它们是applied by type。你也可以apply by plugin Id, which is a String

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

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

这里的问题是你currently cannot apply plugins by Id from init scripts (#gradle/1322)

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

幸运的是,该插件是开源的,因此发现插件的类型相对简单。插件 ID 是com.github.ben-manes.versions,这将我们引向META-INF/gradle-plugins/com.github.ben-manes.versions.properties file。这个文件包含implementation-class=com.github.benmanes.gradle.versions.VersionsPlugin这一行,它告诉我们插件的类型是 com.github.benmanes.gradle.versions.VersionsPlugin。这也可以通过将插件应用于构建(而不是通过初始化脚本)并检查项目中的 pluginspluginManager 以列出插件类型来确定。

要进行修复更改此行:

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

到:

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            
}                                                                               

【讨论】:

  • 感谢您的详细回答,实际上解释了这里发生了什么!
  • 这样做会导致Could not get unknown property 'com' for root project 'edited' of type org.gradle.api.Project.
  • @Brice 是来自初始化脚本吗?使用新问题和完整示例可能会更容易。
  • @mkobit 我真的不明白出了什么问题。但是我的项目 sricpt 是应用具有其他构建脚本部分的外部脚本。因此,将 apply plugin 移到这些脚本中的其他位置是可行的。
【解决方案2】:

我现在使用@mkobit 的解决方案有一段时间了。它工作得很好,但是当您正在处理的项目已经应用了版本插件时,它不支持这种情况。

我现在正在使用稍微修改过的脚本版本,如果插件已经存在,则不应用它:

initscript {
  repositories {
    jcenter()
  }

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

allprojects {
  afterEvaluate { project ->
    if (project.parent == null) {
      def hasPlugin = project.plugins.any { 
        it.class.name == "com.github.benmanes.gradle.versions.VersionsPlugin"
      }

      if (!hasPlugin) {
        project.apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2017-11-06
    • 2022-06-20
    • 2020-06-11
    • 2018-11-03
    相关资源
    最近更新 更多