Override property in build.gradle from the command

2020-05-21 07:47发布

问题:

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.

回答1:

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.



标签: gradle