Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven example of annotation preprocessing and generation of classes in same compile process?

Does anyone have a clean example of a maven project preprocessing class annotations at compile time with subsequent generation of classes to be compiled in the same compilation process?

Does anyone have a step-by-step procedure to implement such a project?

like image 584
Jérôme Verstrynge Avatar asked Aug 06 '11 14:08

Jérôme Verstrynge


People also ask

What is Annotationprocessor?

The process of generating code at compile time to handle the annotations is called Annotation Processing. The annotation processor can validate, generate, and modify your code based on the annotations, which help you significantly reduce the amount of code you need to write.

How do I enable annotations in processing?

Go to "Java Compiler - Annotation Processing" and choose "Enable annotation processing" Go to "Java Compiler - Annotation Processing - Factory Path" and add the JAR hibernate-validator-annotation-processor-5.2.

Can classes be annotated in Java?

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line. As of the Java SE 8 release, annotations can also be applied to the use of types.

Which of the following is annotation processing tool?

The apt tool is a command-line utility for annotation processing.


2 Answers

maven-processor-plugin can do that...

https://code.google.com/p/maven-annotation-plugin/

Example from documentation:

<build> <plugins>
  <!-- Run annotation processors on src/main/java sources -->
  <plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
      <execution>
        <id>process</id>
        <goals>
          <goal>process</goal>
        </goals>
        <phase>generate-sources</phase>
      </execution>
    </executions>
  </plugin>
  <!-- Disable annotation processors during normal compilation -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <compilerArgument>-proc:none</compilerArgument>
    </configuration>
  </plugin>
</plugins> </build>
like image 45
Dežo Avatar answered Oct 06 '22 20:10

Dežo


After navigating a lot in existing documentation on the net, I came up with the following:

What needs to be clarified:

  • In order to process annotations on a given project P, you first need an annotation processor compiled in a separate library S. P should have a dependency on S.
  • Implementing annotation processing in Java 5 is absolutely not the same thing as in Java 6.
  • Java 5 relies on a separate execution of apt. The corresponding tutorials here and here help understanding the basics of annotation processing and implementation in Java 5. Good reading for newbies.
  • Implementing annotation processing in Java 5 with Maven is tricky. One needs to add a local dependency to tools.jar to access the API described in these tutorials. Not clean. Some third-party plugins calling the apt are available, but not well documented.
  • Those using Java 6 should not jump-start implementing their processors according to the above tutorials.

Annotation Processing in Java 6 with Maven

  • A new package has been delivered in Java 6 to process annotations: the Pluggable Annotation Processing.
  • To implement a processor, create a separate Maven project. The above tutorial or this one explains how to proceed. This is our library S.
  • Then, create your project P and add a Maven dependency on S.
  • There is currently an issue with the maven-compiler-plugin, but a workaround is available here. Use it to compile your generated code as part of existing annotated code.

...and code generation

  • A great Java code generation library called CodeModel is available from Maven central. A good tutorial is available here. The javax annotation processing package offers some tools to generate output too.
like image 64
Jérôme Verstrynge Avatar answered Oct 06 '22 21:10

Jérôme Verstrynge