注意:这似乎是"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>
Run Code Online (Sandbox Code Playgroud)
它可以在Eclipse 3.7中使用Maven支持按预期工作.不幸的是,直接从命令行运行Maven给了我
javac: source release 1.6 requires target release 1.6
Run Code Online (Sandbox Code Playgroud)
与生成的相同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:~$
Run Code Online (Sandbox Code Playgroud)
官方Oracle Java 7 JDK for Windows显示了相同的行为.
注意:我不想构建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>
Run Code Online (Sandbox Code Playgroud)
它将类编译为Java 1.5字节码而无需投诉.对于Eclipse Java EE 4.2.2的m2e,它也支持"开箱即用".
编辑:我发现javadoc工具不喜欢Eclipse编译器的输出.
编辑2015-06-28:我最近做了一个快速测试,最新的ecj(对应于Eclipse 4.4)与javadoc一起工作正常.
看来如果你想进行交叉编译,你需要提供一些额外的参数-bootclasspath和-extdirs,虽然我相信你只需要第一个.对于使用javac和实施例可以发现在这里用的额外选项的说明这里(交叉编译选项部分).
然后,您需要为maven-compiler-plugin配置这些选项.根据我的理解,你需要设置插件到fork,以便它将使用编译器参数而不是内置编译器.您可以在此处找到所有选项的列表
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.5</target>
<fork>true</fork>
<compilerArguments>
<bootclasspath>${1.5.0jdk}\lib\rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
....
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47699 次 |
| 最近记录: |