gradle.properties, -P and ext
A build reads values from four places, and the order they win in is worth knowing before the first one surprises you. This lesson shows the file, the command line and the script, with a task that prints what it sees.
The properties file
gradle.properties
# A project property of this build.
shop.release=false
# A property of the build tool itself.
org.gradle.parallel=true
A line without a dot in front of org.gradle is a project property: the script can read it and so can a plugin. The ones that start with org.gradle. and systemProp. configure the build tool and the JVM of the build instead. Booleans are the strings true and false; a number is a string until a script turns it into one.
Who wins
From strongest to weakest: a -P on the command line, the gradle.properties of the project, the same file in GRADLE_USER_HOME (~/.gradle), and a property set in the script with ext or project.properties. An environment variable of the name ORG_GRADLE_PROJECT_shop_release counts as a command-line property, which is how CI passes a value without changing the file.
A property the script reads
build.gradle
plugins {
id 'java'
}
def release = providers.gradleProperty('shop.release').getOrElse('true')
ext.flavor = 'vanilla'
tasks.register('show') {
group = 'help'
doLast {
println "release: ${release}"
println "flavor: ${project.flavor}"
println "cli: ${project.findProperty('fromCli') ?: 'none'}"
}
}
providers.gradleProperty reads the property without failing when it is missing, and getOrElse gives the default. That is the form the configuration cache likes: a provider is a value that Gradle records, not a lookup that runs whenever it is asked.
gradle -q show --console=plain --offline --no-daemon
release: false
flavor: vanilla
cli: none
The file said false, so the first run prints false. The second one overrides it from the command line and adds a property the file does not have:
gradle -q show -Pshop.release=true -PfromCli=yes --console=plain --offline --no-daemon
release: true
flavor: vanilla
cli: yes
ext and extra properties
ext is a bag of values a script attaches to the project: ext.flavor = 'vanilla' creates a property any script of the build can read as project.flavor. It is how a Groovy build passed values between scripts before providers existed, and it still works, but Gradle treats an extra property as an input of everything that reads it, so the configuration cache is invalidated more often than with a typed extension.
A value a build needs to share today is better as a provider in gradle.properties or as an extension of its own: a small class with a Property field, registered with extensions.create. The convention plugin lesson writes that extension in full. For one value passed from the root script to a subproject, ext is still the shortest way.
Related
- The daemon and faster builds: the org.gradle.* properties worth setting.
- Build and configuration caching: why a provider is friendlier to the cache than a lookup.
- Properties in Maven: the same idea in a pom.
Article author: Arthur Isaev