Red Hat
May 20, 2012
by Fred Bricon

Maven Integration for Eclipse JDT Annotation Processor Toolkit a.k.a m2e-apt is now available in its version 1.0. m2e-apt aims at providing automatic Annotation Processing configuration in Eclipse based on your project's pom.xml and its classpath dependencies  (Requires Java >= 1.6).

 

m2e-apt supports both annotation processing set on the maven-compiler-plugin or the maven-processor-plugin (the latter takes precedence over the former).

 

For example, to enable the JPA modelgen annotation processor, you can either set :

 

<plugin>
 
    <artifactId>
maven-compiler-plugin</artifactId>
 
    <version>
2.4</version>
 
    <configuration>
 
        <source>
1.6</source>
 
        <target>
1.6</target>
 
    </configuration>
 
    <dependencies>
 
        <dependency>
 
            <groupId>
org.hibernate</groupId>
 
            <artifactId>
hibernate-jpamodelgen</artifactId>
 
            <version>
1.2.0.Final</version>
 
        </dependency>
 
    </dependencies>
 
</plugin>
 

 

or

 

 

<plugin>
 
    <artifactId>
maven-compiler-plugin</artifactId>
 
    <version>
2.4</version>
 
    <configuration>
 
        <source>
1.6</source>
 
        <target>
1.6</target>
 
        <compilerArgument>
-proc:none</compilerArgument>
 
    </configuration>
 
</plugin>
 
<plugin>
 
    <groupId>
org.bsc.maven</groupId>
 
    <artifactId>
maven-processor-plugin</artifactId>
 
    <version>
2.0.5</version>
 
    <executions>
 
        <execution>
 
            <id>
process</id>
 
            <goals>
 
                <goal>
process</goal>
 
            </goals>
 
            <phase>
generate-sources</phase>
 
        </execution>
 
    </executions>
 
    <dependencies>
 
        <dependency>
 
            <groupId>
org.hibernate</groupId>
 
            <artifactId>
hibernate-jpamodelgen</artifactId>
 
            <version>
1.2.0.Final</version>
 
        </dependency>
 
    </dependencies>
 
</plugin>
 

 

The generated source folders (target/generated-sources/annotation for maven-compiler-plugin; target/generated-sources/apt for maven-processor-plugin) are automatically added to the project classpath.

 

m2e-apt.png

By default, Annotation Processing is managed by Eclipse JDT APT, so a change in your source classes triggers incremental processing automatically. The downside of using JDT APT is, there's no separation between main and test classes (the way maven-processor-plugin handles them).

To mitigate that limitation, you can change the workspace or project preferences to delegate annotation processing to maven, instead of JDT APT. This will result in slower incremental builds (all classes will be processed) but will provide identical results to maven command line builds. Note this only works when using maven-processor-plugin. When annotation processing is driven by maven-compiler-plugin, JDT APT is always used in eclipse.

 

Automatic annotation processing configuration from the maven pom.xml can also be disabled altogether. In this case, your manual settings for Eclipse JDT APT will remain untouched.

 

Go to Window > Preferences > Maven > Annotation processing or right-click on your project > Properties > Maven > Annotation processing to select the Annotation Processing strategy of your choice.

 

m2e-apt-prefs.png

m2e-apt is heavily based on the work from two community members :

  • a patch for m2e by Karl M. Davis, from Knowledge Computing Corp, implementing Eclipse JDT APT configuration based on the maven-compiler-plugin's configuration.
  • com.excilys.ebi.m2e.apt   by St&eacute;phane Landelle, from eBusiness Information, Excilys Group, implementing direct maven-processor-plugin invocation from m2e.

 

m2e-apt is available :

 

Source code is available at github . If you find any bug, please raise a ticket on m2e-apt's issue tracker .

 

Happy hacking,

 

Fred

https://twitter.com/#!/fbricon