Apt

Description

Runs the annotation processor tool (apt), and then optionally compiles the original code, and any generated source code.

This task runs on Java 1.5 to Java 1.7.

Apt is deprecated in Java 1.6, which can run annotation processors as part of javac, and removed from the distribution in Java 1.8. The task will fire an exception when attempting to run under Java 1.8.

This task inherits from the Javac Task, and thus supports nearly all of the same attributes, and subelements. There is one special case, the fork attribute, which is present but which can only be set to true. That is, apt only works as a forked process.

In addition, it supports the following addition items:

Parameters

Attribute Description Required
compile After running the Apt, should the code be compiled. (see the -nocompile flag on the Apt executable) No, defaults to false.
factory The fully qualified classname of the AnnotationProcessFactory to be used to construct annotation processors. This represents the -factory command line flag of the Apt executable. No
factorypathref The reference id of the path used to find the classes needed by the AnnotationProcessorFactory (and the location of the factory itself). This represents the -factorypath flag on the Apt executable. No
preprocessdir The directory used for preprocessing. This is the directory where the generated source code will be place. This represents the -s flag on the Apt executable. No

Parameters specified as nested elements

factorypath

You can specify the path used to find the classes needed by the AnnotationProcessorFactory at runtime, using this element. It is represents as a generic path like structure. This represents the -factorypath flag on the Apt executable.

option

Used to represent a generic option to pass to Apt. This represents the -A flag on the Apt executable. You can specify zero or more <option> elements.

Attribute Description Required
name The name of the option Yes.
value The value to set the option to Yes.

Examples

<apt srcdir="${src}"
     destdir="${build}"
     classpath="xyz.jar"
     debug="on"
     compile="true"
     factory="com.mycom.MyAnnotationProcessorFactory"
     factorypathref="my.factorypath.id"
     preprocessdir="${preprocess.dir}">
</apt>

compiles all .java files under the ${src} directory, and stores the .class files in the ${build} directory. The classpath used includes xyz.jar, and compiling with debug information is on. It also forces the generated source code to be compiled. The generated source code will be placed in ${preprocess.dir} directory, using the class com.mycom.MyAnnotationProcessorFactory to supply AnnotationProcessor instances.

Notes

The inherited "fork" attribute is set to true by default; please do not change it.

The inherited "compiler" attribute is ignored, as it is forced to use the Apt compiler

Using the Apt compiler with the "compile" option set to "true" forces you to use Sun's Apt compiler, which will use the JDK's Javac compiler. If you wish to use another compiler, you will first need run the Apt processor with the "compile" flag set to "false", and then use a <javac> task to compile first your original source code, and then the generated source code:

<apt srcdir="${src}"
     destdir="${build}"
     classpath="xyz.jar"
     debug="true"
     compile="false"
     factory="com.mycom.MyAnnotationProcessorFactory"
     factorypathref="my.factorypath.id"
     preprocessdir="${preprocess.dir}">
</apt>

<javac srcdir="${src}"
       destdir="${build}"
       classpath="xyz.jar"
       debug="on"/>

<javac srcdir="${preprocess.dir}"
       destdir="${build}"
       classpath="xyz.jar"
       debug="true"/>
This may involve more build file coding, but the speedup gained from switching to jikes may justify the effort.