用Eclipse Compiler编译Java项目

 

 我们经常使用ant来编译我们的Eclipse项目,但有时ant中javac任务往往只能编译一部分代码。当代码有编译错误时,ant就停止编译,不再继续编译剩下的代码。这种方式给一些单元测试或者脚本类的工程带来了麻烦,因为这类工程中的错误往往只是一个脚本的语法问题,并不影响其他脚本。所以这类工程希望尽可能多的编译全部代码,而不是因为一个编译错误而停止。

但我们在Eclipse里面编译工程时,却没有这样的问题,Eclipse将会把所有的代码都编译一遍,最后显示所有的错误。这是因为这时编译的是Eclipse自己实现的Java编译器。我们可以利用Eclipse自带的编译器,改进我们的ant脚本。
 
实现起来很简单:
1. 在ant脚本中加入一行:
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
如果javac任务中有这么一行,则删之:
compiler="javac1.5"
 
2. 下载ecj.jar,可以从http://download.eclipse.org/eclipse/downloads/下载,选择eclipse的版本号,然后在" JDT Core Batch Compiler"这个类别中就可以下载对应版本的ecj.jar了。
 
3. 运行ant命令,加上如下参数:
ant -f build.xml -lib ecj.jar
 
4. 如果遇到OutOfMemory的错误在编译过程中,则加上如下的环境变量:
set ANT_OPTS=-Xmx1024m
 
-----------------------------------

Using the ant javac adapter

The Eclipse compiler can be used inside an Ant buildfile using the javac adapter. In order to use the Eclipse compiler, you simply need to define thebuild.compiler property in your buildfile.

In order to get the batch compiler working in an ant buildfile, the ant runtime classpath needs to contain the Eclipse batch compiler. When you run your ant buildfile:

  1. outside of Eclipse: the easiest way to set up the ant runtime classpath is to add the ecj.jar file using the -lib argument or dumping it inside the ANT_HOME location.
  2. inside Eclipse using the same JRE than Eclipse: the Eclipse batch compiler is implicitely added to the ant runtime classpath.
  3. inside Eclipse using the different JRE: the Eclipse batch compiler must be explicitely added to the ant runtime classpath. This can be done using the ecj.jar file or using the org.eclipse.jdt.core jar file and the jdtCompilerAdapter.jar file located inside the org.eclipse.jdt.core jar file (this jar file needs to be extracted first).

Here is a small example:


<?xml version="1.0" encoding="UTF-8"?>
<project name="compile" default="main" basedir="../.">

	<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

	<property name="root" value="${basedir}/src"/>

	<property name="destdir" value="d:/temp/bin" />

	<target name="main">
		<javac srcdir="${root}" destdir="${destdir}" debug="on" nowarn="on" extdirs="d:/extdirs" source="1.4">
		    <classpath>
		      <pathelement location="${basedir}/../org.eclipse.jdt.core/bin"/>
		    </classpath>
		</javac>
	</target>
</project>

The syntax used for the javac Ant task can be found in the Ant javac task documentation. The current adapter supports the Javac Ant task 1.4.1 up to 1.6.5 versions.

If you are using a version above 1.5.0, you can use the nested compiler argument element (<compilerarg>) to specify compiler specific options.


...
<javac srcdir="${root}" destdir="${destdir}" debug="on" nowarn="on" extdirs="d:/extdirs" source="1.4">
    <classpath>
      <pathelement location="${basedir}/../org.eclipse.jdt.core/bin"/>
    </classpath>
    <compilerarg compiler="org.eclipse.jdt.core.JDTCompilerAdapter" line="-1.5 -warn:+boxing"/>
</javac>
...

Note:
  1. To prevent compiler dependant buildfiles, we strongly advise you to use a <compilerarg> whose "compiler" attribute value isorg.eclipse.jdt.core.JDTCompilerAdapter. If this is not set, the buildfile can only be used with the Eclipse compiler. If set, the nested compiler argument is ignored if the name is different from the compiler name specified by the build.compiler property.
  2. <compilerarg> should not be used to set values like target value, source value, debug options, or any options that could be set using the defined attributes of the javac ant task. Its usage must be reserved to pass compiler specific options like warning options. When a command-line argument is specified more than once, the Eclipse batch compiler can report errors like:
    duplicate target compliance setting specification: 1.5
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值