Maven Enforcer Plugin Example

By Arvind Rai, May 15, 2021
Maven Enforcer plugin enforces to follow the certain environment constraints such as Maven version, JDK version and OS family etc. We can use enforcer plugin to follow provided built-in rules as well as user created rules.
The Enforcer plugin has two goals.
enforcer:enforce: This goal executes rules for each project in a multi-project build.
enforcer:display-info: This goal displays the current information as detected by the built-in rules.

We can resolve Maven Enforcer plugin as following.
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M3</version>
</plugin> 
The built-in rules that can be enforced by Enforcer plugin are 'alwaysFail', 'alwaysPass', 'bannedPlugins', 'bannedRepositories', 'requireActiveProfile', 'requireEnvironmentVariable', 'requireFilesExist', 'requireFilesSize', 'requireJavaVersion', 'requireMavenVersion', 'requireOS' etc.

enforcer:enforce Goal

The enforcer:enforce goal executes rules to follow the constraints. In multi-project build, enforcer:enforce goal executes rules for each project. Find a sample Maven code to enforce specified maven version, Java version and OS.
<build>
	<plugins>
	  <plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-enforcer-plugin</artifactId>
		<version>3.0.0-M3</version>
		<executions>
		  <execution>
			<id>enforce-versions</id>
			<goals>
			  <goal>enforce</goal>
			</goals>
			<configuration>
			  <rules>
				<requireMavenVersion>
				  <version>3.1.1</version>
				</requireMavenVersion>
				<requireJavaVersion>
				  <version>1.5</version>
				</requireJavaVersion>
				<requireOS>
				  <family>unix</family>
				</requireOS>
			  </rules>
			</configuration>
		  </execution>
		</executions>
	  </plugin>
	</plugins>
</build> 
If the rules configured by enforcer are not passed, the build will fail.
Maven Enforcer Plugin Example

enforcer:display-info Goal

The enforcer:display-info is used to display current information as detected by standard rules. The information displayed by enforcer:display-info may not match data configured enforcer rules.
Run enforcer:display-info command as following.
mvn enforcer:display-info 
The result can be as following.
------
[INFO] Maven Version: 3.8.1
[INFO] JDK Version: 16 normalized as: 16
[INFO] OS Info: Arch: amd64 Family: windows Name: windows 7 Version: 6.1 

skip, fail and failFast Options

The enforcer:enforce supports three options skip, fail and failFast.
1. skip :
The skip can be used to skip the enforcer. Default value is false.
mvn clean install -Denforcer.skip=true
mvn clean package -Denforcer.skip=true 
2. fail :
The fail can be used to avoid the build fail when an enforcer rule fails. The default value is true. If we assign false value to fail, the build will not fail when an enforcer rule fails.
mvn clean install -Denforcer.fail=false
mvn clean package -Denforcer.fail=false 
3. failFast : The failFast can be used to stop checking rules after the first rule failure by enforcer goal. The default value is false.
mvn clean install -Denforcer.failFast=true
mvn clean package -Denforcer.failFast=true 

Built-in Rules

There are many built-in rules shipped along with enforcer plugin. Find some of them.
requireJavaVersion : This rule enforces the specified Java version for build.
<requireJavaVersion>
  <version>1.5</version>
</requireJavaVersion> 
requireMavenVersion : This rule enforces the specified Maven version for build.
<requireMavenVersion>
  <version>3.1.1</version>
</requireMavenVersion> 
requireOS : This rule enforces the specified OS / CPU Architecture. The parameters supported by requireOS are 'message', 'arch', 'family', 'name', 'version' and 'display'. OS values can be dos, mac, unix, windows, win9x etc.
<requireOS>
  <name>Windows XP</name>
  <family>windows</family>
  <arch>x86</arch>
  <version>5.1.2600</version>
</requireOS> 
requireFilesSize : This rule checks the specified file to be present within specified max and min size. The parameters are 'message', 'files', 'maxsize', 'minsize', 'allowNulls'.
<requireFilesSize>
  <maxsize>5000</maxsize>
  <minsize>1000</minsize>
  <files>
   <file>${project.build.outputDirectory}/xyz1.txt</file>
   <file>${project.build.outputDirectory}/xyz2.txt</file>
  </files>
</requireFilesSize> 

Reference

Maven Enforcer Plugin
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us