Gradle – Task

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


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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with
Get started
%d bloggers like this: