Using project properties in Gradle - bahkified/Notes GitHub Wiki

By default, Gradle will define a Project object for the current project. This object can contain project properties to be set and used by the build script.

Setting Project Properties

Project properties can be set in two ways, either directly in the .gradle file, or on the command line when the Gradle script is executed.

Defined in Gradle Script

The Gradle script will have access to a Project object, called project. With this object, properties can be set and accessed.

project.setProperty('propName', 'propValue')
println project.getProperty('propName)

Defined on command line

Even if default values for these properties are defined in the script itself, they can be overridden with values specified on the command line when executing the script. The -P flag is used for this, instead of the -D flag, in order to distinguish project properties from system properties.

./gradlew clean build test -Penv=prod

This will run the clean task, the build task, and the test task with the project property env = prod. This property can be accessed in the Gradle script in the same way as above, using project.getProperty('propName').

Resources