如何在 Maven 2 中使用自定义注释处理器? [英] How to use custom annotation processor with Maven 2?

查看:37
本文介绍了如何在 Maven 2 中使用自定义注释处理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的企业应用程序中,我们正在寻求一种动态方式来从我们的 Java 类中收集数据.我们创建了一个带有 name 属性的自定义注释接口 (@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 apt:process 命令时,我收到关于没有处理器的注释的警告,我们的自定义注释就在其中.我认为这意味着插件找不到工厂类.

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

插件在main_projectpom.xml中.假设工厂和处理器在 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

推荐答案

需要检查的两件事:

  1. 确保 main_project(或插件)取决于包含您的 ComponentAnnotationProcessFactory 的项目.
  2. Apt Maven 插件的 <factory> 配置标签对我不起作用,但如果它的完全限定名称在 META-INF/services 中,插件会找到工厂类/com.sun.mirror.apt.AnnotationProcessorFactory 文件.(有关详细信息,请参阅 apt 文档.)
  1. Make sure that the main_project (or the plugin) depends on the project which contains your ComponentAnnotationProcessFactory.
  2. 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 the META-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory file. (See the documentation of apt for the details.)

更好的方法是使用 JDK 6 的注释处理特性(而不是 Maven Apt 插件),因为它不需要 com.sun 包和 tools.jar 来自lib JDK 文件夹.

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>

进一步参考:

  • Annotation Processing Tool in the The Java Specialists' Newsletter
  • What is the default annotation processors discovery process?

这篇关于如何在 Maven 2 中使用自定义注释处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆