May 25, 2021 Gradle
3. Gradle properties and system properties
4. Configure the project using an external build script
It is common for multiple tasks to depend on the existence of a directory. O f course, you can solve this problem by adding mkdir at the beginning of these tasks. B ut this is a bloated solution. Here's a better solution (only for situations where these tasks that require this directory have a dependency):
Create a directory using mkdir
build.gradle
classesDir = new File('build/classes')
task resources << {
classesDir.mkdirs()
// do something
}
task compile(dependsOn: 'resources') << {
if (classesDir.isDirectory()) {
println 'The class directory exists. I can operate'
}
// do something
}
The output of the gradle -q compile
> gradle -q compile
The class directory exists. I can operate
Gradle provides many ways to add properties to your build. F rom the JVM started by Gradle, you can use the -D command line option to pass a system property to it. The -D option for the Gradle command has the same effect as the -D option for the java command.
Alternatively, you can add properties to your project object through the property file. Y ou can put a gradle.properties file in Gradle's user home directory (USER_HOME/.gradle by default), or in your project directory. F or multi-project builds, you can put the gradle.properties file in the directory of any sub-project. T he project object gives you access to properties in gradle.properties. The property file in the user's home directory is accessed more first than the property file in the project directory.
You can also add properties directly to your project object by using the -P command line option. I n more usage, you can even pass properties directly to project objects through system and environment properties. F or example, if you run a build on a continuously integrated server, but you don't have administrator rights for the machine, and your build script requires property values that you can't let others know about, you can't use the -P option. I n this case, you can add an environment property in the Project Management section, which is not visible to ordinary users. I f the environment properties follow ORG_GRADLE_PROJECT_propertyName pattern of "somevalue", the propertyName here is added to your project object. W e also support the same mechanism for system properties. The only difference is that it is the pattern of org.gradle.projectpropertyName.
You can also set system properties with the gradle.properties file. I f a property in such a file has a systemProp. prefix, the property and its value are added to the system property without prefix. S ystemProp in multi-project builds, except in the root project. P roperty sets are ignored. T hat is, only systemProp in the root projectgradle.properties file. The property is treated as a system property.
Set properties using the gradle.properties file
gradle.properties
gradlePropertiesProp=gradlePropertiesValue
systemProjectProp=shouldBeOverWrittenBySystemProp
envProjectProp=shouldBeOverWrittenByEnvProp
systemProp.system=systemValue
build.gradle
task printProps << {
println commandLineProjectProp
println gradlePropertiesProp
println systemProjectProp
println envProjectProp
println System.properties['system']
}
gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
output
> gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
commandLineProjectPropValue
gradlePropertiesValue
systemPropertyValue
envPropertyValue
systemValue
When you want to use a variable, you can access the properties of a project in the build script only by its name. I f this property does not exist, an exception is thrown and the build fails. I f your build script relies on optional properties that users might set up in a file such as gradle.properties, you need to check for their presence before accessing them. You can check by using the method hasProperty ('propertyName'), which returns true or false.
You can use external build scripts to configure the current project. E verything in the Gradle build language can also be used in external scripts. You can even apply additional scripts to external scripts.
Configure the project using an external build script
build.gradle
apply from: 'other.gradle'
other.gradle
println "configuring $project"
task hello << {
println 'hello from other script'
}
The output of the gradle -q hello
> gradle -q hello
configuring root project 'configureProjectUsingScript'
hello from other script
You can configure any object in the following very easy-to-understand way.
Configure any object
build.gradle
task configure << {
pos = configure(new java.text.FieldPosition(10)) {
beginIndex = 1
endIndex = 5
}
println pos.beginIndex
println pos.endIndex
}
The output of the gradle -q configure
> gradle -q configure
1
5
You can also configure any object using an external script
Use scripts to configure any object
build.gradle
task configure << {
pos = new java.text.FieldPosition(10)
// Apply the script
apply from: 'other.gradle', to: pos
println pos.beginIndex
println pos.endIndex
}
other.gradle
beginIndex = 1;
endIndex = 5;
The output of the gradle -q configure
> gradle -q configure
1
5
To improve responsiveness, Gradle caches all compiled scripts by default. T his includes all build scripts, initialization scripts, and other scripts. T he first time you run a project build, Gradle creates a .gradle directory to hold compiled scripts. T he next time you run this build, if the script has not been modified since it was compiled, Gradle will use the compiled script. O therwise, the script is recompiled and the latest version is cached. I f you run Gradle with the --recompile-scripts option, the cached script is discarded and then recompiled and exists in the cache. In this way, you can force Gradle to rebuild the cache.