Multi-project builds
A repository with three folders of Java code has one build, not three. The settings file lists the projects, a project depends on a project the way it depends on a jar, and one command builds all of them in the order the dependencies need.
Three projects in one build
settings.gradle
rootProject.name = 'shop'
include 'core', 'app', 'cli'
core/build.gradle
plugins {
id 'java-library'
}
core/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);
}
}
app/build.gradle
plugins {
id 'java'
id 'application'
}
dependencies {
implementation project(':core')
}
application {
mainClass = 'com.example.App'
}
app/src/main/java/com/example/Cart.java
package com.example;
public final class Cart {
public static String total(long cents) {
return Price.format(cents);
}
}
app/src/main/java/com/example/App.java
package com.example;
public final class App {
public static void main(String[] args) {
System.out.println("Total: " + Cart.total(1999));
}
}
cli/build.gradle
plugins {
id 'java'
}
dependencies {
implementation project(':app')
}
cli/src/main/java/com/example/Summary.java
package com.example;
public final class Summary {
public static String line() {
return "summary: " + Cart.total(1999);
}
}
project(':core') is a dependency with three consequences: the classes of core are on the compile class path of app, its runtime class path too, and the build of app needs the jar of core. cli depends on app, and it writes no program of its own here, so its jar holds one class.
gradle projects
gradle projects --console=plain --offline --no-daemon > build.log 2>&1; sed "s#$PWD#<project>#g" build.log
> Task :projects
Projects:
------------------------------------------------------------
Root project 'shop'
------------------------------------------------------------
Location: <project>
Project hierarchy:
Root project 'shop'
+--- Project ':app'
+--- Project ':cli'
\--- Project ':core'
Project locations:
project ':app' - /app
project ':cli' - /cli
project ':core' - /core
To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :app:tasks
BUILD SUCCESSFUL in 15s
1 actionable task: 1 executed
gradle build
The task lines are prefixed with the path of the project, and the order is the order of the dependencies: core first, then app, then cli.
gradle build --console=plain --offline --no-daemon
> Task :core:compileJava
> Task :app:compileJava
> Task :app:processResources NO-SOURCE
> Task :app:classes
> Task :app:jar
> Task :core:processResources NO-SOURCE
> Task :core:classes
> Task :core:jar
> Task :app:startScripts
> Task :app:distTar
> Task :app:distZip
> Task :app:assemble
> Task :app:compileTestJava NO-SOURCE
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses UP-TO-DATE
> Task :app:test NO-SOURCE
> Task :app:check UP-TO-DATE
> Task :app:build
> Task :cli:compileJava
> Task :cli:processResources NO-SOURCE
> Task :cli:classes
> Task :cli:jar
> Task :cli:assemble
> Task :cli:compileTestJava NO-SOURCE
> Task :cli:processTestResources NO-SOURCE
> Task :cli:testClasses UP-TO-DATE
> Task :cli:test NO-SOURCE
> Task :cli:check UP-TO-DATE
> Task :cli:build
> Task :core:assemble
> Task :core:compileTestJava NO-SOURCE
> Task :core:processTestResources NO-SOURCE
> Task :core:testClasses UP-TO-DATE
> Task :core:test NO-SOURCE
> Task :core:check UP-TO-DATE
> Task :core:build
BUILD SUCCESSFUL in 29s
9 actionable tasks: 9 executed
One project at a time
A task can be named with the path of its project. Running one project still runs what it depends on, so :app:run builds core first:
gradle :app:run --console=plain --offline --no-daemon
> Task :core:compileJava UP-TO-DATE
> Task :app:compileJava UP-TO-DATE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :core:processResources NO-SOURCE
> Task :core:classes UP-TO-DATE
> Task :core:jar UP-TO-DATE
> Task :app:run
Total: 19.99
BUILD SUCCESSFUL in 20s
4 actionable tasks: 1 executed, 3 up-to-date
Related
- buildSrc: shared build logic for these three projects.
- Convention plugins versus subprojects: why a root build script should not configure its children.
- The Maven reactor: three modules and a parent, in the other tool.
Article author: Arthur Isaev