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.
The goal of maven enforcer plugin will support the following three options:
- 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.
- 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
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.
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.
4. After opening the project template we are adding the code of maven enforcer plugin.
<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>
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 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:
<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>
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.
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.
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.
<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>
2. Require maven and java version
This rule is enabling the project wide lock. It will help to eliminate the disparity.
<requireMavenVersion> <version>3.0</version> </requireMavenVersion> <requireJavaVersion> <version>1.8</version> </requireJavaVersion>
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.
<requireEnvironmentVariable> <variableName> maven-ui </variableName> </requireEnvironmentVariable> <requireEnvironmentVariable> <variableName> enforce </variableName> </requireEnvironmentVariable>
4. Require active profile
The profile in the project of maven will help us to configure the properties as follows.
<requireActiveProfile> <profiles> local,base </profiles> <message> Missing </message> </requireActiveProfile>
5. Other and custom rule
It also contains other rules which were used in the plugin.
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.
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.
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 –