Task execution

As mentioned earlier, a task is nothing but a single or group of actions that is executed to perform certain operations. You can add multiple actions to doFirst or doLast closures if needed. The doFirst closure will always execute before the doLast closure. You can add the actions to the task even after task definition.

For example, add the following statements after the sampleTask2 task is mentioned in the preceding script.

sampleTask2.doFirst { println "Actions added separately" }
sampleTask2.doLast { println " More Actions added " }

The preceding statement will add two more additional actions to sampleTask2. Gradle provides one short notation for doLast, which is <<.

In Groovy, << is the left shift operator to append elements to a list:

task sampleTask3 << {
        println "Executing task3"
}
sampleTask3.doFirst {println "Adding doFirst action" }

Try to execute sampleTask3 and review the output:

$ gradle sampleTask3
...
:sampleTask3
Adding doFirst action
Executing task3

BUILD SUCCESSFUL

If multiple tasks are mentioned on the command line, they will be executed in the order defined (unless some dependency is applied on the tasks).

..................Content has been hidden....................

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