maven-processor-plugin configuration

5,037 views
Skip to first unread message

Jader Santos

unread,
Oct 14, 2015, 10:52:23 AM10/14/15
to mapstruct-users
Hi all,

First of all, congratulations. MapStruct is a great tool! 

In my project I'm using jpa modelgen to create a metamodel for CriteriaBuilder, and I trying to put the two annotatation processors (JPAMetaModelEntityProcessor, MappingProcessor) to work together. However, it doesn't work when maven processor plugin has two executions. I made an attempt to configure it in maven-compiler-plugin, as an annotationProcessor like this:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<configuration>
<annotationProcessors>
<annotationProcessor>org.mapstruct.ap.MappingProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>

It worked in the packaged war, but didn't work with Eclipse hot deploy to Jboss AS, due to lifecycle.

Is there any alternative?

Thanks.

Andreas Gudian

unread,
Oct 14, 2015, 1:47:03 PM10/14/15
to Jader Santos, mapstruct-users
2015-10-14 16:52 GMT+02:00 Jader Santos <jade...@gmail.com>:
Hi all,

First of all, congratulations. MapStruct is a great tool! 

Hi Jader, thanks for the praise! That's always nice to hear! :-)
 
I had the exact same use case in a former project of mine, and you can just use the JPAMetaModelEntityProcessor and the MappingProcessor in the same execution of the maven-processor-plugin by adding two <processor> tags to the maven-processor-plugin configration and both dependencies to the plugin's dependencies.

Alternatively, you can add both dependencies with scope provided and let the maven-compiler-plugin pick the processors up automatically (which comes with other drawbacks, though, including polluting your classpath, and m2e not configuring the Eclipse APT automatically).

Cheers,
Andreas

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jader Santos

unread,
Oct 14, 2015, 2:27:49 PM10/14/15
to mapstruct-users, jade...@gmail.com
Using the two processors in one execution cause an error:

[ERROR] Failed to execute goal org.bsc.maven:maven-processor-plugin:2.2.4:process (process) on project: Error executing: error d
uring compilation -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.bsc.maven:maven-processor-plugin:2.2.4:process (process) on project: Error executing
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error executing
        at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.execute(AbstractAnnotationProcessorMojo.java:292)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
        ... 20 more
Caused by: java.lang.Exception: error during compilation
        at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.executeWithExceptionsHandled(AbstractAnnotationProcessorMojo.java:548)
        at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.execute(AbstractAnnotationProcessorMojo.java:285)
        ... 22 more

My pom.xml:

<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>${processor.plugin.version}</version>
<dependencies>
<!-- Annotation processor to generate the JPA 2.0 metamodel classes 
for typesafe criteria queries -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${modelgen.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.basedir}/src/main/metamodel/</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
<processor>org.mapstruct.ap.MappingProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>

Jader Santos

unread,
Oct 15, 2015, 11:19:12 AM10/15/15
to mapstruct-users, jade...@gmail.com
It seems that the problem is related with lifecycle phase. The mapstruct processor tries to compile, but in generate-sources phase, other classes are being generated, therefore the code is not compilable. 

Another doubt is how to use the plugin in eclipse to hot deploy in JBoss. In the compiled war the classes MapperImpl are present, but if it is not in source folder, they are not included in JBoss deploy folder to run inside Eclipse. Any suggestion?

Andreas Gudian

unread,
Oct 15, 2015, 2:04:02 PM10/15/15
to Jader Santos, mapstruct-users
Javac in -proc:only mode (as invoked by the maven-processor-plugin) doesn't compile and can operate on non-compiled and even partially erroneous code. For example, the entity classes that you want to use in the MapStruct mapper could contain references to types that are yet to be generated by the JPA metamodel generator. So that should be ok, conceptually.
What exactly is the compile error you face? Could it be that the cxf plugin needs to run first? You can control the order in which the plugins are executed with their order within the <build><plugins> list in case they are bound to the same phase.

Regarding the hot-deploy, m2e should add the target/generated-sources/.. directories as source-folders and changes to mapper interfaces should lead to automatic incremental re-generation and compilation. Just like any other code change. Or are the sources from target/generated-sources not automatically added as source folders for the Jboss deployment assembly? That would be an issue with the respective m2e bridge for your packaging type... :-/


Jader Santos

unread,
Oct 16, 2015, 8:29:16 AM10/16/15
to mapstruct-users, jade...@gmail.com
The only way that I managed to make it work was configure the mapstruct processor in compile phase, after other processors run and generate code. Regarding hot deploy, Eclipse uses its own compiler, so the solution is to use the maven-helper-plugin to add generated mapstruct classes as source folder. Thanks.  

Filipe Sousa

unread,
Oct 27, 2015, 1:24:34 PM10/27/15
to mapstruct-users, jade...@gmail.com
Hi Jader,

I'm using the maven compiler plugin without any problems when using version 3.1

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>

<annotationProcessors>
<annotationProcessor>org.mapstruct.ap.MappingProcessor</annotationProcessor>
</annotationProcessors>
        <showWarnings>true</showWarnings>
</configuration>
</plugin>

I'm using IntelliJ IDEA that detects this configuration. 
Reply all
Reply to author
Forward
0 new messages