Plugins Gradle插件项目版本号
我有一个gradle插件,它使用Plugins Gradle插件项目版本号,plugins,gradle,Plugins,Gradle,我有一个gradle插件,它使用project.version变量 然而,当我在build.gradle文件中更改版本时,插件中的版本不会更新 举例说明: 插件 格雷德尔先生 结果 构建脚本和插件都会犯同样的错误。它们将打印版本作为配置任务的一部分,而不是为任务提供行为(任务操作)。如果在构建脚本中设置版本之前应用插件(通常情况下),它将打印version属性的先前值(可能在gradle.properties中设置了一个) 正确的任务声明: task printVersion { //
project.version
变量
然而,当我在build.gradle
文件中更改版本时,插件中的版本不会更新
举例说明:
插件
格雷德尔先生
结果
构建脚本和插件都会犯同样的错误。它们将打印版本作为配置任务的一部分,而不是为任务提供行为(任务操作)。如果在构建脚本中设置版本之前应用插件(通常情况下),它将打印
version
属性的先前值(可能在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属性提取项目版本,而无需向build.gradle文件添加专用任务 例如:
gradle properties -q | grep "version:" | awk '{print $2}'
酷!我会在星期一第一件事上试试看!:)更好的
/gradlew属性--没有守护进程--console=plain-q | grep“^version:| awk'{printf$2}'
,以防有多个版本。此外,您不需要运行守护进程。我更喜欢此解决方案,因为它不需要更改构建文件(我不一定拥有该文件)。对于此问题,这是一个错误的解决方案:在Gradle中,版本是一个对象,而不是字符串。包含版本的结果字符串只是分配给“project.version”的version对象上的toString()
的结果。使用grep/awk等工具解析build.gradle文件永远是错误的解决方案。这就像使用grep解析XML一样。请参阅,对于使用此解决方案的人,不要忘记在grep的开头添加插入符号。您可能有以“version:”(grep“^version:”)结尾的其他属性。@Vinz486-如果您所做的只是手动验证版本以确保它看起来正确,那么一个快速的黑客解决方案仍然可以得到正确的答案,而且可以说比正确使用API但需要对源代码进行更改/配置的防弹解决方案更理想。所以我认为,根据上下文,它不一定是“错误的”;与使用grep/awk等解析build.gradle
或使用grep解析XML的示例相同。
> gradle printVersion
1.0.1
> gradle printVersionFromPlugin
1.0.0
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 properties -q | grep "version:" | awk '{print $2}'