Enforcer Rules
The previous lessons found problems by looking: at the tree, at -Dverbose, at dependency:analyze. Looking doesn't scale to fifty modules and twenty developers. The enforcer plugin turns expectations into rules that fail the build: "this needs Java 21", "no two versions of one library", "nobody uses commons-logging".
Environment rules
The enforcer's enforce goal binds to validate by default, the very first phase, so a misconfigured machine fails in a second instead of after a ten-minute build. Here the POM demands Java 25, on a Java 21 machine:
mkdir -p env && cd env
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>env</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.6.3</version>
<executions>
<execution>
<id>enforce-environment</id>
<goals><goal>enforce</goal></goals>
<configuration>
<rules>
<requireMavenVersion><version>[3.9,)</version></requireMavenVersion>
<requireJavaVersion><version>[25,)</version></requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
POM
mvn package | grep -A1 -E -- '--- enforcer|Rule' | grep -v -E '^--$|-----' | cut -c1-150
sed -i 's/\[25,)/[21,)/' pom.xml
mvn package | grep -E -- '--- enforcer|Rule|BUILD'[INFO] --- enforcer:3.6.3:enforce (enforce-environment) @ env ---
[INFO] Rule 0: org.apache.maven.enforcer.rules.version.RequireMavenVersion passed
[ERROR] Rule 1: org.apache.maven.enforcer.rules.version.RequireJavaVersion failed with message:
[ERROR] Detected JDK /opt/java is version 21.0.13 which is not in the allowed range [25,).
[INFO] --- enforcer:3.6.3:enforce (enforce-environment) @ env ---
[INFO] Rule 0: org.apache.maven.enforcer.rules.version.RequireMavenVersion passed
[INFO] Rule 1: org.apache.maven.enforcer.rules.version.RequireJavaVersion passed
[INFO] BUILD SUCCESS
Versions use Maven's range syntax: [21,) means 21 or newer, [3.9,4) means at least 3.9 and below 4, a square bracket includes the bound and a round one excludes it.
Dependency rules
The rules that pay for themselves are about the graph:
| Rule | Fails when |
|---|---|
dependencyConvergence |
the tree contains two versions of the same artifact, whatever mediation picked |
requireUpperBoundDeps |
mediation picked a version lower than one some dependency asked for (the NoSuchMethodError case) |
bannedDependencies |
a banned artifact (e.g. commons-logging, log4j:log4j, any *-SNAPSHOT) is in the tree |
requireReleaseDeps |
a SNAPSHOT dependency is used (in a release build) |
banDuplicatePomDependencyVersions |
a POM declares the same dependency twice |
Here are the first two on the conflict from Version Mediation: alpha wants text-util 1.0, gamma (under beta) wants 2.0:
newlib() {
mkdir -p $1/src/main/java/lib
cat > $1/pom.xml <<POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId><artifactId>$2</artifactId><version>$3</version>
<properties><maven.compiler.release>21</maven.compiler.release><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
<dependencies>$4</dependencies>
$5
</project>
POM
}
dep() { echo "<dependency><groupId>org.acme</groupId><artifactId>$1</artifactId><version>$2</version></dependency>"; }
newlib text-util-1.0 text-util 1.0 ""
newlib text-util-2.0 text-util 2.0 ""
newlib alpha alpha 1.0 "$(dep text-util 1.0)"
newlib gamma gamma 1.0 "$(dep text-util 2.0)"
newlib beta beta 1.0 "$(dep gamma 1.0)"
for d in text-util-1.0 text-util-2.0 alpha gamma beta; do (cd $d && mvn -q install); done
mkdir app && cd app
newlib . app 1.0 "$(dep beta 1.0) $(dep alpha 1.0)" '<build><plugins><plugin>
<artifactId>maven-enforcer-plugin</artifactId><version>3.6.3</version>
<executions><execution><id>graph-rules</id><goals><goal>enforce</goal></goals>
<configuration><rules><dependencyConvergence/><requireUpperBoundDeps/></rules></configuration>
</execution></executions>
</plugin></plugins></build>'
mvn validate | sed -n '/Rule 0/,/^\[ERROR\] \]/p'[ERROR] Rule 0: org.apache.maven.enforcer.rules.dependency.DependencyConvergence failed with message:
[ERROR] Failed while enforcing releasability.
[ERROR]
[ERROR] Dependency convergence error for org.acme:text-util:jar:2.0. Paths to dependency are:
[ERROR] +-org.acme:app:jar:1.0
[ERROR] +-org.acme:beta:jar:1.0:compile
[ERROR] +-org.acme:gamma:jar:1.0:compile
[ERROR] +-org.acme:text-util:jar:2.0:compile
[ERROR] and
[ERROR] +-org.acme:app:jar:1.0
[ERROR] +-org.acme:alpha:jar:1.0:compile
[ERROR] +-org.acme:text-util:jar:1.0:compile
[ERROR]
[ERROR] Rule 1: org.apache.maven.enforcer.rules.dependency.RequireUpperBoundDeps failed with message:
[ERROR] Failed while enforcing RequireUpperBoundDeps. The error(s) are [
[ERROR] Require upper bound dependencies error for org.acme:text-util:1.0. Paths to dependency are:
[ERROR] +-org.acme:app:1.0
[ERROR] +-org.acme:alpha:1.0
[ERROR] +-org.acme:text-util:1.0
[ERROR] and
[ERROR] +-org.acme:app:1.0
[ERROR] +-org.acme:beta:1.0
[ERROR] +-org.acme:gamma:1.0
[ERROR] +-org.acme:text-util:2.0
[ERROR] ]
Both rules print every path to the offending artifact, which is exactly the information you need to decide on a fix (pin it in dependencyManagement, upgrade alpha, or exclude). dependencyConvergence is the stricter of the two: it fails even when mediation happened to pick the higher version. Large projects often start with requireUpperBoundDeps and tighten later.
Banning artifacts
<bannedDependencies>
<excludes>
<exclude>commons-logging:commons-logging</exclude> <!-- we route logging through SLF4J -->
<exclude>log4j:log4j</exclude> <!-- Log4j 1.x, end of life -->
<exclude>*:*:*:*:compile:*-SNAPSHOT</exclude> <!-- no snapshots at compile scope -->
</excludes>
</bannedDependencies>Patterns are groupId:artifactId:version:type:scope:classifier, with * wildcards and version ranges.
Where to put the rules
Put the enforcer execution in the parent POM (or your organisation's parent), so every module inherits it. Keep the list short and meaningful: every rule should correspond to an incident you never want again. The rules here run in well under a second, so there is no performance reason to skip them, and -Denforcer.skip exists for emergencies only.
Test yourself
Why does the enforcer bind to the validate phase by default?
- It needs compiled classes
- So bad environments and dependency graphs fail before any expensive work runs
- Because tests run there
- It only works offline
Mediation picked lib:1.0, but another dependency needs lib:2.0. Which rule catches exactly this?
- requireJavaVersion
- requireUpperBoundDeps
- banDuplicatePomDependencyVersions
- requireMavenVersion
What does the version range [3.9,4) mean?
- Exactly 3.9 or 4
- At least 3.9 and below 4
- Above 3.9 and up to 4 inclusive
- Any version