从命令行覆盖build.gradle中的属性 [英] Override property in build.gradle from the command line

查看:254
本文介绍了从命令行覆盖build.gradle中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

build.gradle 中,我们可以定义如下变量:

  def libVersion ='someVersion'

我们可以用覆盖命令行中的属性 - PlibVersion = otherVersion



不幸的是,这个命令行选项并不影响在 build.gradle 。有没有办法从命令行覆盖这些?请注意,由于某些原因,我不想创建 settings.gradle gradle.properties 文件。

解决方案

下面是一个例子:

  ext.greeting = project.hasProperty('greeting')? project.getProperty('greeting'):'hello'

任务问候<< {
println greeting
}

如果您运行 gradle greet ,它会打印 hello 。如果您运行 gradle -Pgreeting = welcome greet ,它将打印 welcome code>。


In build.gradle we can define variables like:

def libVersion='someVersion'

We can override properties in command line with -PlibVersion=otherVersion

Unfortunately it seems this command line option does not effect local variables defined in build.gradle. Is there a way to override these from command line? Please note that for some reasons i do not want to create settings.gradle nor gradle.properties files.

解决方案

Here's an example:

ext.greeting = project.hasProperty('greeting') ? project.getProperty('greeting') : 'hello'

task greet << {
    println greeting
}

If you run gradle greet, it will print hello.

If you run gradle -Pgreeting=welcome greet, it will print welcome.

这篇关于从命令行覆盖build.gradle中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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