Sub topic 10
Gradle Tasks and the Task Graph
In the last chapter you wrote a build.gradle.kts file. Gradle reads that file, but it does no work on its own. All the work happens in tasks. This chapter explains what a task is, how tasks depend on each other, and how Gradle decides which tasks to run and in what order.
What a Task Is
A task is one named piece of work in a Gradle build. Compiling your Java code is a task. Running your tests is a task. Packaging a JAR file is a task.
Each task has:
- A name, such as
compileJavaortest. You type this name to run the task. - Actions, which are the steps the task performs when it runs.
- Dependencies, which are other tasks that must finish first.
- Optional group and description text, which help people find the task in listings.
You rarely write the common tasks yourself. A plugin is a package of ready-made build logic. When you apply the application plugin, it also brings in the java plugin, and those plugins add tasks for compiling, testing, packaging, and running.
Listing the Tasks You Have
Run this command from your project folder:
./gradlew tasksGradle prints the tasks sorted into groups. Here is a shortened part of the output:
Application tasks
-----------------
run - Runs this project as a JVM application
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the classes of the 'main' feature.
Verification tasks
------------------
check - Runs all checks.
test - Runs the test suite.This list shows only tasks that have a group. To see every task, including helper tasks like compileJava, add --all:
./gradlew tasks --allRunning Tasks
You run a task by naming it after ./gradlew. You can name more than one:
./gradlew clean buildThis deletes the old build folder, then builds the project again. Gradle prints a line for each task as it runs:
> Task :clean
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
...
> Task :test
> Task :check
> Task :build
BUILD SUCCESSFUL in 4sThe label after a task name tells you what happened. NO-SOURCE means there were no input files, so the task had nothing to do. You will also see UP-TO-DATE, which means nothing changed since the last run, so Gradle skipped the work. A later chapter covers this in detail.
The Task Graph
You asked for build, but Gradle ran compileJava, jar, test, and more. It did this because build depends on other tasks. A task that depends on another cannot start until the other one finishes.
Gradle collects all these links into a task graph. A task graph is a map of which tasks need which other tasks. The graph never contains a loop, because a loop would mean a task must wait for itself. A graph like this is called a directed acyclic graph, or DAG. "Directed" means each link points one way. "Acyclic" means it has no loops.
Here is the graph for build in a simple Java project:
Read the graph from top to bottom. build sits at the bottom because it runs last. It needs both assemble (make the JAR) and check (run the tests). Your tests need your main classes, so compileTestJava waits for classes.
With the application plugin, assemble also depends on distZip and distTar. These tasks build ZIP and TAR archives you can hand to users. The diagram leaves them out to stay small.
Seeing the Plan Without Running It
You can ask Gradle to show the tasks it would run without actually running them. This is called a dry run:
./gradlew build --dry-runThe short form is -m. The output lists every task in order, each marked SKIPPED:
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:jar SKIPPED
...
:test SKIPPED
:check SKIPPED
:build SKIPPEDA dry run is a quick way to learn the graph of a task you don't know.
Leaving a Task Out
Sometimes you want to build without running the tests. Use -x (short for --exclude-task):
./gradlew build -x testGradle removes test from the graph for this run. Use this only for a quick check. Don't make it a habit, because skipped tests can hide broken code.
Writing Your Own Task
You can add tasks to build.gradle.kts. Here is a task that prints a greeting:
tasks.register("hello") {
group = "custom"
description = "Prints a greeting"
doLast {
println("Hello from Gradle")
}
}Here is what each part does:
tasks.register("hello")creates a task namedhello.groupanddescriptionmake the task show up in./gradlew tasksunder a "Custom tasks" heading.doLast { ... }adds an action. The code inside runs only when the task runs.
Run it:
./gradlew hello> Task :hello
Hello from Gradle
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executedConfiguration Time and Execution Time
A Gradle build runs in phases. First it configures, which means it reads the build script and sets up tasks. Then it executes, which means it runs the tasks in the graph.
Code inside doLast runs at execution time. Code outside doLast, but inside the register block, runs at configuration time. This is a common beginner mistake:
tasks.register("oops") {
println("This prints while Gradle sets up the task")
doLast {
println("This prints only when the task runs")
}
}If you put real work outside doLast, it can run at the wrong time, or run when you didn't ask for that task at all. Put the work inside doLast.
Linking Your Tasks Together
You can make one of your tasks depend on another with dependsOn:
tasks.register("hello") {
group = "custom"
description = "Prints a greeting"
doLast {
println("Hello from Gradle")
}
}
tasks.register("greetAll") {
group = "custom"
description = "Prints a greeting, then a farewell"
dependsOn("hello")
doLast {
println("Goodbye from Gradle")
}
}Run ./gradlew greetAll. Gradle adds hello to the graph first:
> Task :hello
Hello from Gradle
> Task :greetAll
Goodbye from GradleYou can also hook your task into the built-in graph. This line makes build run hello as part of its work:
tasks.named("build") {
dependsOn("hello")
}tasks.named finds a task that already exists so you can change it. It fails if no task has that name, which helps you catch typos.
Common Tasks at a Glance
| Task | What it does | Depends on (main ones) |
|---|---|---|
| `compileJava` | Compiles `src/main/java` | none |
| `classes` | Gathers compiled classes and resources | `compileJava`, `processResources` |
| `jar` | Packages classes into a JAR | `classes` |
| `test` | Runs unit tests | `testClasses` |
| `check` | Runs all checks, including tests | `test` |
| `assemble` | Builds all outputs | `jar` (and distributions) |
| `build` | Assembles and checks | `assemble`, `check` |
| `run` | Starts your `main` method | `classes` |
| `clean` | Deletes the `build` folder | none |
Exercise
You run ./gradlew jar in a fresh Java project. Which tasks run, and does test run?
Show answer
compileJava, processResources, classes, and then jar. The test task does not run, because jar does not depend on it. Only build and check pull in test.
Exercise
A teammate writes tasks.register("stamp") { println("Writing stamp file") } and is surprised that the message appears even when running ./gradlew test. Why, and how should they fix it?
Show answer
The println is outside doLast, so it runs at configuration time, not when the task runs. Move it into a doLast { ... } block so it runs only when the stamp task executes.
Exercise
Write a task named docsCheck in the "custom" group that prints "Checking docs" and make the built-in check task depend on it.
Show answer
tasks.register("docsCheck") { group = "custom"; description = "Checks docs"; doLast { println("Checking docs") } } and then tasks.named("check") { dependsOn("docsCheck") }. Running ./gradlew check or ./gradlew build now runs docsCheck too.