Table of contents
1.
Introduction
2.
Enforcer Plugin
2.1.
enforcer:enforce
2.1.1.
Example Plugin Configurations
2.2.
enforcer:display-info
3.
Frequently Asked Questions
3.1.
How can we fix require upper bound dependencies error in Maven? 
3.2.
What is the difference between phase and goal in Maven?
3.3.
What is a Plugin in Maven? 
4.
Conclusion
Last Updated: Mar 27, 2024

Maven Enforcer Plugin

Author Neha Chauhan
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Maven Enforcer Plugin

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 defaultclean, 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

Frequently Asked Questions

How can we fix require upper bound dependencies error in Maven? 

Update the version in the pom. xml to the newest version listed in the output from maven-enforcer-plugin, in case that artifact is already declared in the pom.xml. You can apply the requireUpperBoundDeps to ensure that the dependencies are in correct inheritance. 

What is the difference between phase and goal in Maven?

A Phase is a collection of goals that perform a task. When we run a phase, several different goals that are included in the phase run in a pre-defined sequence. A Goal is specific task that needs to be performed, it is much finer task than a Phase. For example, in compiler:compile - compile is the Goal for the compiler phase of the compiler plugin. 

What is a Plugin in Maven? 

A Plugin is the heart of Maven. It is a reusable piece of code that can be used to reuse the common build logic across different platforms. A Plugin is a group of different goals. These goals might or might not be related to each other. 

Conclusion

In this article, we have discussed what is the Enforcer Plugin in Maven. We discussed about the two goals provided by the Enforcer Plugin - enforcer:enforce and enforcer:display-info in details along with some rules and options supported by the enforcer:enforce goal of the enforcer plugin. 

Do not stop learning! We recommend you read these articles- 

🔥 Maven Eclipse Plugin

🔥 Maven Plugins

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass