Generated code not taken into account in maven compile process
Asked Answered
C

2

3

I have a maven project generating a new class from an annotation processor during a compile process. The class is successfully generated in /target/generated-sources/annotations/, but it is not compiled into the .jar. Why?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>${project.build.source}</source>
        <target>${project.build.target}</target>
        <encoding>${project.build.sourceEncoding}</encoding>
        <compilerArgument>-Xlint</compilerArgument>
        <annotationProcessors>
                <annotationProcessor>net.preprocessing.MyAnnotationProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>

EDIT

This seems to be a known bug. If anyone has an operational workaround, it is welcome.

EDIT 2

I have performed some tests yesterday, but the suggested workaround in the ticket does not work. I have provided a test case. If anyone has insight on how to solve this issue, it is welcome. Keep in mind that I am a newbie at annotation processing, so there might be something obvious I am missing.

Chemash answered 7/8, 2011 at 19:39 Comment(0)
P
6

I would suggest using the maven-processor-plugin instead of an 'annotationProcessor' argument which you pass to the maven compiler.

From what I've read there seem to be some issues with the compilerArgumens, those are solved when you use the maven-processor-plugin.

Here you can find more info about the maven-processor-plugin: http://maven-annotation-plugin.googlecode.com/svn/docs/usage.html

Here is an example of how the processor plugin replaces compilerArguments (the example uses Hibernate Metamodel Generator, but this will look about the same for all kinds of annotation processors...): http://relation.to/Bloggers/HibernateStaticMetamodelGeneratorAnnotationProcessor

Piccadilly answered 8/8, 2011 at 12:58 Comment(1)
The Hibernate example/workaround did it. I just had to update the directory to my generated source. Many many thanks !!!Polemics
S
1

It seems all you do is compile the classes and not build the jar. You need to use the Maven Jar Plugin This is a usage example.

Stortz answered 7/8, 2011 at 20:5 Comment(7)
A jar is built as part of the process, but it does not contain the generated code.Polemics
Oh then check out this thread https://mcmap.net/q/753189/-maven-producing-empty-jarStortz
Could you share your project structure / tree? Maven projet structures are supposed to be standard so there shouldn't be a problem.Stortz
Which plugin did you use to generate the classes? Can you give an example how it looks like? Furthermore the work-a-round for the problem is given in the JIRE Ticket description (build-helper-plugin)Contiguous
@Contiguous See download.oracle.com/javase/6/docs/technotes/tools/solaris/…, you don't use a plugin to generate the classes. The processor does it. I have tried the workaround, but it does not work. I put my finding and a test case in the ticket.Polemics
Have you bound the compiler plugin for the annotation processing to a different life-cylce like generate-sources ? It does not look like though...I think you have to have two execution blocks one which generating the things from the annotation processing and the other is really compiling everything and of course not to forget to add the supplemental folder (build-helper-plugin)Contiguous
@Contiguous Thanks for your suggestion. I have not tried it since the solution/workaround provided by fgysin worked. It think that what you mention should be part of the final fix to the maven-compiler-plugin.Polemics

© 2022 - 2024 — McMap. All rights reserved.