8

这个问题似乎很明显,但实施对我来说非常困难。

我的目标是编写Ant 构建脚本来编译一些需要由 Annotation Processor生成的其他类的类。我有一个自定义注释,它是处理器实现(从AbstractProcessor类继承)。

据我了解,我需要:

  1. 编译注释处理器
  2. 在一些带注释的类上运行编译器以生成新的类。
  3. 编译需要生成类的类

代码(第 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>

实际上,第一个任务是执行良好并为 Annotation 处理器实现编译 .class 文件。它停止在第二个任务。

蚂蚁 说: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
4

1 回答 1

4

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

然后,无论您对注释有依赖关系,注释处理器都将被自动拾取,而无需任何额外配置。

于 2012-10-12T08:41:17.260 回答