【发布时间】:2019-09-07 00:11:25
【问题描述】:
是否可以在定义它的同一个项目中使用注释处理器?
例子:
-
src/
- MyAnnotation.java
- path_to_MyAnnotationProcessor.MyAnnotationProcessor.java
- 其他类
- 资源
- META-INF/services/javax.annotation.processing.Processor
- pom
当我运行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的解决方案是: “一种方法是两个在同一个项目中有两个模块。一个模块将定义注释和处理器。另一个将它作为建立构建顺序的依赖项。”
【问题讨论】:
-
你试过了吗? :)
-
您是否尝试过,将元语法“path_to_MyAnnotationProcessor”替换为 .pom 中注释处理器的实际路径? (如果是,请发布您的 POM)
-
一种方法是两个在同一个项目中有两个模块。一个模块将定义注释和处理器。另一个将它作为建立构建顺序的依赖项。
-
如果这只是一个解决方案,那我就这样做,谢谢!
-
@BadZen 认为这是一种快速的解决方法。这就是为什么我不把它作为一个答案。对我来说,它工作得非常轻松。