How to compile annotation handler with Ant?

The question seems obvious, but the implementation is pretty hard for me.

My goal is to write an Ant build script to compile some classes that require other classes created by the annotation processor. I have custom annotations and this is a processor implementation (inherited from the AbstractProcessor class).

As I understand it, I need:

  • Compile Annotation Handler
  • Run the compiler on some annotated classes to generate new ones.
  • Compile classes that require generated classes

Code (steps 1 and 2):


 <target name="compileAnnotationProcessor"> <javac destdir="${OUTPUT_DIR}" debug="true" failonerror="true" includeantruntime="false" classpath="${java.class.path}"> <src> <pathelement path="${PROJECT_DIR}/tools/src"/> </src> <include name="/path/to/annotation/processor/package/**"/> </javac> </target> <target name="generateFilesWithAPT" depends="compileAnnotationProcessor"> <javac destdir="${OUTPUT_DIR}" includeantruntime="false" listfiles="false" fork="true" debug="true" verbose="true"> <src> <pathelement path="${PROJECT_DIR}/common/src/"/> </src> <include name="/path/to/files/to/compile/**"/> <classpath> <pathelement path="${OUTPUT_DIR}"/> <pathelement path="${java.class.path}"/> </classpath> <compilerarg line="-proc:only"/> <compilerarg line="-processorpath ${OUTPUT_DIR}/path/to/annotation/processor/package/annProcessorImplement"/> </javac> </target> 

Actually, the first task performs well and compiles the .class file to implement the Annotation processor. He stops at the second task.

Ant says: Annotation processing without compilation requested but no processors were found.

What am I doing wrong? Maybe I should put the annotation processor class in .jar ? Or provide a .class file name as the argument to -processorpath ? I tried several options, but nothing helps.


Notes:

I use the Ant javac task instead of apt because the documentation claims that the apt tool as well as the com.sun.mirror API are deprecated. I also looked through this question , but there is no information on how to compile the processor correctly.

I use:

  • Java 1.6
  • Ant 1.8.2
+8
java build annotations apt ant
source share
1 answer

My usual approach:

  • pack the annotation with the annotation processor in your own bank
  • register the annotation processor through META-INF / services in this bank

Then, wherever you depend on your annotations, the annotation handler is automatically selected without any additional configuration.

+4
source share

All Articles