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 reference any other property in our build script. We can, for example, invoke methods or get and set the property values of our task through the property reference. This means that we are very flexible in how we create our tasks and add behavior to the tasks. In the following script, we use the project
property reference to a task to change the description
property:
// Create a simple task. task simple << { task -> println "Running ${task.name}" } // The simple task is available as // project property. simple.description = 'Print task name' // We can invoke methods from the // Task object. simple.doLast { println "Done" } // We can also reference the task // via the project property // explicitly. project.simple.doFirst { println "Start" }
When we run our task from the command line, we get the following...