Gradle Tasks - Gradle

What is Gradle Tasks?

Gradle construct script describes one or more projects. every project is made up of different tasks. A project is a piece of work which a build plays. The project might be compiling some lessons, storing class files into separate target folder, developing JAR, generating Javadoc, or publishing some files to repositories.

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

Defining tasks

Project is a keyword that is used to define a project into build script. test the following example which represents a project named hello that printswisdomjobs. copy and keep the following script right into a document named build.gradle. This build script defines a project named ‘hello’, that is used to print wisdomjobs string.

Execute the following command inside the command prompt. It executes the above script. You must execute this wherein the build.gradle document is saved.

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

you may simplify this hello project by using specifying a shortcut (represents a symbol <<) to the doLast statement. if you upload this shortcut to the above project hello it will seem like the following script.

You may execute the above script the usage of gradle –q hello command.

Here are some variations in defining a project, test it. the following example defines a project hello.

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

Execute the following command in the command prompt. You must execute this, in which the build.gradle file is saved.

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

You may also use strings for the project names. test the equal hello example. here we can use String as project.

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

Execute the following command in the command set off. You need to execute this in which the build.gradle file is saved.

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

You may also use alternative syntax for defining a project. this is using create() technique to defining a project. test the same hello example.

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

Execute the following command inside the command prompt. You must execute this in which the build.gradle file is saved.

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

Locating Tasks

If you need to locate tasks that you defined in the build file, then you have to use respective standard project properties. that means every project is to be had as a assets of the project, the usage of the project name as the assets name.

Test the following code that accesses tasks as properties.

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

Execute the following command within the command activate. You need to execute this where the build.gradle report is saved.

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

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

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

Execute the following command in the command activate. You need to execute this wherein the build.gradle file is saved.

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

you can access tasks from any project the use of the project's path. For this you can name the getByPath() technique with a project name, or a relative path, or an absolute path.

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

Execute the following command inside the command prompt. You need to execute this where the build.gradle file is saved.

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

Adding Dependencies to Tasks

You may make a project dependent on some other project, this means that when one project is finished only then the other project will start. each project is differentiated with a project name. collection of project names is referred by its tasks collection. to refer to a project in every other project, you should use path of the project as a prefix to the respective project name.

The following example provides a dependency from taskX to taskY.

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

Execute the following command in the command prompt. You have to execute this where the build.gradle file is saved.

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

The above example is including dependency on project by using the usage of its names. there's another manner to achieve project dependency this is define the dependency using a project object.

Let us take the same example of taskY being depending on taskX however we are the use of project items in preference to mission reference names.

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

Execute the following command in the command prompt. You have to execute this where the build.gradle file is saved.

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

There is another manner to feature dependency to the project, that is, through the usage of closures. In this example, the project is released through the closure. in case you use closure in the build script that need to return a single project or series of project items. the following example adds a dependency from taskX to all of the obligations in the project, whose call starts with 'lib'.

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

Execute the following command in the command prompt. You have to execute this where the build.gradle file is saved.


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

Adding a Description to a Task

You may add a description for your project. This description is displayed when executing Gradle tasks. this is possible by way of using the description keyword.

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

Execute the following command in the command prompt. You have to execute this where the build.gradle file is saved.

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

Skipping tasks

Skipping tasks can be performed through passing a predicate closure. that is possible only if technique of a project or a closure throwing a StopExecutionException before the actual work of a project is done.

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

Execute the following command in the command prompt. You have to execute this where the build.gradle file is saved.

Task Structure

Gradle has special levels, when operating with tasks. to start with, there is a configuration phase, wherein the code, that is particular at once in a project's closure, is done. The configuration block is performed for each to be had project and not simplest for the ones tasks which are later really done.

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

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Gradle Topics