【发布时间】:2012-01-23 11:30:25
【问题描述】:
注意:这似乎是“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 库或任何东西进行构建。只是活动 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 上运行良好。
【问题讨论】:
-
注意:已编辑问题以阐明我想要什么,并且我仍然需要与 Eclipse 兼容。
-
@ThorbjørnRavnAndersen 那我再给你一次机会。 javactask是如何配置的?根据文档,大多数编译器都会忽略 source 参数:“-source 命令行开关的值;将被 javac1.4 之前的所有实现忽略(或者当 Ant 不在 1.3 VM 中运行时现代) , gcj 和 jikes。”
-
@PeterLiljenberg 用于 ant 任务,而不是 maven 任务。我不知道在 Maven 引擎“外部”编译是否会丢失任何类型的信息或报告。