Gradle Projects and Tasks
The gradle build is made of one or more projects. Everything in Gradle is based on the project and task.
Gradle Tasks
In Gradle, Task is a single unit of work that a build performs.These tasks can be compiling classes, creating a JAR, Generating Javadoc, and publishing some archives to a repository and more. It can be considered as a single atomic piece of work for a build process.
Gradle’s strength lies in his extensible model; tasks are the heart of this model. A task is a self-contained unit by which the Gradle functions. The essence of a task is its action. For example, we can declare a task to compile Java source code or copy some data from one directory to another directory. A task can perform some action in isolation, but we can also declare dependencies on other tasks. A task can also define its input and the output of the file from which it reads and writes. This allows Gradle to determine whether a task needs to be done.
Gradle Projects
In Gradle, A project displays a library JAR or a web application. It may also serve a distribution ZIP, which is assembled from the JARs produced by other projects. A project could be deploying our application to staging or production environments. Each project in Gradle is made up of one or more tasks.
In Gradle, a project is a collection of one or more tasks. A project represents a library JAR or a web application. It may also serve a distribution ZIP, which is assembled from the JARs of different projects. A project could be deploying our application to staging or production environments. A project could be deploying our application to the staging or production environment. Every Gradle build contains one or more projects. We can relate it with an example of a building; it can have any numbers of floors.
Types of Tasks
There are two types of tasks in Gradle. They are as following:
- Default task
- Custom task
Default Tasks
Default tasks are predefined tasks of Gradle. We can define it in our projects. Gradle let us define one or more default tasks that are executed if no other tasks are specified.
We can list the default tasks by running the gradle task command. Consider the below output:
Custom tasks
Gradle allows us to create tasks; these tasks are called custom tasks. Custom tasks are user-defined tasks that are built to perform some specific work.
Syntax:
Follow the below steps to create a custom task:
Step1: Create a Gradle project. To create a Gradle project, open eclipse and navigate to new-> other and select the Gradle project. After that, follow some necessary steps.
Step2: Explore the project and open build.gradle file.
Step3: Add task code in build.gradle file. Consider the example below:
Step 4: Open the command line and change directory to the Gradle project’s location. Also, we can do so from the eclipse, right-click on the project and select properties.
Click on resources and then click on the location.
In address bar type cmd to open the command line in this directory
Step5: To know the default tasks and define the task in the project, type the gradle tasks command.
Step6: Now, to execute our custom task, run the command gradle taskname (as task name is tutoraspire, so gradle tutoraspire). Consider the below output:
Defining tasks using a DSL specific syntax
In a new folder, create a build.gradle file (To write in Groovy DSL), or a build.gradle.kts file (To write in Kotlin DSL) with the following code:
The above Groovy code will register a new required task called hello and will print the message to the console.
Save the file and run the below command:
write in Kotlin DSL) with the following code:
Our new task will appear under other tasks.
To verify that our task has been created, run the below command:
Defining tasks using Strings for task names:
We can define the tasks using string for task names. For example, we want to define a task name ‘hello,’ put the below code in the build.gradle file:
There is another way to define tasks (we can also define tasks by using task container).
Defining tasks using the tasks container:
A TaskContainer manages a set of Task instances. We can access a TaskContainer instance by calling Project.getTasks(), or we can use the tasks property in our build script.
A ‘hello world’ example of defining a task using TaskContainer is as follows:
Here we can add the tasks to the tasks collection.
The doFirst and doLast block in Custom Task
In Gradle, doFirst and doLast are two different blocks of custom task. These blocks are defined inside the block section. In doFirst block, we determine the task that we want to be executed first, whereas the doLast block is used to run the task at last. Consider the below code snippet:
Copy Task in Gradle
Gradle allows us to copy a task from one directory to another directory. Following syntax is used to copy the task:
Tasks Grouping
In Gradle, We can group the tasks. To group the tasks, we will use the same group name for all the tasks. To group two tasks called tutoraspire1 and tutoraspire2, write the below code to build.gradle file:
Skip a Task
In Gradle, there are multiple ways to skip the execution of a task. To skip a task, attach onlyIf() with the task code. Consider the below code snippet:
Skip a task using a Predicate
In Gradle, onlyif() method can be attached with a predicate. In this scenario, the task only executes when the predicate is true. The predicate is implemented as a closure and is passed with a parameter. If the task is executed, it returns true else it returns false. If the task is skipped. The predicate is evaluated before the task. Consider the below code snippet:
Skip a Task using StopExecutionException
In Gradle, we can also skip a task using StopExecutionException instead of predicate. If this exception is thrown to a task, the action of the task is skipped, and the build continues to execute the next task. Consider the below code snippet:
Enabling and Disabling Tasks
Gradle allows us to enable and disable a task; by default, it is enabled. To disable a task, we have to label it as SKIPPED. Consider the below code snippet:
Task dependency in Gradle
Task dependency is used when a task depends on another task. The dependsOn keyword is used with the task name to make a task dependent.
Consider the below example, it contains two tasks called tutoraspireOperation1 and tutoraspireOperation2. The second task has ‘depends on’ keyword with first task, it means the second task only executes when the first task is successfully executed:
Shortcut notation for a task in Gradle
We can create shortcuts of the existing tasks and each task is treated as the property of build script. To do so, add the below code snippet in build.gradle file.
Custom property in a Task
In Gradle, we can create and add our properties to a task. To add a property, set ext.myProperty as the initial value of a task. Consider the below code snippet: