Gradle Extra Properties: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 20: Line 20:
}
}
</syntaxhighlight>
</syntaxhighlight>
For the above example, './gradlew displayColor' will display "red".
For the above example, <code>./gradlew displayColor</code> will display "red".


Note that the "ext" script block or 'project.ext' syntax must only be used at declaration, for subsequent access the extra property can be looked up directly on the project instance.
Note that the "ext" script block or <code>project.ext</code> syntax must only be used at declaration, for subsequent access the extra property can be looked up directly on the project instance.

Revision as of 03:41, 5 October 2020

Internal

Overview

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 a user-defined variable. Internally, these properties are stored as key-value pairs in a map, associated with the project.

To add properties, use the "ext" namespace:

ext {
    color = 'blue'
}

project.ext.color = 'green'
project.color = 'red' // project.ext.color and project.color are equivalent after property initialization

task displayColor {
    doLast {
        println color
    }
}

For the above example, ./gradlew displayColor will display "red".

Note that the "ext" script block or project.ext syntax must only be used at declaration, for subsequent access the extra property can be looked up directly on the project instance.