Maven:javac:源版本 1.6 需要目标版本 1.6 [英] Maven: javac: source release 1.6 requires target release 1.6

查看:35
本文介绍了Maven:javac:源版本 1.6 需要目标版本 1.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这似乎是javac"程序中的一个限制.

NOTE: This appears to be a limit in the "javac" program.

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

I have Java 6 code that needs to be built for a Java 5 JVM. My previous work with the javac ant target (both with the JDK compiler and with ecj) led me to believe that it would simply be a matter of setting source and target for javac. Hence this pom.xml fragment:

<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>

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

which works as expected from within Eclipse 3.7 with Maven support. Unfortunately, running Maven directly from the command line give me

javac: source release 1.6 requires target release 1.6

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

which is the same as generated by javac -source 1.6 -target 1.5. To clarify, this is the official OpenJDK 6 for Ubuntu

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 表现出相同的行为.

The official Oracle Java 7 JDK for Windows show the same behavior.

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

Note: I do not want to build against Java 5 libraries or anything. Just that the active javac generates Java 5 compatible bytecode.

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

How do I get what I want while still being compatible with the Eclipse Maven plugin?

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

( In addition to the @Override I also want to compile against the JAX-WS libraries in Java 6 when used, but still generated Java 5 byte code - I can then add the JAX-WS libraries deliberately in the web container when deploying to a Java 5 installation)

原来可以告诉 maven-compiler-plugin 使用另一个编译器,而 Eclipse 编译器可以这样做:

It turns out that maven-compiler-plugin can be told to use another compiler, and the Eclipse compiler can do this:

        <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 也支持开箱即用".

which compiles the class to Java 1.5 bytecode without complaints. This is also supported "out of the box" for m2e for Eclipse Java EE 4.2.2.

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

I found that of all things the javadoc tool dislikes the output from the Eclipse compiler.

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

EDIT 2015-06-28: I did a quick test recently and the latest ecj (corresponding to Eclipse 4.4) worked fine with javadoc.

推荐答案

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

The limitation is in javac. The solution is to tell maven to use another compiler. See question for details.

这篇关于Maven:javac:源版本 1.6 需要目标版本 1.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆