Gradle插件项目版本号

时间:2014-07-24 14:40:24

标签: plugins gradle

我有一个使用project.version变量的gradle插件。

但是,当我更改build.gradle文件中的版本时,插件中的版本不会更新。

举例说明:

插件

// 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

2 个答案:

答案 0 :(得分:15)

您可以使用gradle属性提取项目版本,而无需向build.gradle文件添加专用任务。

例如:

gradle properties -q | grep "version:" | awk '{print $2}'

答案 1 :(得分:9)

构建脚本和插件都犯了同样的错误。他们将版本打印为配置任务的一部分,而不是为任务提供行为(任务操作)。如果在构建脚本中设置版本之前应用插件(通常是这种情况),它将打印version属性的先前值(可能在gradle.properties中设置了一个)。 / p>

正确的任务声明:

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
    }
}

插件的任务相同。

相关问题