Organizing tasks

In Chapter 1Starting with Gradle, we already discussed that we could use the tasks task of Gradle to see the tasks that are available for a build. Let's suppose we have the following simple build script:

defaultTasks 'second' 
 
task first << { 
    println "I am first" 
} 
 
task second(dependsOn: first) << { 
    println "I am second" 
} 

Nothing fancy here. The second task is the default task and depends on the first task. When we run the tasks task on the command line, we get the following output:

$ gradle -q tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Default tasks: second
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
components - Displays the components produced by root project 'organize'. [incubating]
dependencies - Displays all dependencies declared in root project 'organize'.
dependencyInsight - Displays the insight into a specific dependency in root project 'organize'.
help - Displays a help message.
model - Displays the configuration model of root project 'organize'. [incubating]
projects - Displays the sub-projects of root project 'organize'.
properties - Displays the properties of root project 'organize'.
tasks - Displays the tasks runnable from root project 'organize'.
Other tasks
-----------
second
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>

We see our task with the name second in the section Other tasks, but not the task with the name first. To see all tasks, including the tasks other tasks depend on, we must add the option --all to the tasks command:

$ gradle tasks --all
...
Other tasks
-----------
second
 first

Now we see our task with the name first. Gradle even indents the dependent tasks so that we can see that the second task depends on the first task.

At the beginning of the output, we see the following line:

Default tasks: second

Gradle shows us the default task in our build.

Adding a description to tasks

To describe our task, we can set the description property of a task. The value of the description property is used by the task of Gradle. Let's add a description to our two tasks, as follows:

defaultTasks 'second' 
 
// Use description property to set description. 
task first(description: 'Base task') << { 
    println "I am first" 
} 
 
task second( 
    dependsOn: first, 
    description: 'Secondary task') << { 
 
    println "I am second" 
} 

Now when we run the tasks task, we get a more descriptive output:

$ gradle tasks --all
...
Other tasks
-----------
second - Secondary task
    first - Base task

Grouping tasks together

With Gradle, we can also group tasks together in so-called task groups. A task group is a set of tasks that belong together, logically. The task group is used, for example, in the output of the tasks task that we used earlier. Let's expand our sample build script by grouping the two tasks together in a sample task group. We must assign a value to the group property of a task:

defaultTasks 'second' 
 
// Define name of the 
// task group we want to use. 
def taskGroup = 'base' 
 
task first( 
    description: 'Base task', 
    group: taskGroup) << { 
 
    println "I am first" 
} 
 
task second( 
    dependsOn: first, 
    description: 'Secondary task', 
    group: taskGroup) << { 
 
    println "I am second" 
} 

Next time when we run the tasks task, we can see our tasks grouped together in a Base tasks section:

$ gradle -q tasks --all
...
Base tasks
----------
first - Base task
second - Secondary task [first]
...

Note that the task dependency is appended to the description property of the second task.

Getting more information about a task

We can get some more information about a task with the Gradle help task. We need to specify an extra argument for the help task: --task, with the name of the task that we want more information about. Gradle will print some details about our task in the console. For example, the description and type of the task. This can be very useful to learn more about a task.

We will invoke the help task to get more information about our second task:

$ gradle help --task second
:help
Detailed task information for second
Path
:second
Type
Task (org.gradle.api.Task)
Description
Secondary task

Group
base
BUILD SUCCESSFUL
Total time: 0.58 secs
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.47.218