Gradle Pass Configuration on Command Line: Difference between revisions
No edit summary |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
It is sometimes necessary to pass environment-specific configuration to the build. An example is the URL of a container registry, which may different values relative to the environment the build is executed. The best place for such configuration is not build.gradle, as we would like to avoid editing it for each environment the build is supposed to execute in. One option, which works well for build containers, is to [[Passing Configuration to a Gradle Build via Custom Environment Variables|use custom environment variables]]. Another option is to use command line-specified system properties, as shown below: | It is sometimes necessary to pass environment-specific configuration to the build. An example is the URL of a container registry, which may different values relative to the environment the build is executed. The best place for such configuration is not build.gradle, as we would like to avoid editing it for each environment the build is supposed to execute in. One option, which works well for build containers, is to [[Passing Configuration to a Gradle Build via Custom Environment Variables|use custom environment variables]]. Another option is to use command line-specified system properties, as shown below: | ||
gradle -Dmy.environment.dependent.config=blue | gradle -Dmy.environment.dependent.config=blue ... | ||
If not provided on command line, those configuration elements can always fall back to build.gradle hardcoded values. | If not provided on command line, those configuration elements can always fall back to build.gradle hardcoded values. | ||
Line 14: | Line 14: | ||
=Playground= | =Playground= | ||
{{External|} | {{External|https://github.com/ovidiuf/playground/tree/master/gradle/passing-configuration-on-command-line}} | ||
=Implementation= | =Implementation= | ||
We assign the value of the system property to a [[Gradle_Variables_and_Properties#Extra_Properties|project extra property]], for convenient handling. The same statement is also use to specify a default value, in case the system property is not provided: | |||
<syntaxhighlight lang='groovy'> | |||
ext { | |||
myEnvironmentDependentConfig = System.getProperty("my.environment.dependent.config", "red"); | |||
} | |||
</syntaxhighlight> | |||
The value can be access as such: | |||
<syntaxhighlight lang='groovy'> | |||
task displayPropertyValueAtExecutionTime { | |||
doLast { | |||
println "myEnvironmentDependentConfig: " + myEnvironmentDependentConfig | |||
} | |||
} | |||
</syntaxhighlight> | |||
Setting the value on command line results in: | |||
gradle -Dmy.environment.dependent.config=blue displayPropertyValueAtExecutionTime | |||
... | |||
myEnvironmentDependentConfig: blue | |||
Not setting reverts to default: | |||
gradle displayPropertyValueAtExecutionTime | |||
... | |||
myEnvironmentDependentConfig: red |
Latest revision as of 20:38, 20 March 2019
Internal
Overview
It is sometimes necessary to pass environment-specific configuration to the build. An example is the URL of a container registry, which may different values relative to the environment the build is executed. The best place for such configuration is not build.gradle, as we would like to avoid editing it for each environment the build is supposed to execute in. One option, which works well for build containers, is to use custom environment variables. Another option is to use command line-specified system properties, as shown below:
gradle -Dmy.environment.dependent.config=blue ...
If not provided on command line, those configuration elements can always fall back to build.gradle hardcoded values.
Playground
Implementation
We assign the value of the system property to a project extra property, for convenient handling. The same statement is also use to specify a default value, in case the system property is not provided:
ext {
myEnvironmentDependentConfig = System.getProperty("my.environment.dependent.config", "red");
}
The value can be access as such:
task displayPropertyValueAtExecutionTime {
doLast {
println "myEnvironmentDependentConfig: " + myEnvironmentDependentConfig
}
}
Setting the value on command line results in:
gradle -Dmy.environment.dependent.config=blue displayPropertyValueAtExecutionTime ... myEnvironmentDependentConfig: blue
Not setting reverts to default:
gradle displayPropertyValueAtExecutionTime ... myEnvironmentDependentConfig: red