[英]How to set project.version by passing version property on gradle command line?
I want to build JAR with self-defined version passed via command line, such as:我想使用通过命令行传递的自定义版本构建 JAR,例如:
When I execute gradle build task like this:当我像这样执行gradle构建任务时:
gradle build -Pversion=1.0
myproject-1.0.jar should be generated.应生成 myproject-1.0.jar。
I have tried adding the line below to the build.gradle , but it did not work:我尝试将下面的行添加到build.gradle ,但没有用:
version = project.hasProperty('version') ? project['version'] : '10.0.0'
Set the property only in the gradle.properties
file (ie remove it from build.gradle
).仅在
gradle.properties
文件中设置属性(即从build.gradle
删除它)。 Also make sure the options come before the command (as mentioned above).还要确保选项出现在命令之前(如上所述)。
gradle.properties contents: gradle.properties 内容:
version=1.0.12
Version can then be overridden on the command line with:然后可以在命令行上使用以下命令覆盖版本:
gradle -Pversion=1.0.13 publish
You are not able to override existing project properties from command line, take a look here .您无法从命令行覆盖现有项目属性,请查看此处。 So try to rename a version variable to something differing from version and set it with
-P
flag before command, like:因此,尝试将版本变量重命名为与版本不同的名称,并在命令之前使用
-P
标志设置它,例如:
gradle -PprojVersion=10.2.10 build
And then in your build.gradle然后在你的 build.gradle
if (project.hasProperty('projVersion')) {
project.version = project.projVersion
} else {
project.version = '10.0.0'
}
Or as you did with ?: operator或者像你对 ?: 操作符所做的那样
如果您将version
条目移动到gradle.properties
文件,您还可以:
gradle clean build -Dorg.gradle.project.version=1.1
If you need a default version other than 'unspecified':如果您需要“未指定”以外的默认版本:
version = "${version != 'unspecified' ? version : 'your-default-version'}"
Pass version via command line:通过命令行传递版本:
gradle build -P version=1.0
You can pass the project version on cli with -Pversion=...
as long as you don't set it in build.gradle .您可以使用
-Pversion=...
在 cli 上传递项目版本,只要您不在build.gradle 中设置它。 If you need a custom default value for when no version is passed on the cli, use gradle.properties file like so: version=...
如果在 cli 上没有传递版本时需要自定义默认值,请使用gradle.properties文件,如下所示:
version=...
TL;DR: Don't set the version in build.gradle file if you want to change it later on via cli. TL;DR:如果您想稍后通过 cli 更改它,请不要在build.gradle文件中设置版本。
version = (findProperty('version') == 'unspecified') ? '0.1' : version
I've found this to be the easiest and cleanest way, without requiring a gradle.properties
file or changing the version
variable name.我发现这是最简单、最干净的方法,不需要
gradle.properties
文件或更改version
变量名称。
In build.gradle
:在
build.gradle
:
// Note - there is intentionally no equals sign here
version project.hasProperty('version') ? version : '1.0.0'
From the command line:从命令行:
./gradlew -Pversion=1.5.2 build
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.