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

Spread the words...Share on Google+4Share on Facebook1Tweet about this on Twitter3Share on LinkedIn2Buffer this pageShare on StumbleUpon0Share on Reddit0

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

Spread the words...Share on Google+4Share on Facebook1Tweet about this on Twitter3Share on LinkedIn2Buffer this pageShare on StumbleUpon0Share on Reddit0

 

 

Give me your email address and whenever I write any tutorial or discuss any java concept, you will receive a little email in your inbox.

Join 2,876 other subscribers

Want to ask any question? Or suggest anything?