简体   繁体   English

如何通过在 gradle 命令行上传递 version 属性来设置 project.version?

[英]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.

相关问题 Maven $ {project.version}无效 - Maven ${project.version} not working Maven:如何在多模块项目中包含 ${project.version} - Maven: How to include ${project.version} in multi-module project 忽略 Maven 版本中的 ${project.version}:准备 - Ignore ${project.version} in Maven release:prepare Jib 插件无法访问由另一个 Gradle 插件更新的 project.version - Jib plugin not able to access project.version as updated by another Gradle plugin 来自父pom的maven依赖管理会覆盖$ {project.version} - maven dependency management from parent pom overrides ${project.version} 如何在没有显式JDK路径的情况下在Gradle项目中设置JDK版本? - How to set JDK version in Gradle project WITHOUT explicit JDK path? maven-checkstyle-plugin中的变量$ {project.version}来自子项目 - Variable ${project.version} in maven-checkstyle-plugin comes from child projekt 如何从gradle项目中的命令行覆盖集成测试中的属性? - How can I override a property in an integration test from the command line in a gradle project? 如何在 Intellij IDEA 社区版本中为 gradle spring boot 项目设置 spring.config.location? - how to set spring.config.location for a gradle spring boot project in Intellij IDEA community version? 如何在命令行上使用不同版本的 Java - How to use a different version of Java on the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM