Skip to Content

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:

  • Access restrictions on projects or libraries: The ECJ compiler adapter supports access restrictions defined for projects and/or libraries. E.g.if you want to compile PDE plug-in projects according to the OSGi visibility rules, it is neccessary to restrict the access to classes in referenced bundles.
  • Access restrictions on the boot class path: If you want to compile against a execution environment instead of compiling against a specfic JDK, it is necessary to restrict the access to the libraries available from the specfic JDK.
  • Full support for 'output folders for source folders': The ECJ compiler adapter also supports the usage of several output folders for source folders. It it possible to compile such a project, even the classes defined in the source folders have cyclic references to each other.
  • Usage of eclipse's compiler options: As the ECJ compiler adapter uses the eclipse compiler, all the eclipse's compiler options are supported (including warnings and/or errors).
  • Compilation of bundle with cyclic dependencies: The ECJ allows you to compile bundles with cyclic dependencies. Although it is very ugly to allow cycles in an application, it can be temporarily necessary for migration purpose.

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>