Creating Gradle Tasks

Gradle is based on preparing and running tasks. Tasks can be written with Groovy, and this page explains the basic ways to create them.

Defining Tasks

Gradle is a program that runs tasks through commands. Commands such as gradle compileJava and gradle run mean “run the compileJava task” and “run the run task”.

Basic task definition

Users can define their own tasks. If task processing is written in the build file, build.gradle, it can be called from the gradle command.

A task is defined in the following form.

task taskName {
    // processing to execute
}

The task keyword defines a task. Write the task name after it, then write the task contents inside {}. The following forms are also possible.

task (taskName) {...}
task ('taskName') {...}

For example, add the following code to the bottom of build.gradle.

task hello {
    println('This is the hello task.')
}

Save the file and run the following command.

$ gradle hello

The task is executed, and Gradle prints other messages in addition to the println output.

Starting a Gradle Daemon (subsequent builds will be faster)

> Configure project :
This is the hello task.


BUILD SUCCESSFUL in 16s

Running in quiet mode with -q reduces most of the Gradle output.

$ gradle -q hello

The output becomes much simpler.

This is the hello task.

doFirst and doLast

A task can be created by writing processing directly inside the task block, but this style is not commonly used for normal task actions. Typical tasks use doFirst and doLast.

task taskName {
    doFirst {
        // processing to run first
    }

    doLast {
        // processing to run last
    }
}

doFirst and doLast are closures provided inside a task. They work as follows.

  • doFirst: action executed first.
  • doLast: action executed last.

A task executes prepared actions in order. An action is a concrete unit of processing. doFirst and doLast add processing to the beginning and end of that action sequence. You do not need to define both; either one can be used alone.

task hello {
    doLast {
        println('This is doLast of the hello task.')
    }
    doFirst {
        println('This is doFirst of the hello task.')
    }
}

Run gradle hello.

> Task :hello
This is doFirst of the hello task.
This is doLast of the hello task.


BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

Although doLast appears first in the code, doFirst is executed before doLast.

Passing Parameters

Values required by a task can be passed as parameters. Inside the task, use them as variables.

task msg {
    println("you typed: " + x)
}

To set the variable x, pass it when running Gradle.

$ gradle msg -Px=value

Write -P, the variable name, and the value joined with =. For example, -Phoge=123 passes 123 to the variable hoge.

The following example receives a number and prints counts up to that number.

task hello {
    doLast {
        def n = max.toInteger()
        for(def i in 1..n){
            println("No," + i + " count.")
        }
        println("--end.")
    }
}

Run it like this.

$ gradle hello -Pmax=5

The output is as follows.

> Task :hello
No,1 count.
No,2 count.
No,3 count.
No,4 count.
No,5 count.
--end.

def n = max.toInteger() converts the variable max to an integer and assigns it to n. That value is then used in a for loop.

Calling and Depending on Other Tasks

Calling another task

Sometimes a task needs to call another task. Suppose the following tasks exist.

task a {...}
task b {...}

Calling b() like a Java method does not work. Tasks are collected in the tasks object. For example, tasks a and b can be accessed as tasks.a and tasks.b. The execute method can be called on the task object.

tasks.a.execute()
tasks.b.execute()

Example:

task hello  {
    doFirst {
        println("*** start:hello task ***")
        tasks.aaa.execute()
     }
    doLast {
        tasks.bbb.execute()
        println("*** end:hello task ***")
    }
}
 
task aaa {
    doLast {
        println("<< This is A task! >>")
    }
}
 
task bbb {
    doLast {
        println("<< I'm task B!! >>")
    }
}

Run gradle hello.

> Task :hello
*** start:hello task ***
<< This is A task! >>
<< I'm task B!! >>
*** end:hello task ***

In this example, aaa is called from doFirst and bbb is called from doLast. Check the order of the printed text to understand the execution flow.

Specifying dependent tasks

When one task must run before another task, use dependsOn.

task taskName (dependsOn : 'task') {
    // omitted
}

The following form can also be used.

task taskName {
    dependsOn : 'task'
    // omitted
}

When the task is called, Gradle runs the task specified by dependsOn first, and then runs the main task processing. To specify multiple tasks, use an array such as ['a', 'b', 'c']. They are executed in the written order.

task hello(dependsOn:['aaa', 'bbb'])  {
    doFirst {
        println("*** start:hello task ***")
    }
    doLast {
        println("*** end:hello task ***")
    }
}
 
task aaa {
    doLast {
        println("<< This is A task! >>")
    }
}
 
task bbb {
    doLast {
        println("<< I'm task B!! >>")
    }
}

Run gradle hello.

> Task :aaa
<< This is A task! >>

> Task :bbb
<< I'm task B!! >>

> Task :hello
*** start:hello task ***
*** end:hello task ***

The aaa and bbb tasks run first, and then the hello task runs. Because of dependsOn, hello does not run until its dependent tasks have completed.