Gradle Extra Properties: Difference between revisions
No edit summary |
|||
Line 2: | Line 2: | ||
* [[Gradle Runtime and Project Configuration#Gradle_Extra_Properties|Gradle Runtime and Project Configuration]] | * [[Gradle Runtime and Project Configuration#Gradle_Extra_Properties|Gradle Runtime and Project Configuration]] | ||
=Overview= | =Overview= | ||
An '''extra property''' is a property that can be declared on most model object instances available to the closure being executed. Extra properties allow defining variables that are used multiple times within the same script, such as a file that is referred from multiple locations of the build script. An extra property is equivalent to a user-defined variable. Internally, extra properties are stored as key-value pairs in a map, associated with the model object instance, | An '''extra property''' is a property that can be declared on most model object instances available to the closure being executed. Extra properties allow defining variables that are used multiple times within the same script, such as a file that is referred from multiple locations of the build script. An extra property is equivalent to a user-defined variable. Internally, extra properties are stored as key-value pairs in a map, associated with the model object instance. | ||
For [[settings.gradle]], the available model objects are [[Gradle_Settings|Settings]] and [[Org.gradle.api.invocation.Gradle|Gradle]] (ProjectDescriptor does not allow defining extra properties). [[Gradle_Settings|Settings]] allows defining extra properties, but those properties cannot be easily accessed at [[Gradle_Concepts#Configuration|configuration]] and [[Gradle_Concepts#Configuration|execution]] phase, so the best candidate for defining extra properties during the configuration phase is [[Org.gradle.api.invocation.Gradle|Gradle]]. | |||
To add properties, use an "ext" namespace closure: | To add properties, use an "ext" namespace closure: |
Revision as of 03:56, 5 October 2020
Internal
Overview
An extra property is a property that can be declared on most model object instances available to the closure being executed. Extra properties allow defining variables that are used multiple times within the same script, such as a file that is referred from multiple locations of the build script. An extra property is equivalent to a user-defined variable. Internally, extra properties are stored as key-value pairs in a map, associated with the model object instance.
For settings.gradle, the available model objects are Settings and Gradle (ProjectDescriptor does not allow defining extra properties). Settings allows defining extra properties, but those properties cannot be easily accessed at configuration and execution phase, so the best candidate for defining extra properties during the configuration phase is Gradle.
To add properties, use an "ext" namespace closure:
ext {
color = 'blue'
}
An equivalent assignment is:
project.ext.color = 'blue'
A benefit of using the closure over the other syntax is that multiple assignments can be declared compactly:
ext {
color = 'blue'
shape = 'square'
size = 10
}
Note that the "ext" script block or project.ext
syntax must be used only for the declaration of the property, for subsequent access the extra property can be looked up directly on the project instance.
project.ext.color = 'green'
project.color = 'red' // project.ext.color and project.color are equivalent after property initialization
task displayColor {
doLast {
println project.ext.color
println project.color
println color
}
}
For the above example, ./gradlew displayColor
will display "red".