Gradle插件项目版本号 [英] Gradle plugin project version number
问题描述
我有一个使用project.version
变量的gradle插件.
I have a gradle plugin that uses the project.version
variable.
但是,当我更改build.gradle
文件中的版本时,插件中的版本不会更新.
How ever, the version in the plugin does not update when I change the version in the build.gradle
file.
// my-plugin
void apply(Project project) {
project.tasks.create(name: 'printVersionFromPlugin') {
println project.version
}
}
build.gradle
version '1.0.1' // used to be 1.0.0
task printVersion {
println project.version
}
apply plugin: 'my-plugin'
结果
> gradle printVersion
1.0.1
> gradle printVersionFromPlugin
1.0.0
推荐答案
构建脚本和插件都犯了相同的错误.他们在配置任务时打印版本,而不是赋予任务行为(任务操作).如果在构建脚本中设置版本之前应用了插件(通常是这种情况),它将打印version
属性的先前值(可能在gradle.properties
中设置了该值).
Both the build script and the plugin make the same mistake. They print the version as part of configuring the task, rather than giving the task a behavior (task action). If the plugin is applied before the version is set in the build script (which is normally the case), it will print the previous value of the version
property (perhaps one is set in gradle.properties
).
正确的任务声明:
task printVersion {
// any code that goes here is part of configuring the task
// this code will always get run, even if the task is not executed
doLast { // add a task action
// any code that goes here is part of executing the task
// this code will only get run if and when the task gets executed
println project.version
}
}
与插件的任务相同.
这篇关于Gradle插件项目版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!