如何使用自定义注解处理器与Maven 2? [英] How to use custom annotation processor with Maven 2?
问题描述
在我们的企业应用程序,我们正在寻求一种动态的方式来收集我们的Java类的数据。我们创建了一个自定义的注释界面( @interface
)以名称
属性。我们想从所有注解的类收集这些属性的值。
In our enterprise application we are seeking a dynamic way to collect data from our Java classes. We created a custom annotation interface (@interface
) with a name
property. We would like to collect the value of this property from all annotated classes.
我设法创建一个 AnnotationProcessorFactory
和 AnnotationProcessor
自定义注释。由于我们使用Maven 2,添加以下到的pom.xml
主体工程的插件。
I managed to create an AnnotationProcessorFactory
and an AnnotationProcessor
for the custom annotation. Since we are using Maven 2, I added the following to the plugins in the pom.xml
of the main project.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.0-alpha-5</version>
<configuration>
<factory>our.company.api.component.lister.ComponentAnnotationProcessFactory</factory>
</configuration>
</plugin>
这驻留在其中有若干个子项目的主项目。工厂和定制处理器在这些子项目之一。自定义注释分散在所有的子项目,这就是为什么我把在主项目的的pom.xml
插件。
This resides in the main project which has several sub-projects. The factory and the custom processor are in one of these sub-projects. The custom annotations are scattered through all of the sub-projects that is why I put the plugin in the pom.xml
of the main project.
问题是,当我发出 MVN贴切:进程
命令我得到了一个关于注释警告不处理器和我们自定义的注释是其中之一。我认为这意味着该插件找不到工厂类。
The problem is when I issue the mvn apt:process
command I got a warning about the annotations without processors and our custom annotation is among them. I assume this means that the plugin cannot find the factory class.
我应该怎么做这样的插件可以找到厂家和处理器的文件?
What should I do so the plugin could find the factory and the processor file?
编辑:
该项目的层次结构是很简单的:
The project hierarchy is very simple:
main_project
|-sub_project1
|...
|-sub_projectn
该插件是在的pom.xml
的 main_project
。只是假设,工厂和处理器都在 sub_project1
和自定义的注解是在 sub_project2
, sub_project3
... sub_projectn
The plugin is in the pom.xml
of the main_project
. Just assume that the factory and processor are in sub_project1
and the custom annotations are in sub_project2
, sub_project3
, ..., sub_projectn
推荐答案
两件事情要检查:
- 确保该
main_project
(或插件)的取决于其中包含项目你的ComponentAnnotationProcessFactory
。 - 的
&LT;工厂&GT;
的公寓Maven插件的配置标签并没有为我工作,但该插件发现工厂类,如果它的全名是在META-INF /服务/ com.sun.mirror.apt.AnnotationProcessorFactory
文件。 (见apt的文档的详细信息 。)
- Make sure that the
main_project
(or the plugin) depends on the project which contains yourComponentAnnotationProcessFactory
. - The
<factory>
configuration tag of the Apt Maven Plugin did not work for me but the plugin finds the factory class if its fully qualified name is in theMETA-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory
file. (See the documentation of apt for the details.)
有一个更好的办法是使用 JDK 6 (而不是Maven的公寓插件)的批注处理功能,因为它不要求的com.sun
包和工具的.jar
从JDK的 LIB
文件夹中。
A better approach is using the annotation processing features of JDK 6 (instead of the Maven Apt Plugin) since it does not require the com.sun
package and the tools.jar
from the lib
folder of the JDK.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<annotationProcessors>
<annotationProcessor>
com.example.annotationprocessor.Processor
</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
</plugins>
</build>
更多参考:
- 注释处理工具在了Java专家的时事通讯
- 什么是默认的注解处理器发现过程?
- Annotation Processing Tool in the The Java Specialists' Newsletter
- What is the default annotation processors discovery process?
这篇关于如何使用自定义注解处理器与Maven 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!