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 the build.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 implicitly added to the ant runtime classpath.
  3. inside Eclipse using the different JRE: the Eclipse batch compiler must be explicitly 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 is org.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