4
votes

I am using maven enforcer plugin to enforce only jdk 1.7 (I am using java.nio.file). For some reason, maven enforcer plugin can't detect jdk 1.7.

λ ~/ java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
λ ~/ javac -version
javac 1.7.0_13
λ ~/ mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 15:51:28+0200)
Maven home: /usr/local/Cellar/maven/3.0.5/libexec
Java version: 1.7.0_13, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.7.5", arch: "x86_64", family: "mac"

This is the code I have in my pom.xml -

   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <executions>
        <execution>
          <id>enforce-versions</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireJavaVersion>
                <version>1.7</version>
              </requireJavaVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>

And this is the error I get -

[INFO] --- maven-enforcer-plugin:1.2:enforce (enforce-versions) @ com.microsoft.gittf.core ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireJavaVersion failed with message:
Detected JDK Version: 1.6.0-43 is not in the allowed range 1.7.

EDIT

mvn enforcer:display-info

shows version 1.7 and not 1.6... why the enforce detects java version 1.6?

3
Do you have multiple JDK on your machine? Please also post the configuration for the maven-compiler-plugin.Charlee Chitsuk
Running mvn enforcer:display-info solved it for after applying MrTinkz solution.ronnyfm

3 Answers

8
votes

Add the following (or create this file if it is missing) ~/.mavenrc

export JAVA_HOME=/Library/Java/JavaVirtualMachines/{your-jdk-here}/Contents/Home

The .mavenrc is only picked up by maven so other Java programs won't be affected by this change.

7
votes

The version decleration is fixed. What you gave is a exact version of java. Always remember if we are not sure about exact version we should use give the range. Change the version to [1.7,)

          <requireJavaVersion>
            <version>[1.7,)</version>
          </requireJavaVersion>

This means any version equal or greater than 1.7 is acceptable.

0
votes

You don't say what OS you're running under but the command "java ...." will find the first executable file called java (or java.exe on windows) on the path, this may or may not be the one that you want to use with Maven.

There is a system under linux called "alternatives" that maps a specific installation of java to a location that is almost certain (unless you mess with your path) to be first on your path. The purpose of this system is to allow you to keep many versions of programs such as java on your system, and have one be the default you will use unless you specify something different.

However this has nothing to do with Maven, which requires a JDK installation to run correctly. There are many executables in a java JDK installation that maven relies on, the javac compiler, the jar executable for building jars, just to name the most obvious ones. Using the alternatives system you would have to map alternatives for each of these and you might easily miss a few. That is why Maven relies instead on the JAVA_HOME variable.

On a linux system one handy place to set the JAVA_HOME variable is the file ~/.mavenrc. Maven reads this file if it exists.