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

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

问题描述

注意:这似乎是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>

在Eclipse 3.7中支持Maven,可以正常工作。不幸的是,直接从命令行运行Maven给了我

  javac:源版本1.6需要目标版本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:源版本1.6需要目标版本1.6
x @JENKINS:〜$

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



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



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



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






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

 <插件> 
<! - 使用eclipse编译器允许不同的源和目标,
这是一件好事(超过这是一个很少使用的组合,
和大多数人使用javac)还应该允许我们在JRE而不是JDK上运行maven build
。 - >

<! - 请注意,使用早期版本的maven-compiler-plugin
的初步实验表明捆绑的eclipse编译器在
中给出了错误的行调试信息。通过使用更新版本的plexus-compiler-eclipse
插件,希望这不是一个问题。如果不是,我们还必须捆绑更新的
版本的eclipse编译器本身。 - >

< 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使用另一个编译器。有关详细信息,请参阅问题。


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

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>

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

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:~$

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

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

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

(EDIT: 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)


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

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.

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

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

解决方案

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天全站免登陆