Gradle Tasks

Tasks in Gradle

In Gradle, the build script is used to describe one or more projects. Each project in Gradle is made up of different types of tasks. A task is the lines of code which is performed by the build. A task can compile classes, save the class file into different folders, create JAR files, etc.

In Gradle, a task is a keyword which is used to define a task.

There are two types of tasks in Gradle. They are as following:

  1. Default Task
  2. Custom Task

Default Task

In Gradle, the default task is system defined.

To check the default task, open the command prompt and type gradle tasks and press the enter button. All the default tasks will appear.

Gradle Tasks

Custom Task

In Gradle, the custom task is the task which a user defines to perform some task.

Syntax:

Task task_name{
         group “-------group_name for task-------“
         description “-------description of the task-------“
         doLast{
         -------code for execution-------
-----------------------------------
-----------------------------------
}
} 

Following are the steps for creating a task in Gradle:

Step 1: create a Gradle project.

Step 2: open build.gradle file

Gradle Tasks 1

Step 3: Now add the code of the below example.

Example:

taskJavatpoint_task{
     group"Custome task"
     description"Javatpointgradle Custom task Example."
     doLast{
          println"Welcome to Javatpoint. This is a Custom task Example.";
     }
} 

Step 4: Now, right-click on the project and select properties.

Gradle Tasks 2

Step 5: Click on resources and then click on the location.

Gradle Tasks 3

Step 6:  In address bar type cmd

Gradle Tasks 4

To know the default task and to define the task in the project, type the command gradle tasks.

Gradle Tasks 5

Now to execute our custom task, type the command gradle Javatpoint.

Gradle Tasks 6

What is doFirst and doLast in Custom Task?

In Gradle, doFirst and doLast are the blocks of Custom task. In both, the block tasks are written. In doFirst block, we perform the task which we want to be executed first whereas in doLast block is used to execute the task at last.

Example:

taskpqr{
     doFirst{
          println"Hello welcome to Javatpoint"
     }
     doLast{
          println"Thank You Bye Bye"
     }
} 

Output:

Gradle Tasks 7

How to Copy task in Gradle

In Gradle, to copy a task from one location to another, following syntax is used:

Syntax:

task task_name(type: Copy){
          From “--------------------Path----------------”
          Into “ --------------------Path----------------”
} 

Example:

taskxyz(type: Copy){
     from'D:\\xyz\\test.txt'
     into'E:\\pqr'
} 

Output:

Gradle Tasks 8

Tasks grouping in Gradle

In gradle, to group two or more tasks, a group name is assigned to all the tasks. Following is an example of grouping two different tasks.

Example:

taskjavatpoint1{
     group"Javatpoint_task"
     doLast{
          println"This is a task 1"
     }
}
taskjavatpoint2{
     group"Javatpoint_task"
     doLast{
          println"This is a task 2"
    }
 } 

Output:

In Command prompt type gradle tasks.

Gradle Tasks 9

Skip tasks in Gradle

In Gradle, there are multiple ways to skip the execution of a task.  To skip a task onlyIf() method is attached to the task.

Example:

taskjavatpoint1{
     group"Javatpoint_task1"
     doLast{
          println"This is a task"
     }
}
javatpoint1.onlyIf{
    project.hasProperty('doOperation')
} 

Output:

Gradle Tasks 10

Skipping task Using a predicate

In Gradle, onlyIf() method is attached to a task with a predicate. The task only executes when the predicate is true. The predicate is implemented as a closure. In a task, closure is passed with a parameter and returns true if the task is executed. It returns true else returns false. If the task is skipped, the predicate is evaluated before the task.

Example:

tasktest_Javatpoint{
     group"Task1"
     doLast{
          println"This is a task"
     }
}
test_Javatpoint.onlyIf{
     !project.hasProperty('skipTest')
} 

Output:

Gradle Tasks 11

Using StopExecutionException

In Gradle, to skip a task instead of Predicate StopExecutionException can also be used. If this exception is thrown then the action of the task is skipped, and the build continues executing the next task.

Example:

taskcompile{
     doLast{
          println"Here we are compiling"
     }
}
compile.doFirst{
     if (true)
     {
          thrownewStopExecutionException()
     }
}
taskJavatpoint (dependsOn: 'compile') {
     doLast{
          println'i am not affected'
     }
} 

Output:

Gradle Tasks 12

Enabling and disabling tasks

In Gradle, we can enable and disable a task, by default, it is enabled. To disable a task, we have to label it SKIPPED.

Example:

taskJavatpoint_disableTask{
     doLast{
          println"this statement should not be printed if the task is disabled."
          }
}
Javatpoint_disableTask.enabled = false 

Output:

Gradle Tasks 13

Task dependency in Gradle

In Gradle, Task dependency is used when task 1 depends on task 2, and task 2 is executed when task1 is successfully executed. A dependsOn keyword is used with the task name to make a task-dependent.

Example:

taskjavatpointOperation1{
     group"CustomTasks"
     doLast{
          println"This is operation 1 of Javatpoint"
     }
}
taskjavatpointOperation2(dependsOn: 'javatpointOperation1'){
     group"CustomTasks"
     doLast{
          println"This is operation 2 of Javatpoint"
     }
} 

Output:

Gradle Tasks 14

Shortcut notation for a task in Gradle

In Gradle, we can create shortcuts of the existing task. And each task is treated as the property of build script.

Example:

taskJavatpoint {
     doLast {
          println'Hello world!'
     }
}
Javatpoint.doLast {
     println"Greetings from the $Javatpoint.name task."
} 

Output:

Gradle Tasks 15

How to make Custom property in a task

In Gradle, we can create and add our own properties to a task. To add a property ext.myProperty is set to the initial value.

Example:

taskJavatpointTask {
ext.myProperty = "This is a custom property"
}
taskprintTask {
doLast {
printlnJavatpointTask.myProperty
    }
} 

Output:

Gradle Tasks 16