Gradle - Tasks


Advertisements

Gradle build script describes about one or more Projects. Each project is made up of different tasks and 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 the 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 at the following example which represents a task named hello that prints tutorialspoint. 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 tutorialspoint string.

task hello {
   doLast {
    println 'tutorialspoint'
   }
}

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

\

Given below is the output of the code −

tutorialspoint

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 be as follows −

task hello << {
   println 'tutorialspoint'
}

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

The following example defines a task hello.

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

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

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

C:\> gradle –q hello

Output

The output is shown below −

tutorialspoint

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 "tutorialspoint"
}

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

C:\> gradle –q hello

Output

When you execute the above code, you should see the following output −

tutorialspoint

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

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

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

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

C:\> gradle –q hello

Output

Upon execution, you will receive the following output −

tutorialspoint

Locating Tasks

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

Take a look into the following code that accesses the 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 script given above. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output

The output is mentioned below −

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 script which is mentioned above. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output

This produces the following output −

hello
hello

You can also access the task's path by 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 script which is given above. You should execute this, where the build.gradle file stores.

C:\> gradle –q hello

Output

The output is stated below −

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

Adding Dependencies to Tasks

You can make a task dependent on another task and that means, when one task is done then only other task will begin.

Each task is differentiated with the task name. The 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 adds a dependency from taskX to taskY.

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

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

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

C:\> gradle –q taskY

Output

The output is given herewith −

taskX
taskY

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

Let us take the same example of taskY being dependent on taskX, but here, 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

The output is given below −

taskX
taskY

The above example is adding dependency on task by using its names.

There is another way to achieve task dependency which is, to define dependency using a Task object.

Here, we take the same example that taskY is dependent on taskX but, we are using task objects instead of task references names.

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

The output is cited below −

lib1
lib2
taskX

Adding a Description

You can add a description to your task. This description is displayed when you execute the Gradle tasks and 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 it comes to working with the 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