Why Maven 4’s New Features Are a Game‑Changer for Java Projects
Maven 4 introduces a revamped POM model, native flattening, explicit artifact types, subproject support, a tree‑based lifecycle for parallel builds, and richer configuration options, making it a timely upgrade for modern Java development pipelines.
POM Model Upgrade: 4.0.0 → 4.1.0
Maven 4 introduces POM model version 4.1.0. The modelVersion element can be omitted because Maven derives it from the XML schema.
<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
<modelVersion>4.1.0</modelVersion>
</project>Backward compatible: Maven 4 can still build projects that use the older 4.0.0 POM.
New capabilities: Certain enhancements are activated only when the model version is 4.1.0.
The modelVersion element may be omitted; Maven infers it from the schema.
Build POM / Consumer POM Separation (Flattening)
Maven 4 separates the POM used for the project's own build (Build POM) from the POM published for downstream consumers (Consumer POM). The Consumer POM contains only the real transitive dependencies, with all plugin configuration, parent references, unused dependencies, and unresolved properties removed.
Does not contain plugin configuration.
Does not contain a parent POM.
Does not contain unused dependencies.
Retains only true transitive dependencies.
All properties are resolved to concrete values.
Enable native flattening without an extra plugin:
mvn clean install -Dmaven.consumer.pom.flatten=trueNew Artifact Types: Explicit Classpath / Module‑Path Control
Maven 4 adds explicit artifact types to distinguish where a dependency should be placed.
<type>classpath-jar</type>
<type>module-jar</type> classpath-jar– placed on the classpath. module-jar – placed on the module path.
Additional processor types separate annotation‑processor classpaths from the regular API classpath. Example with Lombok:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<type>classpath-processor</type>
<scope>provided</scope>
</dependency>
</dependencies>Modules Renamed to Subprojects (Java 9 Compatibility)
The ambiguous <modules> element is deprecated and replaced by <subprojects>. This improves clarity and adds several conveniences:
Parent inference when <parent /> is empty.
Automatic discovery of subprojects without explicit listing.
Unified build timestamps.
Safe release: if any subproject fails, the whole release is aborted.
<subprojects>
<subproject>project-a</subproject>
<subproject>project-b</subproject>
</subprojects>Tree‑Based Lifecycle: Parallel Builds
Unlike Maven 3’s linear lifecycle, Maven 4 uses a tree‑based lifecycle where each subproject progresses independently as soon as its dependencies are ready, enabling efficient parallel builds for large multi‑module projects.
mvn -b concurrent verifySmall Configuration Enhancements
1. Conditional Profile Expressions
Profiles can now be activated with full expressions instead of simple OS or JDK checks.
<condition>exists('${project.basedir}/src/**/*.xsd') && length(${user.name}) > 5</condition>2. Unified Sources Model
Maven 4 consolidates sourceDirectory and testSourceDirectory into a single <sources> element, supporting multiple directories, scopes, and versions.
<sources>
<source>
<scope>main</scope>
<directory>my-custom-dir/foo</directory>
</source>
<source>
<scope>test</scope>
<directory>my-custom-dir/bar</directory>
</source>
</sources>Official Upgrade Tool (mvnup)
Maven 4 ships with mvnup, which analyses the existing POM, plugins, and project structure and generates executable upgrade suggestions.
mvnup check # generate report only
mvnup apply # apply automatic fixesmacrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
