The task graph
Before Gradle runs a task it builds the whole plan: every task the command needs, in an order that respects the dependencies, and nothing else. Two options show the plan without doing the work, and the plan is the best explanation of what a command really does.
--dry-run
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java'
id 'application'
}
application {
mainClass = 'com.example.App'
}
src/main/java/com/example/App.java
package com.example;
public final class App {
public static void main(String[] args) {
System.out.println("shop");
}
}
--dry-run prints the plan and marks every task SKIPPED. Nothing is compiled and nothing is written:
gradle build --dry-run --console=plain --offline --no-daemon
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:jar SKIPPED
:startScripts SKIPPED
:distTar SKIPPED
:distZip SKIPPED
:assemble SKIPPED
:compileTestJava SKIPPED
:processTestResources SKIPPED
:testClasses SKIPPED
:test SKIPPED
:check SKIPPED
:build SKIPPED
BUILD SUCCESSFUL in 13s
--info and the plan
--info runs the build and prints what it decided on the way. Among thousands of lines there is one that lists the plan as data:
gradle build --info --console=plain --offline --no-daemon 2>&1 | grep "Tasks to be executed"
Tasks to be executed: [task ':compileJava', task ':processResources', task ':classes', task ':jar', task ':startScripts', task ':distTar', task ':distZip', task ':assemble', task ':compileTestJava', task ':processTestResources', task ':testClasses', task ':test', task ':check', task ':build']
The same fourteen tasks as above, in the order they run, with the project path in front of each name. The list comes from the graph, not from the script: the application plugin added startScripts, distTar and distZip to assemble, and the java plugin added the three verification tasks.
Reading the graph
- A command names the end of the plan. build is not a step: it is a task with dependencies, and everything else hangs below it.
- A task appears once. Two tasks may depend on the same third one, and Gradle runs it once for the build, not once per path.
- Order inside a task does not matter. The graph is a set of tasks with edges; two tasks with no edge between them may run in either order, and with --parallel even at the same time.
- A failing task stops the rest. After a failure Gradle runs only the tasks that are finalizedBy the failed one, then stops the build.
Related
- Task dependencies: the three relations that shape this graph.
- The java plugin and its lifecycle: the tasks of the plan, one by one.
- The daemon and faster builds: what --parallel does to a graph with independent branches.
Автор статьи: Arthur Isaev