Java 如何通过在 gradle 命令行上传递 version 属性来设置 project.version?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32717251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set project.version by passing version property on gradle command line?
提问by pat.inside
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'
采纳答案by Stanislav
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
或者像你对 ?: 操作符所做的那样
回答by Opal
If you move version
entry to gradle.properties
file you can also:
如果您将version
条目移至gradle.properties
文件,您还可以:
gradle clean build -Dorg.gradle.project.version=1.1
回答by zeroblaz3
Set the property only in the gradle.properties
file (i.e. remove it from build.gradle
). Also make sure the options come before the command (as mentioned above).
仅在gradle.properties
文件中设置属性(即从 中删除build.gradle
)。还要确保选项出现在命令之前(如上所述)。
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
回答by Bian Jiaping
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
回答by Ajax
version = (findProperty('version') == 'unspecified') ? '0.1' : version
version = (findProperty('version') == 'unspecified') ? '0.1' : version
回答by Alex
You can pass the project version on cli with -Pversion=...
as long as you don't set it in build.gradle. If you need a custom default value for when no version is passed on the cli, use gradle.propertiesfile like so: version=...
-Pversion=...
只要您不在build.gradle 中设置它,您就可以在 cli 上传递项目版本。如果在 cli 上没有传递版本时需要自定义默认值,请使用gradle.properties文件,如下所示:version=...
TL;DR: Don't set the version in build.gradlefile if you want to change it later on via cli.
TL;DR:如果您想稍后通过 cli 更改它,请不要在build.gradle文件中设置版本。