7

I would like to use a custom annotation in my project.

How can I configure maven 3 to process my annotation. The annotation and the implementation of the AbstractProcessor class are embedded in my application.

My annotation is available only for testing (src/test/java).

State annotation :

@Target(ElementType.METHOD)
public @interface State {
  boolean success() default true;
}

TestAnnotationsProcessor :

@SupportedAnnotationTypes("com.*****.client.State")
public class TestAnnotationsProcessor extends AbstractProcessor {

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    System.out.print("TESSST ANNOTATION");
    return true;
  }
}

I don't want to put my annotation in an external project... it will be stupid because it's really dependend of my project.

How can I do that ? Thanks.

Sandro Munda
  • 39,921
  • 24
  • 98
  • 123
  • What is the problem? Is maven ignoring the annotation during compilation? Or are you getting a problem similar to http://stackoverflow.com/questions/2335655/why-is-javac-failing-on-override-annotation – Raghuram Feb 03 '12 at 12:57
  • 1
    Nothing happens, I would like to see in my console : "TESSST ANNOTATION" but nothing is displayed. – Sandro Munda Feb 03 '12 at 13:13
  • @SandroMunda I have the same issue. I followed this tutorial: https://deors.wordpress.com/2011/10/08/annotation-processors/ and what I need is skip the step with exporting annotations.processors project into .jar and setting "Factory path" to it. Is there a way to do make annotation processors work without exporting to .jar after every single change ? I would like to have Maven task to do this during compilation. Is this possible? – DominikStyp Apr 06 '16 at 00:59

1 Answers1

3

Could you do something like (forgive me if the syntax is a little off, I don't have my IDE handy):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin<artifactId>
  <version>2.3.2</version>
  <executions>
    <execution>
      <goals>
        <goal>testCompile</goal>
      </goals>
      <phase>test-compile</phase>
      <configuration>
        <annotationProcessors>com.******.TestAnnotationsProcessor</annotationProcessors>
        <proc>only</proc>
      </configuration>
    </execution>
  </executions>
</plugin>
Pace
  • 41,875
  • 13
  • 113
  • 156
  • How do you do this in Gradle? – Avik Oct 31 '14 at 08:38
  • Is this work with other versions of the maven-compiler-plugin ? Cause I've already tried this with 3.3, and annotation processor doesn't work. It works only when I include exported .jar file in: Project Properties -> Java Compiler -> Annotation Processing -> Factory Path -> Add Jars. Is there any other way to to do this automatically without exporting processors to .jar after each change ? – DominikStyp Apr 05 '16 at 20:58
  • @DominikStyp It sounds like you're trying to get the annotation processing running from Eclipse or some other IDE? These instructions are for Maven only. Getting Eclipse to understand annotations like this automatically is something I have not had much luck with and not from lack of trying. – Pace Apr 06 '16 at 03:37
  • @Pace Yes, exactly. I seems that Eclipse doesn't have a proper tool to do the trick. What I managed to do is automatically generate the JAR file using maven-assembly-plugin and my own assembly descriptor, which generates the proper JAR during package phase. – DominikStyp Apr 25 '16 at 20:35