将注释处理器集成到同一个项目中

是否可以在定义它的同一个项目中使用注释处理器?

例子:

  • 源/

    • MyAnnotation.java

    • path_to_MyAnnotationProcessor.MyAnnotationProcessor.java

    • 其他类

  • 资源

    • META-INF/services/javax.annotation.processing.Processor

  • 绒球

当我运行时mvn clean install,我希望我的处理器将处理使用 MyAnnotation 注释的类。

我不想从另一个库导入已经编译的处理器,我只想在我的 src 中定义它后使用它。

现在,我得到错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project my-project: Compilation failure [ERROR] Annotation processor 'path_to_MyAnnotationProcessor' not found

pom.xml 的一部分,我在这里引用。给我的处理器:

       <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>${maven.plugin.compiler}</version>

            <configuration>

                <source>${version.java}</source>

                <target>${version.java}</target>

                    <annotationProcessors>

                       <proc>path_to_MyAnnotationProcessor.MyAnnotationProcessor</proc>

                    </annotationProcessors>

            </configuration>

        </plugin>

感谢大家,尤其是 @Stefan Ferstl 和 @yegodm。来自 yegodm 的解决方案是:“一种方法是两个在同一个项目中有两个模块。一个模块将定义注释和处理器。另一个将它作为建立构建顺序的依赖项。”


潇湘沐
浏览 108回答 2
2回答

繁星淼淼

解决此问题的最简单方法是将您的项目转换为多模块项目,其中注释处理器位于其自己的模块中。注释处理器有一个不同的模块,您可以使用全新的<annotationProcessorPaths>选项通过groupId/定义注释处理器artifactId。使用注释处理器的模块可能需要依赖于注释处理器模块才能首先构建它。注意:在此答案的先前版本中,我描述了解决此问题的另一种方法,显然不能开箱即用。该部分已被删除。

白猪掌柜的

您可以通过单独的编译器执行更早地编译您的处理器。<build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>compile-generator</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>generate-sources</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>compile</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <includes>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <include>com/example/YourProcessor.java</include>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </includes>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build>我已经对此进行了测试,它可以工作——处理器确实在实际编译阶段稍后被调用。如果您也从同一个项目中预编译了一些其他类,那么您可以直接在处理器中引用和使用它们。那可能很有用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java