Sunday, April 19, 2020

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:
Gradle Projects and Tasks

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:
  1. Task task_name{  
  2.     group "-------group_name for task-------'  
  3.     description '-------description of the task-------'  
  4.     doLast{  
  5.     -------code for execution-------  
  6. -----------------------------------  
  7. -----------------------------------  
  8. }  
  9. }  
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.
Gradle Projects and Tasks
Step3: Add task code in build.gradle file. Consider the example below:
  1. task Javatpoint_task{  
  2.     group "Custom task"  
  3.     description "Javatpoint Gradle Custom task Example."  
  4.     doLast{  
  5.         println "Welcome to Javatpoint. It is a Custom task Example.";  
  6.     }  
  7. }  
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.
Gradle Projects and Tasks
Click on resources and then click on the location.
Gradle Projects and Tasks
In address bar type cmd to open the command line in this directory
Gradle Projects and Tasks
Step5: To know the default tasks and define the task in the project, type the gradle tasks command.
Gradle Projects and Tasks
Step6: Now, to execute our custom task, run the command gradle taskname (as task name is Javatpoint, so gradle javatpoint). Consider the below output:
Gradle Projects and Tasks

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:
  1. tasks.register("hello") {   
  2.     doLast {   
  3.         println 'Hello, World!'  
  4.     }  
  5. }  
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:
  1. gradle tasks --all.   
Our new task will appear under other tasks.
To verify that our task has been created, run the below command:
  1. gradle tasks --all  

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:
  1. build.gradle  
  2. task('hello') {  
  3.     doLast {  
  4.         println "hello"  
  5.     }  
  6. }  
  7. task('copy', type: Copy) {  
  8.     from(file('srcDir'))  
  9.     into(buildDir)  
  10. }  
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:
  1. build.gradle  
  2. tasks.create('hello') {  
  3.     doLast {  
  4.         println "hello"  
  5.     }  
  6. }  
  7. tasks.create('copy', Copy) {  
  8.     from(file('srcDir'))  
  9.     into(buildDir)  
  10. }  
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:
  1. task A{  
  2.     doFirst{  
  3.         println "Hello welcome to Javatpoint"  
  4.     }  
  5.     doLast{  
  6.         println "Thank You Bye Bye."  
  7.     }  
  8. }  

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:
  1. task task_name(type: Copy){  
  2.     From "--------------------DirectoryPath----------------"  
  3.     Into " --------------------DirectoryPath----------------"  
  4. }  

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 javatpoint1 and javatpoint2, write the below code to build.gradle file:
  1. task javatpoint1{  
  2.     group "Javatpoint_task"  
  3.     doLast{  
  4.         println "This is a task 1."   
  5.     }  
  6. }  
  7. task javatpoint2{  
  8.     group "Javatpoint_task"  
  9.     doLast{  
  10.         println "This is a task 2."   
  11.     }  
  12. }  

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:
  1. task javatpoint1{  
  2.     group "Javatpoint_task1"  
  3.     doLast{  
  4.         println "This is a task."   
  5.     }  
  6. }  
  7. javatpoint1.onlyIf{  
  8.     project.hasProperty('doOperation')  
  9. }  

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:
  1. task test_Javatpoint{  
  2.     group "Task1"  
  3.     doLast{  
  4.         println "This is a task."   
  5.     }  
  6. }  
  7. test_Javatpoint.onlyIf{  
  8.     !project.hasProperty('skipTest')  
  9. }  

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:
  1. task compile{  
  2.     doLast{  
  3.         println "Here we are compiling"  
  4.     }  
  5. }  
  6. compile.doFirst{  
  7.     if (true)  
  8.     {  
  9.         throw new StopExecutionException()  
  10.     }  
  11. }  
  12. task Javatpoint (dependsOn: 'compile') {  
  13.     doLast{  
  14.         println 'i am not affected'  
  15.     }  
  16. }  

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:
  1. task Javatpoint_disableTask{  
  2.     doLast{  
  3.         println "this statement should not be printed if the task is disabled."  
  4.         }  
  5. }  
  6. Javatpoint_disableTask.enabled = false  

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 JavatpointOperation1 and JavatpointOperation2. The second task has 'depends on' keyword with first task, it means the second task only executes when the first task is successfully executed:
  1. task javatpointOperation1{  
  2.     group "CustomTasks"  
  3.     doLast{  
  4.         println "This is operation 1 of Javatpoint"  
  5.     }  
  6. }  
  7. task javatpointOperation2(dependsOn: 'javatpointOperation1'){  
  8.     group "CustomTasks"  
  9.     doLast{  
  10.         println "This is operation 2 of Javatpoint"  
  11.     }  
  12. }  

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.
  1. task Javatpoint {  
  2.     doLast {  
  3.         println 'Hello world!'  
  4.     }  
  5. }  
  6. Javatpoint.doLast {  
  7.     println "Greetings from the $Javatpoint.name task."  
  8. }  

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:
  1. task JavatpointTask {  
  2.     ext.myProperty = "This is a custom property"  
  3. }  
  4. task printTask {  
  5.     doLast {  
  6.         println JavatpointTask.myProperty  
  7.     }  
  8. }  








No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...