JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Maven Enforce Minimum Java Version

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 maven project

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

2) Add maven enforcer plugin

pom.xml
<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 complain for operating system.

3) Demo

Compile the project with version mismatches.
pom.xml
$ mvn compile
Compilation fails with following error.
Console
[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:
Console
[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.

.