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
-Pcommand-line argument to pass a property to the build script. - We can define a
gradle.propertiesfile and set the property in this file. We can place the file in our project directory or in the<USER_HOME>/.gradledirectory. 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.propertiesfile, 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:
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:
property2 = gradle.properties property systemProp.property3 = gradle.properties system property
Written with Gradle 0.9.