Maven example of annotation preprocessing and generation of classes in same compile process?
12,515
Solution 1
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.
Solution 2
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>
Comments
-
Jérôme Verstrynge almost 2 years
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?
-
Jérôme Verstrynge over 12 yearsThanks, but honestly I think maven-antlr-plugin is overkill. Moreover, the site does not provide a complete example explaining how to solve the issue I raise. I am thinking more about something along the AnnotationProcessor.The maven-annotation-plugin looks interesting, but it is work-in-progress with incomplete documentation. Not helpful.
-
Admin over 12 yearsI don't think he was advocating using the antlr plugin directly, but looking at its source to see how it does what it does.
-
user1050755 about 8 yearsThere is a lot of good stuff in this answer. But one thing is NOT true: you can do it in one maven pass. Here is an example that also includes an annotation processor using JCodeModel: github.com/jjYBdx4IL/example-maven-project-setups/tree/master/…
-
Don Rhummy over 6 years@user1050755 Thanks for a great suggestion. One question: why did you use that JCodeModel and not the official one? github.com/javaee/jaxb-codemodel