Gradle Variables and Properties
External
Internal
Overview
Concepts
Properties
An important characteristic of any property type is that an attempt to access a property that was not explicitly defined using one of the means described below will trigger a build error.
task printProp {
doLast {
println someProperty
}
}
FAILURE: Build failed with an exception. * Where: Build file '/Users/ovidiu/playground/gradle/concepts/build.gradle' line: 11 * What went wrong: Execution failed for task ':printProps'. > Could not get unknown property 'someProperty' for task ':printProps' of type org.gradle.api.DefaultTask.
System Properties
Gradle Properties
These are properties that can be used to configure aspects of a Gradle build run, regardless of the specific of the project being built. They can be seen as Gradle runtime configuration options, and can be used to configure the following aspects:
- Build artifact caching: org.gradle.caching, org.gradle.caching.debug.
- Configuration on demand: org.gradle.configureondemand.
- Gradle runtime configuration, such as console output coloring and verbosity, running in debug mode, JVM arguments, logging level, running in parallel mode, etc.
- Whether Gradle Daemon runs or not and its operational parameters.
Project Properties
A Gradle project properties are key value pairs that can be accessed directly from build scripts without any qualifications - they can be considered logically attached directly to the project.
Declaring Properties
These configuration methods are listed in the descending order of their precedence, with the highest precedence at the top. The first one encountered wins.
Command Line Flags
Command line flags have precedence over properties and environment variables with the same effect. See:
Command Line Property Definitions
-D
Command Line Project Properties
Project properties can be declared on command line with "-P="
... -PprojectProp1=blue ...
gradle.properties Files
gradle.properties can be used to define properties for the build. gradle.properties files can be placed either in Gradle user home directory (~/.gradle/gradle.properties) or in the project root directory. If both files are present, their content is merged and the configuration that comes from the user home directory takes precedence over the configuration that comes from the project root directory. If a command line -Dproperty-name=... is specified, the value overrides the value read from the property files, if any.
Form a security perspective, it makes sense to use the user home directory if the project is checked into a repository and we do not want to maintain sensitive information in that file.
someUser=someuser
somePassword=somepassword
Gradle User Home gradle.properties
This location is configurable as GRADLE_USER_HOME environment variable.
Project Root gradle.properties
This property definition vehicle is important because it can be stored in source control, so the entire team working on the project can share the same configuration.
Environment Variables
Environment variables such as GRADLE_OPTS and JAVA_OPTS are sourced by the environment that executes Gradle. These environment variables can be used to provide properties definition, but they are only available in a specific user's environment, and do not propagate to version control. For definitions that propagate to version control, so an entire team can use them, use the project root gradle.properties.
Accessing Properties
The effective value of the properties can be displayed with:
gradle properties