The java plugin and its lifecycle
One line applies the plugin that turns a folder of Java files into a build: id 'java'. Everything after that is tasks, and the tasks have a fixed shape: compileJava compiles, test tests, jar packs, build asks for all of it, clean deletes the result.
The project
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java'
}
version = '1.0.0'
src/main/java/com/example/Price.java
package com.example;
public final class Price {
public static String format(long cents) {
return String.format("%d.%02d", cents / 100, cents % 100);
}
}
gradle build
gradle build --console=plain --offline --no-daemon
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build
BUILD SUCCESSFUL in 13s
2 actionable tasks: 2 executed
The tasks it ran
| task | what it does | depends on |
|---|---|---|
| compileJava | compiles src/main/java into build/classes/java/main | the task that copies the resources of the main source set |
| processResources | copies src/main/resources; NO-SOURCE when the folder is empty | nothing |
| classes | a lifecycle task that depends on the two above | both |
| compileTestJava | compiles src/test/java against the main classes | classes |
| test | runs the tests of the test source set | testClasses |
| jar | writes build/libs/shop-1.0.0.jar | classes |
| assemble | a lifecycle task for every archive of the project | jar |
| check | a lifecycle task for every verification | test |
| build | a lifecycle task that depends on assemble and check | both |
| clean | deletes build/ | nothing |
Three of them do nothing themselves. classes, assemble, check and build are lifecycle tasks: they exist so that a command can name a stage instead of a list of tasks, and a plugin adds its own work to the stage it belongs to.
gradle clean
gradle clean --console=plain --offline --no-daemon
> Task :clean
BUILD SUCCESSFUL in 10s
1 actionable task: 1 executed
ls build
ls: cannot access 'build': No such file or directory
gradle jar
Asking for one task runs that task and the tasks it needs, and nothing more: no tests, no verification.
gradle jar --console=plain --offline --no-daemon
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
BUILD SUCCESSFUL in 12s
2 actionable tasks: 2 executed
Related
- Source sets and the layout: the folders behind compileJava and compileTestJava.
- The jar and its manifest: the archive jar writes.
- The Maven lifecycle: the same stages, fixed by the tool instead of declared by a plugin.
Автор статьи: Arthur Isaev