EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Maven Enforcer Plugin

Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML
Home Software Development Software Development Tutorials Maven Tutorial Maven Enforcer Plugin

Maven Enforcer Plugin

Introduction to Maven Enforcer Plugin

Maven enforcer plugin provides goals to control the constraint of certain environments. The version of Jdk and the family of os contains the user-created rules and built-in rules. It contains two goals, the first is enforcer:enforce is executing the rules for every project which was built in a multi-project. The second goal is the enforcer:display-info which was used to display the current information.

The Enforcer plugin goals of enforcer display info will detect the information from the built-in rules. The goal is bound to the lifecycle phase and it will configure in the pom.xml. The enforcer plugin will execute the rules which were configured and those rules were checked in the constraints.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The goal of maven enforcer plugin will support the following three options:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)
  • Skip: Skip option is a quick way which was used to skip checks by using the denforcer skip from the command line or by user profile.
  • Fall: Suppose the goal is to fall in the build when the rule falls. Suppose this value is false then the error is logged as a warning.
  • failFast: Suppose goal is stopped checking at the time of the first failure then the default value of this option is false.

Every rule in it will be executed and added in the rules element with the configuration which was specific for the enforcer plugin that rule. Before the version of 1.4 we need to add the level element in the specified rules. The valid values of maven are warned and error. The warn and level is specified in the rule which was spitting out a warning and which was not fail in the build.

Key Takeaways

  • The maven enforcer plugin will control the constraint which was built on the environment. This is nothing but a java version, maven version, or system parameter which was defined in user rules.
  • It is defining the two types of rules i.e. enforce and display info.

How to Use Maven Enforcer Plugin?

To use the maven enforce plugin we need to create the project in the spring tool suite. Here we are creating the project template name as maven_enforcer_plugin as follows:

1. In this step we are creating the project template name as maven_enforcer_plugin by using spring initializer. In the below example, we are defining the name maven_enforcer_plugin.

Also, we are defining the group name as com.example, we are defining the artifact as maven_enforcer_plugin, and the packaging as jar and java version as 8.

Group name – com.example Artifact – maven_enforcer_plugin
Name – maven_enforcer_plugin Packaging – jar
Java version – 8

Maven Enforcer Plugin 1

2. In the above example we can see that we have created a template. Now in this step, we are extracting the template file and after extracting the same we are opening it by using the spring tool suite.

Maven Enforcer Plugin 2

3. After extracting and opening the project in the spring tool suite now in this step we are checking the project structure and pom.xml file as follows.

project structure and pom.xml

4. After opening the project template we are adding the code of maven enforcer plugin.

Code:

<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-enforcer-plugin </artifactId>
<executions>
<execution>
<id> enforce </id>
<goals>
<goal> enforce </goal>
</goals>
</execution>
</executions>
</plugin>

Output:

Maven Enforcer Plugin 4

5. After adding the enforcer plugin in the pom.xml file now we are running the project of maven enforcer plugin by using the mvn install.

Maven Enforcer Plugin 5

Maven Enforcer Plugin 6

Maven Enforcer Plugin Configuration

We are configuring the plugin in the pom.xml file. We need to add it to the pom.xml file. In the below example we are configuring the maven enforcer plugin as follows:

Code:

<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-enforcer-plugin </artifactId>
<executions>
<execution>
<id> enforce </id>
<goals>
<goal> maven-enforce </goal>
</goals>
</execution>
</executions>
</plugin>

Output:

Maven Enforcer Plugin 7

After adding the code in the pom.xml file, not in this step we are running the maven install command by using the maven install command as follows.

Maven Enforcer Plugin 8

Maven Enforcer Plugin 9

In the below example we are using the command line tool for executing the command of mvn install. We can see that we are executing the same code by using the command line as follows. Command line and GUI will work the same while executing the code.

command line tool

Maven Enforcer Plugin 11

Maven Enforcer Rules

The enforce keyword in maven plugin is giving subtle suggestions to the existing rules which tell us how the maven enforcer will work. We can configure the rules of the maven enforcer plugin at the time of the build phase of the project.

Below is the rule as follows:

1. Ban duplicate dependency

In a multi-module project at the time, child relationship will exist as per POM ensuring duplicate dependency in the POM project which was effective.

Code:

<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-enforcer-plugin </artifactId>
<executions>
<execution>
<id> enforce </id>
<goals>
<goal> maven-enforce </goal>
</goals>
<rules>
<banDuplicatePomDependencyVersions/>
</rules>
</execution>
</executions>
</plugin>

Output:

Ban duplicate dependency

2. Require maven and java version

This rule is enabling the project wide lock. It will help to eliminate the disparity.

Code:

<requireMavenVersion>
<version>3.0</version>
</requireMavenVersion>
<requireJavaVersion>
<version>1.8</version>
</requireJavaVersion>

Output:

Maven Enforcer Plugin 13

3. Require environment variables

By using this rule we can ensure that the specified environment variables will be set in the execution environment as follows.

Code:

<requireEnvironmentVariable>
<variableName> maven-ui </variableName>
</requireEnvironmentVariable>
<requireEnvironmentVariable>
<variableName> enforce </variableName>
</requireEnvironmentVariable>

Output:

Require environment variables

4. Require active profile

The profile in the project of maven will help us to configure the properties as follows.

Code:

<requireActiveProfile>
<profiles> local,base </profiles>
<message> Missing </message>
</requireActiveProfile>

Output:

Maven Enforcer Plugin 15

5. Other and custom rule

It also contains other rules which were used in the plugin.

Code:

mvn enforcer:display-info

Output:

Other custom rules

Maven Enforcer Plugin 18

FAQ

Given below are the FAQs mentioned:

Q1. What is the use of maven enforcer plugin in maven project?

Answer: Basically, this plugin is used to control to provide goals with certain constraints on maven and java versions.

Q2. How many types of rules are defined?

Answer: There are two types of rules used in the maven enforcer login i.e. display in and enforce. Both rules are important while using it.

Q3. Which files do we need to use for adding maven enforcer plugin in maven project?

Answer: We need to use the pom.xml file for adding the maven enforcer plugin in the project of maven.

Conclusion

The maven enforcer plugin goal is bound to the lifecycle phase and it will configure in the pom.xml. It provides goals to control the constraint of certain environments, the version of Jdk and family of OS contains the user-created rules and built-in rules.

Recommended Articles

This is a guide to Maven Enforcer Plugin. Here we discuss the introduction, and how to use maven enforcer plugin. configuration, rules, and FAQ respectively. You may also have a look at the following articles to learn more –

  1. Maven Build Command
  2. Maven WAR Plugin
  3. Maven Profile
  4. Maven Versions
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Maven Training (4 Courses, 6 Projects) Learn More