Chapter 2. Gradle Tasks

Inside of a Gradle build file, the fundamental unit of build activity is the task. Tasks are named collections of build instructions that Gradle executes as it performs a build. You’ve already seen examples of tasks in Chapter 1, and they may seem like a familiar abstraction compared to other build systems, but Gradle provides a richer model than you may be used to. Rather than bare declarations of build activity tied together with dependencies, Gradle tasks are first-class objects available to you to program if you desire.

Let’s take a look at the different ways of defining a task, the two key aspects of task definitions, and the task API we can use to perform our own customization.

Declaring a Task

In the introduction, we saw how to create a task and assign it behavior all at the same time. However, there’s an even simpler way to create a task. All you need is a task name (Example 2-1).

Example 2-1. Declaring a task by name only
task hello

You can see the results of this by running gradle tasks (Example 2-2).

Example 2-2. Gradle’s report of the newly created task
 ------------------------------------------------------------ Root Project ------------------------------------------------------------ Help tasks ---------- dependencies - Displays the dependencies of root project 'task-lab'. help - Displays a help message projects - Displays the subprojects of root project 'task-lab'. properties - Displays the properties of root project 'task-lab'. tasks - Displays the ...

Get Building and Testing with Gradle now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.