Tech and Media Labs
This site uses cookies to improve the user experience.




Gradle Tasks

Jakob Jenkov
Last update: 2015-08-04

Gradle tasks define the actions needed to build your project. Without tasks your build script will not build anything when Gradle executes it. In this tutorial we will see how to define tasks in Gradle build scripts.

Defining Gradle Tasks

You define a Gradle task inside the Gradle build script. You can define the task pretty much anywhere in the build script.

A task definition consists of the keyword task and then the name of the task, like this:

task myTask

This line defines a task named myTask. The task doesn't do anything yet - it has only been defined.

Task Execution Phases

When a Gradle task is executed the execution passes through two phases:

  • first
  • last

A task is represented internally in Gradle as an object. To hook into the two build phases you must call one of the corresponding methods, passing a closure as parameter (a closure is an anonymous function). The corresponding build phase methods are:

  • doFirst()
  • doLast()

Here is an example of how to call these two methods in a task:

task myTask

myTask.doFirst( {
    println "This is done first"
} );

myTask.doLast( {
    println "This is done last"
} );

First, this example defines a task named myTask. This you have already seen earlier in this tutorial.

Second, this example calls the myTask doFirst() method, passing along a closure as parameter. The closure is contained within the { and } characters. Everything inside the curly brackets ({ and }) is interpreted as a separate function.

Third, this example calls the myTask doLast() method, passing along another closure as parameter.

When the task myTask is executed Gradle will first execute all closures passed as parameters the doFirst() method, and then all closures passed as parameters the doLast() method.

Shorter Syntax

A Gradle script is a Groovy script and Groovy allows a few syntax optimizations to make your Groovy scripts shorter.

First of all you do not need the semicolon ; after the doFirst() and doLast() method calls, if the code line with the method calls ends with a new line.

Second, you do not need the parentheses around the closure when calling doFirst() or doLast().

With these two syntax optimizations you can write the doFirst() and doLast() method calls like this:

myTask.doFirst {
    println "This is done first"
}

myTask.doLast {
    println "This is done last"
}

This shorter syntax is what you will most often see in the official Gradle documentation and in other Gradle tutorials.

doLast() Shortcut Notation

Often you will not need to pass closures to both doFirst() and doLast() . It is quite common to pass just a single closure to the doLast() method. Because this is such a common use case, Gradle has a shortcut for passing a closure to the doLast() method. Here is how the shortcut doLast() call notation looks:

myTask <<{
    println "This is done last"
}

The two characters << corresponds to calling the doLast() method like this:

myTask.doLast {
    println "This is done last"
}

You can even define the task on the same line as you pass the closure to the doLast() method. Here is an example of how that looks:

task myTask << {
    println "This is done last"
}

Defining the task and last closure in the same Groovy statement makes the build script more concise.

Multiple Closures For the Same Execution Phase

It is actually possible to add multiple closures for the same task execution phases. Here is an example:

myTask.doLast {
    println "This is done 2nd last"
}
myTask.doLast {
    println "This is done last"
}

This examples adds two closures to the last execution phase.

Multiple closures added to the same execution phase will be executed in the sequence they were added to the execution phase. In the example above, first the closure printing the text "This is done 2nd last" would be executed, then the closure printing the "This is done last" text would be executed.

All closures added to the first execution phase will of course be executed before any of the closures added to the last execution phase.

You can also add multiple closures using the doLast() shortcut notation. Here is an example:

myTask << {
    println "This is done 2nd last"
}
myTask << {
    println "This is done last"
}

Default Tasks

A Gradle build script can contain one or more default tasks. A default task is a task that is executed if you do not provide any task name to execute to the gradle command. If you open a command prompt and change directory into the directory where your project (incl. gradle build script) is located, and execute this command:

gradle

Then Gradle will execute the default tasks listed in the Gradle build script for that project.

You specify the default task for a Gradle build script like this:

defaultTasks 'firstTask'

This example sets the task named firstTask as default task for the gradle build script.

If you need more than one default task, you can list the extra tasks after the first task, with the task names separated by comma. Here is an example:

defaultTasks 'firstTask', 'secondTask'

Task Dependencies

A task can depend on another task, just like in Ant. If task B depends on task A, that means that task A must have been executed before task B is executed. If you execute task B then Gradle will detect the dependency and execute task A first, then task B.

Gradle task dependencies are listed inside parentheses after the task name. Here is a Gradle task dependency example:

task firstTask << {
    println "This is first task"
}

task myTask (dependsOn:firstTask) << {
    println "This is myTask"
}

This example defines two tasks called firstTask and myTask. The myTask task depends on the firstTask task. This is stated inside the parentheses after the name of myTask. More specifically, by this code:

(dependsOn:firstTask)

When you execute myTask Gradle will first execute firstTask and then myTask because myTask depends on firstTask .

Real Build Actions

The tasks you have seen so far only printed out text. I kept the tasks simple so that you could more easily understand what was going on. In later texts in this Gradle tutorial trail you will see how to actually compile Java code, run unit tests, create JAR files and similar build actions.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC