8
votes

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

My goal is to write Ant build script to compile some classes that require another classes generated by Annotation Processor. I have a custom annotations and it's processor implementation (inherited from AbstractProcessor class).

As I understand I need to:

  1. Compile the Annotation Processor
  2. Run the compiler over some annotated classes to generate the new ones.
  3. Compile the classes that require generated classes

The code (step 1 & 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 is performing good and compiles the .class file for the Annotation processor implementation. It is stopping at 2nd 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 a .jar? Or provide a file name with .class extension as -processorpath argument? I tried several options but nothing helps..


Notes:

I'm using ant javac task instead of aptone because documentation claims that apt tool as well as com.sun.mirror API is deprecated. I've also looked through this question, but there is no information how to compile the processor in right way.

I'm using:

  • Java 1.6
  • Ant 1.8.2
1

1 Answers

4
votes

My usual approach is:

  • pack the annotation together with the annotation processor in its own jar
  • register the annotation processor via META-INF/services in that jar

Then wherever you have a dependency on your annotations, the annotation processor will be picked up automatically without any additional configuration.