Maven Enforcer Plugin Example
May 15, 2021
Maven Enforcer plugin enforces to follow the certain environment constraints such as Maven version, JDK version and OS family etc. We can use enforcer plugin to follow provided built-in rules as well as user created rules.
The Enforcer plugin has two goals.
enforcer:enforce: This goal executes rules for each project in a multi-project build.
enforcer:display-info: This goal displays the current information as detected by the built-in rules.
We can resolve Maven Enforcer plugin as following.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>3.0.0-M3</version> </plugin>
Contents
enforcer:enforce
Goal
The enforcer:enforce
goal executes rules to follow the constraints. In multi-project build, enforcer:enforce
goal executes rules for each project. Find a sample Maven code to enforce specified maven version, Java version and OS.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>3.0.0-M3</version> <executions> <execution> <id>enforce-versions</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireMavenVersion> <version>3.1.1</version> </requireMavenVersion> <requireJavaVersion> <version>1.5</version> </requireJavaVersion> <requireOS> <family>unix</family> </requireOS> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build>

enforcer:display-info
Goal
The enforcer:display-info
is used to display current information as detected by standard rules. The information displayed by enforcer:display-info
may not match data configured enforcer rules.
Run
enforcer:display-info
command as following.
mvn enforcer:display-info
------ [INFO] Maven Version: 3.8.1 [INFO] JDK Version: 16 normalized as: 16 [INFO] OS Info: Arch: amd64 Family: windows Name: windows 7 Version: 6.1
skip
, fail
and failFast
Options
The enforcer:enforce
supports three options skip
, fail
and failFast
.
1.
skip
:
The
skip
can be used to skip the enforcer. Default value is false.
mvn clean install -Denforcer.skip=true mvn clean package -Denforcer.skip=true
fail
:
The
fail
can be used to avoid the build fail when an enforcer rule fails. The default value is true. If we assign false value to fail
, the build will not fail when an enforcer rule fails.
mvn clean install -Denforcer.fail=false mvn clean package -Denforcer.fail=false
failFast
:
The failFast
can be used to stop checking rules after the first rule failure by enforcer goal. The default value is false.
mvn clean install -Denforcer.failFast=true mvn clean package -Denforcer.failFast=true
Built-in Rules
There are many built-in rules shipped along with enforcer plugin. Find some of them.requireJavaVersion : This rule enforces the specified Java version for build.
<requireJavaVersion> <version>1.5</version> </requireJavaVersion>
<requireMavenVersion> <version>3.1.1</version> </requireMavenVersion>
requireOS
are 'message', 'arch', 'family', 'name', 'version' and 'display'. OS values can be dos, mac, unix, windows, win9x etc.
<requireOS> <name>Windows XP</name> <family>windows</family> <arch>x86</arch> <version>5.1.2600</version> </requireOS>
<requireFilesSize> <maxsize>5000</maxsize> <minsize>1000</minsize> <files> <file>${project.build.outputDirectory}/xyz1.txt</file> <file>${project.build.outputDirectory}/xyz2.txt</file> </files> </requireFilesSize>