`

用Eclipse Compiler编译Java项目

    博客分类:
  • JVM
 
阅读更多

 

 我们经常使用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
分享到:
评论

相关推荐

    compiler-eclipse:适用于 Java 1.8 的 Maven 编译器插件

    编译器-eclipse 适用于 ... 使用 xqbase-compiler-eclipse 作为通过 maven 编译的解决方法: &lt; build&gt; &lt; plugins&gt; &lt; plugin&gt; &lt; artifactId&gt;maven-compiler-plugin&lt;/ artifactId&gt; &lt; version&gt;3.2&lt;/ version&gt; &lt; conf

    JAVA上百实例源码以及开源项目源代码

     Java编写的网页版魔方游戏,编译后生成.class文件,然后用HTML去调用,不过运行时候需要你的浏览器安装有运行Class的插件。Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像...

    一个优秀的java反编译小工具

    * Eclipse Java Compiler v_677_R32x,3.2.1 release * jrockit90_150_06 3、支持java5如下新特性: * Annotations * Generics * Type “enum” 4、支持对整个jar反编译 5、支持拖放和源代码高亮显示 6、免安装...

    JAVA上百实例源码以及开源项目

     Java编写的网页版魔方游戏,编译后生成.class文件,然后用HTML去调用,不过运行时候需要你的浏览器安装有运行Class的插件。Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像...

    Java解析语法树(AST)使用JDT相关库

    包括以下包:jdt-3.2.1-r321_v20060823.jar、 jdt-3.3.0-v20070607-1300.jar、jdt-3.4.2_r894.jar、jdt-compiler.jar、org.eclipse.datatools.enablement.jdt.classpath_1.0.1.v201107221501.jar、org.eclipse.jdt....

    kotlin compiler

    这使得在 IDE 中开发的代码能够利用已有的机制来构建,从而尽可能的减少了在新环境中使用所受的干预,哪怕与那些没有安装 Kotlin 插件的开发人员一起合作项目也没有问题。 The IntelliJ Kotlin 插件扩展了 Java ...

    eclipse 开发c/c++

    CDT 是完全用 Java 实现的开放源码项目(根据 Common Public License 特许的),它作为 Eclipse SDK 平台的一组插件。这些插件将 C/C++ 透视图添加到 Eclipse 工作台(Workbench)中, 现在后者可以用许多视图和向导...

    intellij-bug-eclipse-compiler-warnings:https 的示例项目

    示例项目用 javac 编译器编译,正确抑制警告: 用 Eclipse 编译器编译,警告没有被抑制:

    推荐一款Java反编译器,比较好用

    推荐一款Java反编译器,也使用了挺久的了,感觉还是很好用,就拿出和大家分享一下。 这款反编译器叫 "Java Decompiler", 由 Pavel Kouznetsov开发,目前最新版本为0.2.5. 它由 C++开发,并且官方可以下载 windows、...

    java开源包8

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包4

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包101

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包11

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包6

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包9

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包5

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    java开源包10

    WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...

    JAVA入门1.2.3:一个老鸟的JAVA学习心得 PART1(共3个)

    1.5.2 Java编译器(Java Compiler) 17 1.5.3 Java类库(Java Class Libraries) 17 1.5.4 Java虚拟机(Java Virtual Machine) 17 1.5.5 HelloWorld的整个流程 17 1.6 小结:我们学会了编译和运行一个Java程序...

    demo-project-eclipse:演示如何使用Eclipse IDE集成RongCloud SDK 2.0

    融云 ##融云 Demo 2.0 运行方式 ###Eclipse 环境 #####1. 下载: ...操作步骤:在 Eclipse 中分别右击 RongDemo、RongIMKit、android-support-v7-appcompat 项目 在菜单中选择 Properties-&gt;java Compiler-

Global site tag (gtag.js) - Google Analytics