Dark theme | Light theme

September 21, 2010

Gradle Goodness: Different Ways to Set Project Properties

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 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 a file, we only have to prefix the property name with systemProp..
  • 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 ' property' == property2
 assert 'argument property' == property4
 assert 'environment property' == property5
 assert 'system property' == property6
 assert ' system property' ==['property3']

defaultTasks 'assertProps'

We use the following file:

property2 = property
systemProp.property3 = system property

Written with Gradle 0.9.