← Back to Advanced Java Course | Chapter 12: Build Tools | Lesson 5 of 5

Java Multi-Module Projects

A multi-module project groups related sub-projects, like an API, service, and web layer, under one parent so they share configuration and can be built together as one unit.

What is a Multi-Module Project?

A multi-module project groups several related sub-projects, such as an API layer, a service layer, and a web layer, under a single parent project, letting them share configuration and be built together as one unit.

Example: What is a Multi-Module Project?

java
public class Main {
	public static void main(String[] args) {
		String[] modules = {"api", "service", "web"}; // share config, built together
		for (String m : modules) System.out.println(m);
	}
}

Parent POM Structure

In Maven, the parent module has packaging type pom rather than jar, since it exists only to declare shared properties, dependency versions, and plugin configuration that every child module inherits.

Example: Parent POM Structure

java
public class Main {
	public static void main(String[] args) {
		String parentPom = "<packaging>pom</packaging>"; // declares shared config only, not a jar itself
		System.out.println(parentPom);
	}
}

Module Dependencies

Modules within the same multi-module project can depend on each other exactly like external libraries, using the same groupId, artifactId, and version coordinates, letting the build system track the correct build order automatically.

Example: Module Dependencies

java
public class Main {
	public static void main(String[] args) {
		String moduleDependency = "<groupId>com.example</groupId>\n<artifactId>api</artifactId>\n<version>1.0</version>";
		System.out.println(moduleDependency); // same coordinates as an external library
	}
}

Building All Modules Together

Running a build command from the parent project's root builds every module in the correct dependency order, while flags like -pl and -am let you target a specific module and its dependencies without rebuilding the entire project.

Example: Building All Modules Together

java
public class Main {
	public static void main(String[] args) {
		String buildAll = "mvn install"; // builds every module in dependency order
		String buildOne = "mvn install -pl service -am"; // targets one module plus its dependencies
		System.out.println(buildAll + " / " + buildOne);
	}
}

Gradle Multi-Project Builds

Gradle multi-project builds are configured through a settings.gradle file that lists every sub-project, and one sub-project can depend on another using Gradle's project() function instead of full external dependency coordinates.

Example: Gradle Multi-Project Builds

java
public class Main {
	public static void main(String[] args) {
		String settingsGradle = "include 'api', 'service', 'web'";
		String dependency = "implementation project(':api')"; // internal, not external coordinates
		System.out.println(settingsGradle + " / " + dependency);
	}
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.