Organizing tasks

In Chapter 1, Starting with Gradle, we already learned that we could use the tasks task of Gradle to see which tasks are available for a build. Let us 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. Task second is the default task and depends on task first. 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

Help tasks
----------
dependencies - Displays the dependencies of root project 'chapter2'.
help - Displays a help message
projects - Displays the sub-projects of root project 'chapter2'.
properties - Displays the properties of root project 'chapter2'.
tasks - Displays the tasks runnable from root project 'chapter2' (some of the displayed tasks may belong to subprojects).

Other tasks
-----------
second

To see all tasks and more detail, run with --all.

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
...
Default tasks: second
...
Other tasks
-----------
second
    first
...

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

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

Default tasks: second

Gradle shows us which task is 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 tasks task of Gradle. Let's add a description to our two tasks:

defaultTasks 'second'

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 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'

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 section Base tasks:

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

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

..................Content has been hidden....................

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