Sub topic 16
Multi-Module Maven and Multi-Project Gradle Builds
So far, every project in this tutorial has been one folder that produces one JAR file. Real applications grow. After a while, one folder holds the business rules, the command-line program, and maybe a web server, all tangled together. This chapter shows how to split a project into smaller parts that one build still handles together.
Why Split a Project into Parts
A module is a smaller project inside a bigger one. It has its own source folders and its own build file, and it produces its own JAR. Maven calls the whole thing a multi-module build. Gradle calls it a multi-project build, and each part is a subproject. Both mean the same idea.
Splitting helps in a few ways:
- Clear boundaries. The
coremodule cannot use code fromappunless you allow it, so you can't create accidental tangles. - Reuse. Other programs can depend on the
coreJAR without pulling in the command-line code. - One command builds everything. You still type one command, and the tool builds the modules in the right order.
In this chapter we build a small shop with two modules:
coreholds the shop's logic, such as price calculation.appholds themainmethod, and it usescore.
The Folder Layout
Both tools use almost the same layout. Each module follows the standard src/main/java layout you already know.
shop/
├── pom.xml (Maven parent)
├── settings.gradle.kts (Gradle settings)
├── core/
│ ├── pom.xml
│ ├── build.gradle.kts
│ └── src/main/java/com/example/core/Prices.java
└── app/
├── pom.xml
├── build.gradle.kts
└── src/main/java/com/example/app/App.javaYou only need the Maven files or the Gradle files, not both. We show both so you can compare them.
The Java Code
Here is the class in core. It adds a 10 percent tax to a price given in cents.
package com.example.core;
public class Prices {
public static long withTax(long cents) {
return cents + cents / 10;
}
}Here is the class in app. It imports Prices from the other module.
package com.example.app;
import com.example.core.Prices;
public class App {
public static void main(String[] args) {
System.out.println("Total: " + Prices.withTax(1000) + " cents");
}
}The app code compiles only if the build knows that app needs core. Telling it that is the main job of the build files below.
Multi-Module Builds in Maven
The Parent POM
A parent POM is a pom.xml at the root that lists the modules. Its packaging is pom, which means it produces no JAR of its own. It only groups the modules and shares settings with them.
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>core</module>
<module>app</module>
</modules>
</project>Each <module> entry is a folder name, not an artifactId. The properties here are passed down to every module, so you set the Java version once.
The Child POMs
A child POM is a module's own pom.xml. It names its parent in a <parent> block, which lets it inherit the groupId, version, and properties. Here is core/pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>
</project>And app/pom.xml, which adds core as a normal dependency:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>app</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>${project.version} means "the same version as this project." Because the version comes from the parent, the two modules always stay in step.
Building with the Reactor
Run Maven from the shop folder:
mvn package
java -cp app/target/app-1.0-SNAPSHOT.jar:core/target/core-1.0-SNAPSHOT.jar com.example.app.AppOn Windows, use ; instead of : between the two JAR paths. The program prints Total: 1100 cents.
Maven's reactor is the part that reads all the modules, works out which depends on which, and sorts them. It builds core first because app needs it, even though nothing in the <modules> list says so. At the end, Maven prints a "Reactor Summary" with one line per module.
You can also build just part of the project:
mvn -pl app -am package-pl app picks the app module. -am means "also make" the modules it needs, so core is built too.
Multi-Project Builds in Gradle
The Settings File
Gradle doesn't use a parent build file to list the parts. The settings file, settings.gradle.kts, is the file at the root that names the build and lists its subprojects.
// settings.gradle.kts
rootProject.name = "shop"
include("core", "app")Each name in include matches a folder.
The Subproject Build Scripts
core/build.gradle.kts uses the java-library plugin. This plugin is for code that other projects will use.
plugins {
`java-library`
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}app/build.gradle.kts uses the application plugin and depends on core:
plugins {
application
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
dependencies {
implementation(project(":core"))
}
application {
mainClass = "com.example.app.App"
}project(":core") points to another subproject in the same build. The colon starts a project path, which is how Gradle names subprojects. Neither file needs a repositories block, because neither module downloads anything from outside.
Running Tasks Across Projects
From the shop folder:
./gradlew build
./gradlew :app:run
./gradlew :core:build./gradlew buildruns thebuildtask in every subproject../gradlew :app:runruns only theruntask ofapp. Gradle adds thecoretasks it needs to the task graph by itself../gradlew :core:buildbuilds onlycore.
Unlike Maven, Gradle doesn't need an install step. It always uses the fresh output of :core directly.
Maven and Gradle Side by Side
| Idea | Maven | Gradle |
|---|---|---|
| Where modules are listed | `<modules>` in the parent `pom.xml` | `include(...)` in `settings.gradle.kts` |
| Shared settings | Inherited from the `<parent>` | Repeated, or put in a shared build plugin |
| Depend on another module | `<dependency>` with its coordinates | `implementation(project(":core"))` |
| Build one module and its needs | `mvn -pl app -am package` | `./gradlew :app:build` |
| Part that orders the work | The reactor | The task graph |
Common Mistakes
- Typo in a module name. Maven reports that a child module does not exist, and Gradle treats an unknown name as a new, empty project. Check that folder names match.
- Circular dependencies. If
coredepends onappandappdepends oncore, both tools stop with an error about a cycle. Move the shared code into a third module. - Forgetting
<packaging>pom</packaging>in the parent. Maven then expects the parent to have Java code and refuses to accept modules.
Exercise
You add a third Maven module called web that also uses core. Which two files must you change, and what goes in each?
Show answer
In the parent pom.xml, add <module>web</module> inside <modules>. In web/pom.xml, add a <parent> block pointing to shop-parent and a <dependency> on com.example:core with version ${project.version}. You must also create the new web/pom.xml itself.
Exercise
In the Gradle build, what does ./gradlew :app:run do with the core subproject, and why?
Show answer
It compiles and packages core first. The app script declares implementation(project(":core")), so Gradle puts the core tasks into the task graph ahead of app's compile and run tasks.
Exercise
A teammate runs mvn package inside the app folder on a fresh machine and gets an error that com.example:core:1.0-SNAPSHOT cannot be found. Give two ways to fix it.
Show answer
Run mvn -pl app -am package from the root folder so the reactor builds core too. Or run mvn install from the root once, which puts core into the local repository so building app alone works.