Skip to content

Gradle tips

Thomas SCHWENDER edited this page Nov 28, 2015 · 9 revisions

Gradle Tips

Creation of new properties on Project from Gradle 2.0

Since Gradle 2.0, the creation of new properties on Project object is not allowed anymore. A workaround is to use extension objects.

Impact on javaPropFile plugin

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;
        }
    }
}

Extra properties and owning object

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 !

Example:

build.gradle
// 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
}