How to enforce operating system and minimum java version using maven enforcer plugin

Many times, we need to enforce that java build process should halt immediately if deployment environment does not have not a specific operating system, or it does not contain a minimum required java version. If you are using maven for build process, then these limitations can be easily be configured using maven enforcer plugin.

The enforcer plugin provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more standard rules and user created rules.

Lets see how to use this plugin using an example:

1) Create a sample project in workspace.

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=EnforceJavaVersionDemo
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2) Update the pom.xml file with following dependency


<build>
	<plugins>
		<plugin>
		<groupid>org.apache.maven.plugins</groupid>
		<artifactid>maven-enforcer-plugin</artifactid>
		<version>1.2</version>
		<executions>
			<execution>
			<id>enforce-versions</id>
			<goals>
				<goal>enforce</goal>
			</goals>
			<configuration>
				<rules>
					<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>

Here, required dependencies are operating system is linux, maven version is 2.0.6, and java version is 1.5. I am running this code on my windows machine so above pom.xml file should compain for operating system. Lets see.

3) Compile the project

mvn compile

Compilation fails with following error.

[INFO] --- maven-enforcer-plugin:1.2:enforce (enforce-versions) @ EnforceJavaVer
sionDemo ---
[WARNING] Rule 2: org.apache.maven.plugins.enforcer.RequireOS failed with messag
e:
OS Arch: amd64 Family: windows Name: windows 7 Version: 6.1 is not allowed by Fa
mily=unix
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.000s
[INFO] Finished at: Tue Jan 08 11:03:01 IST 2013
[INFO] Final Memory: 7M/132M

Similarly, If I update the maven minimum requirement with 3.0.6 and I have maven version 3.0.4, then it complains like this:

[INFO] --- maven-enforcer-plugin:1.2:enforce (enforce-versions) @ EnforceJavaVer
sionDemo ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireMavenVersion failed w
ith message:
Detected Maven Version: 3.0.4 is not in the allowed range 3.0.6.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.000s
[INFO] Finished at: Tue Jan 08 11:03:01 IST 2013
[INFO] Final Memory: 7M/132M

The same you can test for minimum java versions also.

Happy Learning !!

Reference: http://maven.apache.org/enforcer/maven-enforcer-plugin/usage.html

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Leave a Reply

Your email address will not be published. Required fields are marked *


eight − = 2

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>