Defining tasks
A project has one or more tasks to execute some actions, so a task is made up of actions. These actions are executed when the task is executed. Gradle supports several ways to add actions to our tasks.
We can use the doFirst
and doLast
methods to add actions to our task, and we can use the left shift operator (<<
) as a synonym for the doLast
method. With the doLast
method or the left shift operator (<<
) we add actions at the end of the list of actions for the task. With the
doFirst
method we can add actions to the beginning of the list of actions. The following script shows how we can use the several methods:
task first { doFirst { println 'Running first' } } task second { doLast { Task task -> println "Running ${task.name}" } } task third << { taskObject -> println 'Running ' + taskObject.name }
When we run the script, we get the following output:
$ gradle first second third :first Running first :second Running...