我正在使用 Maven 3.0.5。我对“maven-compile-plugin”有疑问。基本上,我有 2 个项目,旧的在 Java 6 中,新的在 Java 7 下。我目前的 JAVA_HOME 很好,是:
$ echo $JAVA_HOME
C:\Program Files (x86)\Java\jdk1.6.0_38
在新项目中,我将 maven-compile-plugin 与此配置一起使用:
<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>
我在 settings.xml 中声明了 JDK7 变量:
<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>
在新项目中,我还使用强制插件来检查编译的源代码是否符合 Java 7,所以在我的 POM 中,我有:
<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>
我对 Maven 的不理解是它总是使用 JAVA_HOME 定义的编译器。由于它默认为 Java 6,所以当我构建新项目时,强制插件总是失败,因为 Maven 使用 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.
你能帮我解决这个问题吗?