java - Maven:javac:源版本 1.6 需要目标版本 1.6

标签 java maven javac

注意:这似乎是“javac”程序中的一个限制。

我有需要为 Java 5 JVM 构建的 Java 6 代码。我之前使用 javac ant 目标(使用 JDK 编译器和使用 ecj)的工作让我相信这只是为 javac 设置源和目标的问题。因此这个 pom.xml 片段:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.5</target>
    </configuration>
</plugin>

在具有 Maven 支持的 Eclipse 3.7 中按预期工作。不幸的是,直接从命令行运行 Maven 给了我

javac: source release 1.6 requires target release 1.6

javac -source 1.6 -target 1.5生成的相同。澄清一下,这是适用于 Ubuntu 的官方 OpenJDK 6

x@JENKINS:~$ javac -version
javac 1.6.0_20
x@JENKINS:~$ javac -source 1.6 -target 1.5
javac: source release 1.6 requires target release 1.6
x@JENKINS:~$

适用于 Windows 的官方 Oracle Java 7 JDK 显示相同的行为。

注意:我不想针对 Java 5 库或任何东西进行构建。只是 Activity javac 生成 Java 5 兼容字节码。

如何在仍然与 Eclipse Maven 插件兼容的同时获得我想要的东西?

(编辑:除了@Override,我还想在使用时针对 Java 6 中的 JAX-WS 库进行编译,但仍会生成 Java 5 字节码 - 然后我可以故意在 Web 中添加 JAX-WS 库部署到 Java 5 安装时的容器)


编辑:事实证明,maven-compiler-plugin 可以被告知使用另一个编译器,而 Eclipse 编译器可以这样做:

        <plugin>
            <!-- Using the eclipse compiler allows for different source and target, 
                which is a good thing (outweighing that this is a rarely used combination, 
                and most people use javac) This should also allow us to run maven builds 
                on a JRE and not a JDK. -->

            <!-- Note that initial experiments with an earlier version of maven-compiler-plugin 
                showed that the eclipse compiler bundled with that gave incorrect lines in 
                the debug information. By using a newer version of the plexus-compiler-eclipse 
                plugin this is hopefully less of an issue. If not we must also bundle a newer 
                version of the eclipse compiler itself. -->

            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.5</target>
                <debug>true</debug>
                <optimize>false</optimize>
                <fork>true</fork>
                <compilerId>eclipse</compilerId>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-compiler-eclipse</artifactId>
                    <version>2.1</version>
                </dependency>
            </dependencies>
        </plugin>

它将类编译为 Java 1.5 字节码而没有任何提示。 Eclipse Java EE 4.2.2 的 m2e 也支持“开箱即用”。

编辑:我发现 javadoc 工具不喜欢 Eclipse 编译器的输出。

编辑 2015-06-28:我最近做了一个快速测试,最新的 ecj(对应于 Eclipse 4.4)在 javadoc 上运行良好。

最佳答案

限制在 javac 中。解决方案是告诉 maven 使用另一个编译器。详情见问题。

关于java - Maven:javac:源版本 1.6 需要目标版本 1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970920/

相关文章:

java - 无法运行打包的 TestMain.java

java - 为什么 Java 字节码方法中使用的局部变量数量不是最经济的?

java - 为什么Java除以0.0时不抛出异常?

android - 本地Maven存储库需要sudo通过Gradle解决 Artifact

maven - 如何指定为原型(prototype) :generate? 创建项目的目录

java - Maven 没有运行 JUnit 测试

Java 编译器为字符串连接构建低效代码?

java - 使用 Java 对无序 XML 进行排序

java - Clojure:[D 和 [Ljava.lang.Double; 之间有什么区别?

java - Citrus 示例 TCP 发送和接收失败