The ECJ compiler adapter

Scope: JDT projects

Ant4Eclipse provides it's own compiler adapter that allows you to use the eclipse compiler for Java (ECJ) within the ant <javac> task.

Advantages

Using the ECJ has many advantages over using the standard javac from the underlying JDK:

Enabling the ECJ compiler adapter (classic way)

To enable the ECJ, you have to set the Ant4Eclipse ECJ compiler adapter. This can be done by either setting the global build.compiler property, which will affect all <javac> tasks throughout the build, or by setting the compiler attribute, specific to the current <javac> task. The value to enable the Ant4Eclipse EcjCompilerAdapter is org.ant4eclipse.jdt.ecj.EcjCompilerAdapter.

The following example shows how to enable the Ant4Eclipse ECJ compiler adapter:

<javac destdir="${default.output.directory}"
       compiler="org.ant4eclipse.jdt.ecj.EcjCompilerAdapter" >
  ...
</javac>

Enabling the ECJ compiler adapter (modern way)

Alternatively you can make use of the jdtCompiler which is just an extension of the javac task simply allowing to switch between the javac and ecj compiler backend.

The following example shows how to enable the Ant4Eclipse ECJ compiler adapter:

<jdtCompiler destdir="${default.output.directory}" useecj="true">
  ...
</jdtCompiler>

Setting compiler options

The compiler options can be set in several ways.

Setting javac attributes

Setting JDT compiler option files

THe JDT compiler options file can be found at
workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.core.prefs or
project\.settings\org.eclipse.jdt.core.prefs. You can set the compiler option files using the compilerarg element:

<javac destdir="${buildPlugin.default.output.directory}"
      compiler="org.ant4eclipse.jdt.ecj.EcjCompilerAdapter" >
      ...
  <compilerarg value="compiler.options.file=my/project/path/.settings/org.eclipse.jdt.core.prefs"
            compiler="org.ant4eclipse.jdt.ecj.EcjCompilerAdapter" /> 
</javac>

<javac destdir="${buildPlugin.default.output.directory}"
      compiler="org.ant4eclipse.jdt.ecj.EcjCompilerAdapter" >
      ...
  <compilerarg value="default.compiler.options.file=my/path/org.eclipse.jdt.core.prefs"
            compiler="org.ant4eclipse.jdt.ecj.EcjCompilerAdapter" /> 
</javac>

Example usage

<javac destdir="${default.output.directory}"
      compiler="org.ant4eclipse.jdt.ecj.EcjCompilerAdapter" >
  <src refid="source.directories.path" />
  <bootclasspath refid="boot.classpath.path" />
  <classpath refid="classpath.absolute.compiletime.path" />
</javac>