We cannot make a software available on different operating systems, or different environments without configuring it dynamically. One approach to configure software is by using the properties file or environment properties. The following are the different ways Gradle provides to configure properties to build.gradle
:
ext
closuregradle.properties
We saw many examples in Chapter 3, Managing Task, of adding custom properties to a project using the ext
closure. Thus, we will not discuss the topic in this chapter.
Gradle provides a default mechanism of reading the properties file using gradle.properties
. You can add the gradle.properties
file in any of the following locations:
<USER_HOME>/.gradle
: gradle.properties
defined under this directory would be accessible to all the projects. You can use this file to define global properties and you can access these properties using $project.<propertyname>
. If you have defined GRADLE_USER_HOME
to some other directory, then Gradle will skip the <USER_HOME>/.gradle
directory and will read gradle.properties
from the GRADLE_USER_HOME
directory. By default <USER_HOME>/.gradle
would be considered to read the gradle.properties
file. If properties are defined in <USER_HOME>/.gradle/gradle.properties
, but are not set by the user, it leads to an exception. If this is not desired, such properties should be checked using the hasProperty
method of project
, and if not set, it should be initialized with a default value. This property file may also be used for storing passwords.<ProjectDir>
: gradle.propertie
s defined under this directory would be accessible to the current project. You cannot access these properties from any other project. So, all the project-specific properties can be defined in the project's gradle.properties
file.Along with project-level properties, you can also define system-level properties in the gradle.properties
file. To define system-level properties, you can append properties with systemProp
. So systemProp.sProp1=sVal1
will set sProp1
as a system-level property with the value sVal1
.
We will see an example in the next section.
You can define runtime properties on the command line also using the -P
and -D
options. Using -P
, you can define project-specific properties. Using -D
, you can define system-level properties. To access system-level properties, you can use System.properties['<propertyname>'].
Note that, command line properties override gradle.properties
. When you configure properties in multiple places, the following order applies and the last one gets the highest priority:
gradle.properties
in project build
dir.gradle.properties
in Gradle user home.You might want to use the custom filename for your properties file, for example, login.properties
or profile.properties
. To use the custom properties, simply read the file using FileInputStream
and convert it to the properties object:
task showCustomProp << { Properties props = new Properties() props.load(new FileInputStream("login.properties")) println props println props.get('loginKey1') }
The preceding code will read the login.properties
file, and the first println
statement will print all the properties while the second println
statement will display the value of the loginKey1
property.
Let's take a look at a comprehensive example. We will create one gradle.properties
file in the <USER_HOME>/.gradle
directory and another gradle.properties
file in the project directory:
<USER_HOME>/.gradle/gradle.properties
globalProp1=globalVal1 globalProp2=globalVal2
Chapter6/PropertyExample/Proj1/gradle.properties
Proj1Prop1=Proj1Val1 Proj1Prop2=Proj1Val2 systemProp.sysProp1=sysVal1
Here is our build script, Chapter6/PropertyExample/Proj1/build.gradle
:
task showProps << { println "local property "+Proj1Prop1 println "local property "+Proj1Prop2 println "local property via command line: "+projCommandProp1 println "global property "+globalProp1 println "global property "+globalProp2 println "System property "+System.properties['sysProp1'] println "System property via command line: "+System.properties['sysCommandProp1'] }
Now, execute the following command:
$gradle -PprojCommandProp1=projCommandVal1-DsysCommandProp1=sysCommandVal1 showProps :showProps local property Proj1Val1 local property Proj1Val2 local property via command line: projCommandVal1 global property globalVal1 global property globalVal2 System property sysVal1 System property via command line: sysCommandVal1 BUILD SUCCESSFUL
Here, you can see that the first two lines contain the properties defined in the project's gradle.properties
file. The third line shows the property, which the user initialize with the -P
option. The fourth and fifth lines show the properties defined in <USER_HOME>/.gradle/gradle.properties
. The sixth line shows the system properties defined in the project's gradle.properties
file, and finally, the example shows the system property passed in the command line using the -D
option.
18.227.111.208