Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile the annotation processor with Ant?

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
like image 618
ivstas Avatar asked Oct 12 '12 08:10

ivstas


People also ask

How do I run an annotation processor?

Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.

How does annotation processing work?

The annotation processing is done in multiple rounds. Each round starts with the compiler searching for the annotations in the source files and choosing the annotation processors suited for these annotations. Each annotation processor, in turn, is called on the corresponding sources.

What is annotation processor in eclipse?

The JDT-APT project provides plugins that add Java 5 annotation processing support to Eclipse. A Java annotation processor is a compiler plug-in that can gather information about source code as it is being compiled, generate additional Java types or other resource files, and post warnings and errors.

What is annotation processing tool?

Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.


1 Answers

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.

like image 147
Puce Avatar answered Oct 09 '22 08:10

Puce