Adding additional properties to tasks

A task object already has several properties and methods. However, we can add any arbitrary new property to a task and use it. Gradle provides an ext namespace for the task object. We can set new properties and use them again once they are set. We can either set a property directly or use a closure to set a property with a value. In the following sample, we print the value of the message task property. The value of the property is assigned with the simple.ext.message = 'world' statement:

// Create simple task. 
task simple << { 
    println "Hello ${message}" 
} 
 
// We set the value for 
// the non-existing message 
// property with the task extension 
// support. 
simple.ext.message = 'world' 

When we run the task, we ...

Get Gradle Effective Implementations Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.