Sub topic 16
Multi-Module Maven and Multi-Project Gradle Builds
As a project grows, putting all its code in one folder gets messy. Both build tools let you split the code into smaller parts that build together. Maven calls each part a module. Gradle calls each part a subproject. They mean the same thing: a folder with its own build file and its own src/main/java.
Why Split a Project?
Picture an online shop. The business rules, such as prices and orders, live in a core part. The program that starts the shop lives in an app part. Splitting them like this has these benefits:
- Each part stays small and easy to understand.
corecan be reused by other programs later.- The build tool works out the right build order for you.
Multi-Module Maven
A parent POM is a pom.xml in the root folder that lists the modules. Its packaging is pom, because it produces no JAR of its own.
<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>
<modules>
<module>core</module>
<module>app</module>
</modules>
</project>Each module folder has its own pom.xml that points back to the parent. Here is app/pom.xml. It uses core's coordinates to depend on it:
<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>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>The module inherits its groupId and version from the parent. When you run mvn package in the root folder, Maven builds core first and app second. The reactor is the part of Maven that works out this order.
Multi-Project Gradle
In Gradle, the settings.gradle.kts file in the root lists the subprojects. Each subproject then gets its own build.gradle.kts.
// settings.gradle.kts
rootProject.name = "shop"
include("core", "app")
// app/build.gradle.kts
plugins {
application
}
dependencies {
implementation(project(":core"))
}project(":core") means "the subproject named core in this build." You don't need a version number, because Gradle builds it from source. Run ./gradlew build in the root to build every subproject in the right order.
Comparing the Two
| Idea | Maven | Gradle |
|---|---|---|
| A part is called | module | subproject |
| Parts are listed in | parent `pom.xml` | `settings.gradle.kts` |
| Depend on another part | its coordinates | `project(":name")` |
Exercise
You add a third part named web that uses code from core. What two changes do you make in a Gradle build?
Show answer
Add "web" to the include(...) line in settings.gradle.kts. Then, in web/build.gradle.kts, add implementation(project(":core")) to the dependencies block.
Exercise
Why does a Maven parent POM use <packaging>pom</packaging>?
Show answer
The parent only groups the modules and shares settings with them. It has no code of its own, so it produces no JAR.