A Gradle build is made up of tasks. Each task represents a set of instructions to gradle for execution.
A task might consist of compiling classes, copying files from source derectory to destination.
A simple task is defined below.
task hello {
doLast {
println 'helloworld'
}
}
put the above code in a build.gradle file and execute gradle hello, it should print helloworld.
The “doLast” keyword tells the gradle to execute the statements within doLast at the last of the task execution. Similarly there is “doFirst“. As the name suggest, the doFirst will execute before any other sub task is executed within a task.
if there are multiple doLast, the last doLast will be executed in then end and then the prior and so on.
if there are multiple doFirst, the last doFirst will execute first and then prior one and so on.
You can also view the task you created by running gradle task –all under “Other Tasks“.
Task dependencies
It is possible to add dependencies in a task. so a task B might be dependent on the execution of task A. To do this, you have to add set the dependsOn attribute of a task
e.g.
task taskA {
println 'taskA'
}
task taskB(dependsOn: 'taskA') {
println "taskB"
}
The above build.gradle will always execute taskA before taskB as we have mentioned the dependency in the build file. So if you run the task taskB (gradle taskB) it will execute taskA first and then taskB