Introduction
Java projects can be managed using Maven i.e. Maven is a project management tool. By using Maven you can build, manage and configure Java projects easily.
Maven has a centralized repository that stores the dependencies of various software, so you can download the dependencies by visiting mvnreposiroty. It helps in getting the correct libraries and JAR files for your project. Maven will help you create the right project structure for the proper execution of files.
It is based on the concept of a Project Object Model (POM), which is an XML file holding all the information about the configuration details, plugin details and goals of a project.
There are many Plugins in Maven, one of them is the Enforcer Plugin which is used to apply and enforce rules on your Maven projects.

In this article, we will discuss what is an Enforcer Plugin and how to use it in Maven.
Enforcer Plugin
In Maven, a Plugin is a reusable piece of code using which you can apply the same build logic on different platforms without writing the code again and again.
According to the Cambridge Dictionary, Enforcer means “someone who makes people obey a law or rule, or makes a particular situation happen or be accepted”, so intuitively we can say that the Enforcer plugin will also check that the project holds the required configurations.
Enforcer Plugin helps in controlling the environment configurations. You can mention the required Maven versions, JDK versions, and OS Family rules. You can also create your own rules.
Before moving on, let’s understand some basic concepts of Maven -
▶️ In maven, there are three build phases - default, clean, and site.
▶️ Each lifecycle has phases that are responsible for performing many tasks.
▶️ Each Phase is made up of a sequence of Goals.
▶️ A Goal in Maven is specific task that will be performed.
▶️ A group of Goals is called a Plugin.
There are two Goals in the Enforcer Plugin -
Goal Name |
Description |
enforcer:enforce |
It will execute the rules that you have mentioned. |
enforcer:display-info |
It will display the rules that you have applied to your project. It will display the current information that is detected by the built-in rules. |
Let’s understand each goal in detail!
enforcer:enforce
This ‘goal’ of the Enforcer plugin, will go over all the rules and check for certain constraints i.e. it will execute rules specified in the configuration.
Some built-in Rules of the enforcer:enforce goal are as follows -
Rule |
Description |
banDuplicatePomDependencyVersions |
This rule ensures that there are no duplicate dependencies. This rule comes in handy when there exists a parent-child relationship in the POM XML file. |
requireJavaVersion |
By applying this rule, you can ensure the java version. |
requireMavenVersion |
By applying this rule, you can ensure the Maven version. |
requirePrerequisite |
This rule ensures that the mentioned prerequisites have been mentioned in the project. |
requireEnvironmentVariable |
This rule ensures that the required environment variables have been set. |
requireSameVersions |
This rule ensures that the specified dependencies and plugins have same versions. |
alwaysFail |
This rule will always fail and is used to test proper plugin configurations. |
alwaysPass |
This rule will always pass and is used to test proper plugin configurations. |
requireFilesSize |
It ensures that the mentioned files are present and the size of the file is as required. |
This goal supports three Options -
skip: use this option if you want to skip any kind of rule. You can do this by using -Denforcer.skip
fail: use this option, if you want the build to fail if a rule fails. By default, the build will fail if the rule fails but if you want to just show the failed build as warning then set the fail to False.
failFast: if the goal should stop checking after the first failure. The default is false.
Example Plugin Configurations
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedPlugins>
<!-- will only display a warning but does not fail the build. -->
<level>WARN</level>
<excludes>
<exclude>org.apache.maven.plugins:maven-verifier-plugin</exclude>
</excludes>
<message>Please consider using the maven-invoker-plugin (http://maven.apache.org/plugins/maven-invoker-plugin/)!</message>
</bannedPlugins>
<requireMavenVersion>
<version>2.0.6</version>
</requireMavenVersion>
<requireJavaVersion>
<version>1.5</version>
</requireJavaVersion>
<requireOS>
<family>unix</family>
</requireOS>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
enforcer:display-info
As the name suggests, this rule is used to determine the current configurations of the project.
mvn enforcer:display-info
...
[enforcer:display-info]
Maven Version: 2.0.6
JDK Version: 1.5.0_11 normalized as: 1.5.0-11
OS Info: Arch: x86 Family: windows Name: windows xp Version: 5.1