How to use custom annotation handler with Maven 2?

In our enterprise application, we are looking for a dynamic way to collect data from our Java classes. We created an annotation user interface ( @interface ) with the name property. We would like to get the value of this property from all annotated classes.

I managed to create AnnotationProcessorFactory and AnnotationProcessor for custom annotation. Since we are using Maven 2, I added the following plugins to the pom.xml 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> 

This is in the main project, which has several subprojects. factory and user processor are in one of these subprojects. User annotations are scattered across all subprojects, so I put the plugin in pom.xml main project.

The problem is that when I issue the mvn apt:process command, I received a warning about annotation without processors, and there is a separate annotation among them. I assume this means that the plugin cannot find the factory class.

What should I do so that the plugin can find the factory and processor file?

EDIT:

The project hierarchy is very simple:

 main_project |-sub_project1 |... |-sub_projectn 

The plugin is located in pom.xml main_project . Let's say that the factory and processor are in sub_project1 , and the user annotations are in sub_project2 , sub_project3 , ..., sub_projectn

+6
source share
1 answer

Two things to check:

  • Make sure main_project (or the plugin) is dependent on the project that contains your ComponentAnnotationProcessFactory .
  • The Apt Maven plugin <factory> configuration tag didnโ€™t work for me, but the plugin finds the factory class if its full name is in the META-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory file. (See apt documentation for details .)

The best approach is to use the JDK 6 annotation processing functions (instead of the Maven Apt plugin), since it does not require com.sun and tools.jar from the JDK lib folder.

 <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> 

Other links:

+6
source

Source: https://habr.com/ru/post/921390/


All Articles