如何通过在 gradle 命令行上传递 version 属性来设置 project.version? [英] How to set project.version by passing version property on gradle command line?

查看:17
本文介绍了如何通过在 gradle 命令行上传递 version 属性来设置 project.version?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用命令行传递的自定义版本构建JAR,例如:

I want to build JAR with self-defined version passed via command line, such as:

当我像这样执行gradle构建任务时:

When I execute gradle build task like this:

gradle build -Pversion=1.0

应生成myproject-1.0.jar.

myproject-1.0.jar should be generated.

我已尝试将以下行添加到 build.gradle,但没有奏效:

I have tried adding the line below to the build.gradle, but it did not work:

version = project.hasProperty('version') ? project['version'] : '10.0.0'

推荐答案

您无法从命令行覆盖现有的项目属性,请查看 此处.因此,尝试将版本变量重命名为与版本不同的名称,并在命令之前使用 -P 标志设置它,例如:

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:

gradle -PprojVersion=10.2.10 build 

然后在你的 build.gradle

And then in your build.gradle

if (project.hasProperty('projVersion')) {
  project.version = project.projVersion
} else {
  project.version = '10.0.0'
}

或者像你对 ?: 操作符所做的那样

Or as you did with ?: operator

这篇关于如何通过在 gradle 命令行上传递 version 属性来设置 project.version?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆