【问题标题】:Maven ignores the "maven-compiler-plugin" requirementsMaven 忽略“maven-compiler-plugin”要求
【发布时间】:2014-02-23 01:07:50
【问题描述】:

我正在使用 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,所以当我构建新项目时,enforcer 插件总是失败,因为 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.

你能帮我解决这个问题吗?

【问题讨论】:

  • 我不认为enforcer插件的这条规则是来检查JDK兼容性的。只是保证机器上安装的JDK版本的一种方式:maven.apache.org/enforcer/enforcer-rules/…
  • 但如果这样可以简化您的流程。您实际上可以在每天使用版本 7 的同时以 JDK 6 兼容模式进行编译。你可以相信 maven 编译器插件来处理他的 cnnfiguration
  • 如果您想检查是否符合特定的 JDK,您应该查看animal-sniffer-maven-plugin

标签: java maven maven-compiler-plugin maven-enforcer-plugin


【解决方案1】:

好的,我想我找到了根本原因,可能是我是个白痴。 Maven 是使用 JDK 6 启动的,但是这些类在 Java 7 中编译得很好,我检查了输出类的次要/主要版本。正如 Aurélien 提到的,enforcer 插件只是在这里检查 Maven 是否随 JDK 6 一起启动。所以我删除了enforcer 规则。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多