Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven 3 - How to add annotation processor dependency?

I need to run an annotation processor on my project's sources. The annotation processor should not become a transitive dependency of the project since it's only needed for annotation processing and nothing else.

Here is the complete (non-working) test pom I use for this:

<project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>test</groupId>   <artifactId>test</artifactId>   <packaging>jar</packaging>   <version>1.0-SNAPSHOT</version>   <name>Test annotations</name>   <properties>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     <hibernate-jpamodelgen.version>1.2.0.Final</hibernate-jpamodelgen.version>   </properties>   <dependencies>     <dependency>       <groupId>javax</groupId>       <artifactId>javaee-api</artifactId>       <version>6.0</version>       <scope>provided</scope>     </dependency>   </dependencies>   <build>     <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-compiler-plugin</artifactId>         <version>3.0</version>         <configuration>           <annotationProcessors>             <annotationProcessor>               org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>           </annotationProcessors>           <debug>true</debug>           <optimize>true</optimize>           <source>1.6</source>           <target>1.6</target>           <compilerArguments>             <AaddGeneratedAnnotation>true</AaddGeneratedAnnotation>             <Adebug>true</Adebug>           </compilerArguments>         </configuration>         <dependencies>           <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-jpamodelgen</artifactId>             <version>${hibernate-jpamodelgen.version}</version>           </dependency>         </dependencies>       </plugin>     </plugins>   </build> </project> 

I explicitly defined org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor as an annotation processor in the plugin configuration for tests and I know it shouldn't be required.

The problem I'm encountering is that the hibernate-jpamodelgen dependency is not added to the compiler classpath so the annotation processor is not found and the build fails.

As per this answer, I tried adding the dependency as a build extension (not sure I understand what those are supposed to be!) like so:

<extensions>   <extension>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-jpamodelgen</artifactId>     <version>${hibernate-jpamodelgen.version}</version>   </extension> </extensions> 

This also doesn't add hibernate-jpamodelgen to the compiler classpath.

The only thing I found which works so far is adding the dependency to the project in the <dependencies> section. This has the unfortunate side-effect of adding hibernate-jpamodelgen as a transitive dependency afterwards which I want to avoid.

My previous working setup uses the maven-processor-plugin plugin to achieve what I want. However, this plugin is not supported by eclipse m2e and the latest version of the maven-compiler-plugin now handles multiple compiler arguments properly so I'd rather use the latter.

like image 672
bernie Avatar asked Jan 14 '13 16:01

bernie


People also ask

How do I run an annotation processor?

Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Enable Annotation Processing.

What is annotation processing tool?

Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.

What is Maven compiler plugin in POM XML?

The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle: compile – compile main source files. testCompile – compile test source files.

What is the default value of failOnError while configuring plugin parameters?

failOnError. Indicates whether the build will continue even if there are compilation warnings. Default value is: false.


2 Answers

The annotationProcessorPaths option can be used in recent versions of the Maven compiler plug-in:

<pluginManagement>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-compiler-plugin</artifactId>             <version>3.6.1</version>             <configuration>                 <annotationProcessorPaths>                     <annotationProcessorPath>                         <groupId>org.hibernate</groupId>                         <artifactId>hibernate-jpamodelgen</artifactId>                         <version>5.2.6.Final</version>                     </annotationProcessorPath>                 </annotationProcessorPaths>             </configuration>         </plugin>     </plugins> </pluginManagement> 

That way the processor is separated from the actual project dependencies. This option is also picked up by the Eclipse M2E plug-in if annotation processing is enabled for the project.

like image 89
Gunnar Avatar answered Sep 17 '22 01:09

Gunnar


Add the dependency as an optional dependency (<optional>true</optional>). This will add the dependency under compilation, but will prevent it for being a transitive dependency:

<dependency>   <groupId>org.hibernate</groupId>   <artifactId>hibernate-jpamodelgen</artifactId>   <version>${hibernate-jpamodelgen.version}</version>   <optional>true</optional> </dependency> 

If you're creating an artifact in this module with all your dependencies in it (like a .war), you may use the <scope>provided</scope> instead. This both prevents the dependency to be transitive and to be included in the artifact the module produces.

like image 23
Aleksander Blomskøld Avatar answered Sep 19 '22 01:09

Aleksander Blomskøld