Question

I am using Maven 3.0.5. I have an issue with the "maven-compile-plugin". Basically, I have 2 projects, the legacy one is in Java 6, and the new one is under Java 7. My current JAVA_HOME is fine and is:

$ echo $JAVA_HOME C:\Program Files (x86)\Java\jdk1.6.0_38

In the new project, I use the maven-compile-plugin with this configuration:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <fork>true</fork>
            <verbose>true</verbose>
            <source>1.7</source>
            <target>1.7</target>
            <executable>${JDK7}</executable>
            <compilerVersion>1.7</compilerVersion>
        </configuration>
    </plugin>

I have declared the JDK7 variable in the settings.xml:

    <profiles>
    <profile>
        <id>compiler</id>
        <properties>
            <JDK6>C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac</JDK6>
            <JDK7>C:\Program Files (x86)\Java\jdk1.7.0_45\bin\javac</JDK7>
        </properties>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>compiler</activeProfile>
</activeProfiles>

In the new project, I am also using the enforcer plugin to check the compiled sources are Java 7 compliant, so in my POM, I have:

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

The thing I don't understand with Maven is that it always uses the compiler defined by the JAVA_HOME. Since it's defaulted to Java 6, when I build the new project, the enforcer plugin always fails cause Maven uses java 6:

        $ mvn compile -X
    Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
    Maven home: c:\Dev\apache-maven-3.0.5
    Java version: 1.6.0_38, vendor: Sun Microsystems Inc.
    Java home: c:\Program Files (x86)\Java\jdk1.6.0_38\jre
    Default locale: fr_FR, platform encoding: Cp1252
    OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
[...]
[INFO] --- maven-enforcer-plugin:1.3.1:enforce (enforce-versions) @ arom-wrapper ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireJavaVersion failed with message:
Detected JDK Version: 1.6.0-38 is not in the allowed range 1.7.

Could you please help me on this one ?

Was it helpful?

Solution

Ok, I think I have found the root cause and it's probably me being an idiot. Maven is launched using the JDK 6, but the classes are well compiled in Java 7, I have check the minor/major version of the output classes. The enforcer plugin, as Aurélien mentionned, is only here to check that Maven was launched with the JDK 6. So I removed the enforcer rule. Thank you !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top