Gradle Goodness: Define System Properties in gradle.properties File
To define system properties for our Gradle build we can use the command line option --system-prop
or -D
. But we can also add the values for system properties in the gradle.properties
file of our project. This file contains project properties we want to externalized, but if we prefix the property name with systemProp.
the property is turned into a system property. For a multi-module build only system properties defined in the gradle.properties
file in the root of the project structure are used, others are ignored.
In the following build script we have the task showSystemProperty
. Inside the task we assert the value of the system property sample
and the project property sample
:
// Simple task to show
// some properties.
task showSystemProperty << {
// System property 'sample' is set
// in gradle.properties file.
assert System.properties.sample == 'Gradle is gr8'
// Regular project property set
// in gradle.properties file.
assert project.sample == 'Gradle is great'
}
We can run the following command line command to make sure the assertions are true: $ gradle --system-prop "sample=Gradle is gr8" --project-prop "sample=Gradle is great" showSystemProperty
.
Or we could create the following gradle.properties
file in our project directory:
systemProp.sample = Gradle is gr8
sample = Gradle is great
Now we can run $ gradle showSystemProperty
and the assertions are true.
Written with Gradle 2.3.