-
Notifications
You must be signed in to change notification settings - Fork 0
Gradle tips
Since Gradle 2.0, the creation of new properties on Project object is not allowed anymore. A workaround is to use extension objects.
For more, check https://discuss.gradle.org/t/is-it-possible-to-use-project-setpropertiy-for-a-property-which-is-not-declared-in-gradle-properties/5261
Because of this, the javaPropFile plugin (lastest version 1.0.0, at the 2015/11/17) isn’t able anymore to use its DotDeref behavior.
It means that:
x.a=eks
# No whitespace immediately after key x.a so property name is x.a.
vs.
x.a =eks
# Whitespace immediately after key x.a so this means
# property a of object x.
# NO!!!! This is NOT working anymore!
# You will face an error:
# > Assignment to 'x.a' of value 'eks' failed
The dotDeref behaviour is now only working for ALREADY EXISTING properties, like compileJava.options.debug
.
Using this kind of properties, you can still use the notation:
# only possible use of dotDeref behaviour from Gradle 2.0
compileJava.options.debug(Boolean) =false
Here is a possible, simple, workaround using the native Gradle config
object:
def loadProperties(String sourceFileName) {
def config = new Properties()
def propFile = new File(sourceFileName)
if (propFile.canRead()) {
config.load(new FileInputStream(propFile))
for (Map.Entry property in config) {
ext[property.key] = property.value;
}
}
}
As explained in the user guide:
Extra properties can be added, read and set via the owning object's "ext" property.
This OWNING object is DIFFERENT depending on where you declare the extra property !
-
if NOT in a task, meaning directly in the build script, the owning object is the
Project
object -
if in a task, the owning object is the Task itself, accessible using the implicit parameter
"it"
(for the "it" implicit parameter, see http://docs.groovy-lang.org/latest/html/documentation/#implicit-it)
Example:
// the OWNING OBJECT of the following extra properties is the Project object, as they are declared in the build script
ext {
springVersion = '3.1.0.RELEASE'
emailNotification = 'build@master.org'
}
task printProperties << {
// the next 2 extra properties are owned by the Project object
println 'Extra property \'ext.springVersion\' defined outside the task, directly in the build script: ' + springVersion
println 'Extra property \'ext.emailNotification\' defined outside the task, directly in the build script: ' + emailNotification
println "Does the Project object own the next.innerTaskExtraProperty defined in the build script? ${project.hasProperty('springVersion')}"
assert project.hasProperty("springVersion") == true
// the next extra property is defined IN the task, its owning object is NOT the Project object, but the task itself, accessible with the "it" parameter
ext.innerTaskExtraProperty = 'an extra property declared IN a task, its owning object is NOT the project object'
println "Does the Project object own the next.innerTaskExtraProperty defined in the task? ${project.hasProperty('innerTaskExtraProperty')}"
assert project.hasProperty("innerTaskExtraProperty") == false
println "Does the task own the next.innerTaskExtraProperty defined in it? ${it.hasProperty('innerTaskExtraProperty')}"
assert it.hasProperty("innerTaskExtraProperty") == true
}