Maven

Apache Maven Enforcer Plugin Example

1. Apache Maven Enforcer Plugin – Introduction

Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

1.1 Maven installation guide

You just need to download the Maven’s zip file, unzip it to a folder, and configure the Windows environment variables.

1.2 JDK and JAVA_HOME

Make sure JDK is installed, and JAVA_HOME environment variable is configured. Follow the steps below to see Environment Variables:

  1. Right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables.
Apache Maven Enforcer - Configure JAVA_HOME
Configure JAVA_HOME variable

1.3 Download Apache Maven

Visit Maven official website, download the Maven zip file, for example apache-maven-3.6.1-bin.zip. Unzip it to a folder: C:\Program Files\apache-maven-3.6.1

Apache Maven Enforcer - apache-maven-3.6.1
apache-maven-3.6.1 location

That’s all, just download and unzip, installation is NOT required.

1.4 Add MAVEN_HOME system variable

Add a MAVEN_HOME system variables, and point it to the Maven folder.

Here are the steps to add a new variable:

Apache Maven Enforcer - System Properties
System Properties

Click New to add a new variable:

New System variable

Pay attention to the capital letters of the variable name, as it is case sensitive.

Variable value points to the location we extracted the Maven zip file.

Apache Maven Enforcer - Define variable name
Define variable name and its value

1.5 Add %MAVEN_HOME%\bin To PATH

In system variables, find PATH variable and follow the steps below:

Click Edit:

Apache Maven Enforcer - click on Edit button
Select “Path” variable and click on Edit button

Add %MAVEN_HOME%\bin to the end of the Variable value:

Apache Maven Enforcer - %MAVEN_HOME%\bin
Add ” %MAVEN_HOME%\bin ” to the end of “Path” variable value

1.6 Verification

Done, start a new command prompt, type mvn –version : Holddown the Windows Key and Press R on your keyboard.

Apache Maven Enforcer - Command Prompt
Run “Command Prompt” window

By clicking OK, cmd.exe runs. Now type the command below:

Apache Maven Enforcer - Maven installation
Verification of Maven installation

2. What is Maven Enforcer Plugin?

The Enforcer plugin helps maintain project standards and provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more standard(built-in) rules and user created rules.

General instructions on how to use the Enforcer Plugin can be found on the usage page.

3. What can the Enforcer plugin do for the project?

Generally we can use it to guarantee the level of compliance in our project, it is especially handy when we have distributed teams:

  • Enforce banned dependencies:
    • Dependencies that are undesirable by your organization can be banned and reported during the build so that other developers can not add them to the pom.
    • Some third party dependencies include transitive dependencies that may conflict with your project’s dependencies. For example: If your project uses Logback and SFL4j, it is a best practice to ensure no other logging frameworks are included in the build.
  • Reduce package size when packaging the application in a war or jar, by excluding banned dependencies.
  • Enforce a specific version of Java, OS, and Maven.

Now lets see how to use this plugin using an example:

4. Plugin Configuration and Goals

Maven Enforcer has two goals: 

  • enforcer:enforce goal: runs during a project build to execute rules specified in the configuration
  • enforcer:display-info goal: shows current information about the built-in rules that are present in the project’s pom.xml

5. Create a Maven project

Open IntelliJ Idea, choose Create New Project:

Apache Maven Enforcer - New Project
Create New Project

From the right sidebar select Maven:

Apache Maven Enforcer - Select Maven
Select Maven

In the next window enter GroupId and ArtifactId as you wish:

Apache Maven Enforcer - Fill the GroupId, ArtifactId
Fill the GroupId, ArtifactId and version of your maven project

By clicking Next the Project name and location is displayed. You can change the values as you wish.

Apache Maven Enforcer - Final-step
Final-step

After clicking the “Finish” button this window appears in the IDE:

Apache Maven Enforcer - Main window
Main window of the project

Let’s define the enforce goal in the executions tag. Furthermore, we’ll add the configuration tag that holds the rules definitions for the project:

executions tag:

...
<executions>
    <execution>
        <id>enforce</id>
        <goals>
            <goal>enforce</goal>
        </goals>
        <configuration>
            <rules>
                <requireMavenVersion>
                    <version>4.0</version>
                    <message>Invalid Maven version. It should, at 
                    least, be 4.0</message>
                </requireMavenVersion>
                <requireJavaVersion>
                    <version>1.9</version>
                    <message>Invalid Java version. It should, at 
                    least, be 1.9</message>
                </requireJavaVersion>
            </rules>
        </configuration>
    </execution>
</executions>
...

The keyword enforce shows there are rules to follow. In fact the rules are enforced during the build phase of the project.

The available built-in rules are described here. Each rule to be executed should be added to the rules element along with the specific configuration for that rule.

  • requireMavenVersion rule enforces certain Maven versions.
  • requireJavaVersion rule enforces certain Java JDK versions.
  • requireOs rule can enforce certain values about the Operating System and processor architecture.

All rules also accept a message parameter for specifying a custom message:

rules:

...
<requireMavenVersion>
    <version>4.0</version>
    <message>Invalid Maven version. It should, at least, be 4.0</message>
</requireMavenVersion>
...

To check the rule’s behavior, we run mvn clean compile in the Terminal window:

Apache Maven Enforcer - Compile command
Compile command

It’ll produce the following error lines on the console:

Apache Maven Enforcer - Compile result
Compile result

As you see we have warnings on Rule 1 and Rule 2 with our defined messages.

Now we add another rule related to OS version:

Require OS Version: This rule can enforce certain values about the Operating System and processor architecture. The values and code used to determine if an OS is allowed are exactly the same as the OS Profile activation in Maven.

rules:

...
<requireOS>
   <family>unix</family>
</requireOS>

To check the rule’s behavior, we run again mvn clean compile in the Terminal window. We can run first mvn clean then mvn compile

Apache Maven Enforcer - mvn clean
mvn clean command result

Now we run the install command:

Apache Maven Enforcer - mvn install
mvn install command result

As you see there is one rule added to WARNINGs saying that windows 7 is not allowed.

6. Download the Complete Source Code

This was a tutorial of Apache Maven Enforcer Plugin Example.

Download
You can download the full source code of this example here: Apache Maven Enforcer Plugin Example

Firouzeh hejazi

A self-motivated and hard-working developer with more than 4 years of extensive experience in developing software in java platforms. A multi-skilled and problem-solving engineer who has participated in implementing a range of applications in different roles. Possess a MSc in Knowledge Engineering and Decision Science.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button