如何从初始化脚本中应用Gradle版本插件? [英] How can I apply the Gradle Versions Plugin from an initscript?

查看:289
本文介绍了如何从初始化脚本中应用Gradle版本插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to set things up so that I can use the Gradle Versions Plugin without having to add it to all of my build.gradle files.

基于有关相关问题的答案,我尝试创建文件~/.gradle/init.d/50-ben-manes-versions.gradle:

Based on this answer to a related question, I tried creating a file ~/.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,则会得到:

If I then try to invoke ./gradlew dependencyUpdates in my repo, I get:

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').这样我得到:

That answer said to not use quotes around the plugin name, but since that didn't work I tried adding quotes (ie: apply plugin: 'com.github.ben-manes.versions'). With that I get:

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.

有什么方法可以从初始化脚本中应用Gradle版本插件吗?

Is there any way to apply the Gradle Versions Plugin from an initscript?

顺便说一下,我正在使用Gradle 4.3.1.

I am using Gradle 4.3.1, by the way.

推荐答案

插件可以以几种不同的方式应用.在问题的参考答案中,它们是通过插件ID(String )申请.

Plugins can be applied in a few different ways. In the referenced answer from the question, they are being applied by type. You can also apply by plugin Id, which is a String.

在第二次尝试通过ID进行申请时,您做的是正确的事,但是构建错误出在以下地方:

In your second attempt when you are applying by Id you are doing the right thing, but the build errors out with:

未找到ID为'com.github.ben-manes.versions'的插件.

Plugin with id 'com.github.ben-manes.versions' not found.

这里的问题是,您当前无法通过初始化脚本中的ID应用插件(#gradle/1322).

解决方案是改为按类型应用插件.

The solution is to instead, apply the plugin by type.

幸运的是,该插件是开源的,因此发现插件的类型相对简单.插件ID为com.github.ben-manes.versions,可将我们引至 com.github.benmanes.gradle.versions.VersionsPlugin .也可以通过将插件应用于构建(而不是通过init脚本)并检查项目中的pluginspluginManager列出插件类型来确定.

Luckily, the plugin is open source so it relatively simple to discover the type of the plugin. The plugin Id is com.github.ben-manes.versions, which leads us to the META-INF/gradle-plugins/com.github.ben-manes.versions.properties file. This file contains the line implementation-class=com.github.benmanes.gradle.versions.VersionsPlugin, which tells us that the type of the plugin is com.github.benmanes.gradle.versions.VersionsPlugin. This could have also be determined by applying the plugin to the build (instead of through an init script) and inspecting the plugins or pluginManager from the project to list out the plugin types.

要进行更改,请更改此行:

To make the fix change this line:

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

收件人:

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

因此,完整的有效初始化脚本为:

So the full, working initscript is:

initscript {                                                                    
    repositories {                                                              
       jcenter()                                                                
    }                                                                           

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


allprojects {                                                                   
    apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin            
}                                                                               

这篇关于如何从初始化脚本中应用Gradle版本插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆