Gradle - Tasks


Advertisements

Gradle build script describes about one or more Projects. Each project is made up of different tasks. A task is a piece of work which a build performs. The task might be compiling some classes, storing class files into separate target folder, creating JAR, generating Javadoc, or publishing some achieves to a repositories.

This chapter explains about what is task and how to generate and execute a task.

Defining Tasks

Task is a keyword which is used to define a task into build script. Take a look into the following example which represents a task named hello that prints howcodex. Copy and save the following script into a file named build.gradle. This build script defines a task name hello which is used to print howcodex string.

task hello {
   doLast {
      println 'howcodex'
   }
}

Execute the following command in the command prompt. It executes the above script. You should execute this where the build.gradle file is stored.

C:\> gradle –q hello

Output:

howcodex

You can simplify this hello task by specifying a shortcut (represents a symbol <<) to the doLast statement. If you add this shortcut to the above task hello it will look like the following script.

task hello << {
   println 'howcodex'
}

You can execute the above script using gradle –q hello command.

Here are some variations in defining a task, take a look at it. The following example defines a task hello.

Copy and save the following code into build.gradle file.

task (hello) << {
   println "howcodex"
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output:

howcodex

You can also use strings for the task names. Take a look at the same hello example. Here we will use String as task.

Copy and save the following code into build.gradle file.

task('hello') << {
   println "howcodex"
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output:

howcodex

You can also use alternative syntax for defining a task. That is using create() method to defining a task. Take a look into the same hello example given below.

Copy and save the below given code into build.gradle file.

tasks.create(name: 'hello') << {
   println "howcodex"
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output:

howcodex

Locating Tasks

If you want to locate tasks that you defined in the build file, then you have to use respective standard project properties. That means each task is available as a property of the project, using the task name as the property name.

Take a look into the following code that accessing tasks as properties.

Copy and save the below given code into build.gradle file.

task hello

println hello.name
println project.hello.name

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output:

hello
hello

You can also use all the properties through the tasks collection.

Copy and save the following code into build.gradle file.

task hello

println tasks.hello.name
println tasks['hello'].name

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output:

hello
hello

You can also access the task's path using the tasks. For this you can call the getByPath() method with a task name, or a relative path, or an absolute path.

Copy and save the below given code into build.gradle file.

project(':projectA') {
   task hello
}
task hello

println tasks.getByPath('hello').path
println tasks.getByPath(':hello').path
println tasks.getByPath('projectA:hello').path
println tasks.getByPath(':projectA:hello').path

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output:

:hello
:hello
:projectA:hello
:projectA:hello

Adding Dependencies to Tasks

You can make a task is dependent on another task that means when one task is done then only other task will start. Each task is differentiate with the task name. Collection of task names is referred by its tasks collection. To refer to a task in another project, you should use path of the project as a prefix to the respective task name.

The following example which adds a dependency from taskX to taskY.

Copy and save the below given code into build.gradle file. Take a look into the following code.

task taskX << {
   println 'taskX'
}
task taskY(dependsOn: 'taskX') << {
   println "taskY"
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q taskY

Output:

taskX
taskY

The above example is adding dependency on task by using its names. There is another way to achieve task dependency that is define the dependency using a Task object.

Let us take the same example of taskY being dependent on taskX but we are using task objects instead of task reference names.

Copy and save the following code into build.gradle file.

task taskY << {
   println 'taskY'
}
task taskX << {
   println 'taskX'
}
taskY.dependsOn taskX

Execute the following command in the command prompt. You should execute this where the build.gradle file is stored.

C:\> gradle –q taskY

Output:

taskX
taskY

The above example is adding dependency on task by using its names. There is another way to achieve task dependency that is define dependency using a Task object.

Here we take the same example that taskY is depend on taskX but we are using task objects instead of task references names. Take a look into it.

Copy and save the below given code into build.gradle file. Take a look into the following code.

task taskX << {
   println 'taskX'
}
taskX.dependsOn {
   tasks.findAll { 
      task → task.name.startsWith('lib') 
   }
}
task lib1 << {
   println 'lib1'
}
task lib2 << {
   println 'lib2'
}
task notALib << {
   println 'notALib'
}

Execute the following command in the command prompt. It executes the above given script. You should execute this, where the build.gradle file stores.

C:\> gradle –q taskX

Output:

lib1
lib2
taskX

Adding a Description to a Task

You can add a description to your task. This description is displayed when executing Gradle tasks. This is possible by using the description keyword.

Copy and save the following code into build.gradle file. Take a look into the following code.

task copy(type: Copy) {
   description 'Copies the resource directory to the target directory.'
   from 'resources'
   into 'target'
   include('**/*.txt', '**/*.xml', '**/*.properties')
   println("description applied")
}

Execute the following command in the command prompt. You should execute this where the build.gradle file is stored.

C:\> gradle –q copy

If the command is executed successfully, you will get the following output.

description applied

Skipping Tasks

Skipping tasks can be done by passing a predicate closure. This is possible only if method of a task or a closure throwing a StopExecutionException before the actual work of a task is executed.

Copy and save the following code into build.gradle file.

task eclipse << {
   println 'Hello Eclipse'
}

// #1st approach - closure returning true, if the task should be executed, false if not.
eclipse.onlyIf {
   project.hasProperty('usingEclipse')
}

// #2nd approach - alternatively throw an StopExecutionException() like this
eclipse.doFirst {
   if(!usingEclipse) {
      throw new StopExecutionException()
   }
}

Execute the following command in the command prompt. You should execute this where the build.gradle file is stored.

C:\> gradle –q eclipse

Task Structure

Gradle has different phases, when working with tasks. First of all, there is a configuration phase, where the code, which is specified directly in a task's closure, is executed. The configuration block is executed for every available task and not only for those tasks which are later actually executed.

After the configuration phase, the execution phase runs the code inside the doFirst or doLast closures of those tasks, which are actually executed.

Advertisements