Accessing tasks as project properties

Each task that we add is also available as a Project property, and we can reference this property like we can any other property in our build script. We can, for example, invoke methods or get and set property values of our task through the property reference. This means we are very flexible in how we create our tasks and add behaviour to the tasks. In the following script, we use the Project property reference to a task to change the description property:

task simple << { task ->
    println "Running ${task.name}"
}

// The simple task is available as project property.
simple.description = 'Print task name'
simple.doLast {
    println "Done"
}
project.simple.doFirst {
    println "Start"
}

When we run our task from the command line, we get the following output:

$ gradle -q simple
Start
Running simple
Done
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.163.197