jaxb2-annotate-plugin: Adding annotations to the XJC classpath

2.8k views Asked by At

Is it possible to add custom annotations to the XJC classpath, while they are defined within my project itself? This when using the maven jaxb2-annotate-plugin.

The problem regards this piece of the documentation:

Annotation classes must be known in compile time. I.e. annotation classes must be made available in the XJC classpath. If you want to use your own annotations, you have to pre-compile them and add your annotation classes to the XJC classpath.

When I make a seperate project and add it to the plugin, it works fine as shown in the documentation examples.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.3</version>
    <executions>
        <execution>
            <id>generate</id>
            <phase>process-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>1.0.2</version>
                    </plugin>
                    <plugin>
                        <groupId>com.acme</groupId>
                        <artifactId>external-project</artifactId>
                        <version>1.0.0</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
</plugin>

But I'd like to use the annotation that resides in the same project. How do I let it get picked up? If I compile it in a stage before process-resources, with the following piece:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <executions>
        <execution>
            <id>compile</id>
            <phase>process-resources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>  
    </executions>
</plugin>

The class gets compiled before the generation from my XSD's, but I still get the exception AnnotationClassNotFoundException. I'd like to avoid making seperate projects and/or modules just for adding annotations. How come it can't find my classes and the only way to provide annotations seems to be external projects/modules?

2

There are 2 answers

0
Jaims On BEST ANSWER

I have found the solution to my problem.

When configuring the maven-jaxb2-plugin plugin, it's possible to provide dependencies for that plugin. When providing my own project as the dependency, it can find the classes for it. So the following works out:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.1</version>
    <executions>
        <execution>
            <id>generate</id>
            <phase>process-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>1.0.2</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>current-project</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</plugin>

It is required for your annotations to be compiled beforehand however. So to compile it beforehand, the following plugin should be added before the maven-jaxb2-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <executions>
        <execution>
            <phase>process-resources</phase>
            <id>compile</id>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
0
Albin. Com. On

The problem with that is the first time of mvn clean install. What if you haven't compiled it yet? For example go to your .m2 or mavel-local-repo and cancel your project and then do mvn clean install and see if it fails