We can add properties to our Gradle build script in several different ways.
- First of all we can define the properties in the script itself.
- Or we can use the
-P
command-line argument to pass a property to the build script. - We can define a
gradle.properties
file and set the property in this file. We can place the file in our project directory or in the<USER_HOME>/.gradle
directory. The properties defined in the property file in our home directory take precedence over the properties defined in the file in our project directory. As a bonus we can also define system properties in agradle.properties
file, we only have to prefix the property name withsystemProp.
. - We can use an environment variable of which the name starts with
ORG_GRADLE_PROJECT_
followed by the property name. - Or we use the Java system property that starts with
org.gradle.project.
followed by the property name.
The following sample Gradle build file uses all these techniques to get the value of properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | description = '' '\ To run this build script we must first set an environment variable ORG_GRADLE_PROJECT_property5= "environment property" Run as : gradle -Property4= "argument property" -Dorg.gradle.project.property6= "system property" '' ' property1 = 'Project property' task assertProps << { description: 'Print different properties' assert 'Project property' == property1 assert 'gradle.properties property' == property2 assert 'argument property' == property4 assert 'environment property' == property5 assert 'system property' == property6 assert 'gradle.properties system property' == System.properties[ 'property3' ] } defaultTasks 'assertProps' |
We use the following gradle.properties
file:
1 | property2 = gradle.properties property systemProp.property3 = gradle.properties system property |
Written with Gradle 0.9.