Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing JAXB-generated classes in a Maven project

I have a Maven-based project, in which I trying to add some JAXB classes automatically generated by the "jaxb2-maven-plugin" Maven plugin. However, my first cut has me in a circular dependency loop:

  • Because these JAXB classes aren't generated yet, my other sources which reference them have compilation errors.
  • Because those other sources have compilation errors, these JAXB classes don't get generated.

It seems like there are two obvious possibilities for solving this:

  1. Comment-out the broken references, so that the project builds and the JAXB classes are automatically generated. Then copy those generated sources from /target into /src/main/java, so that references to them won't cause compilation errors.
  2. Create an entirely separate project, consisting of nothing but the JAXB stuff. Include it as a dependency in my main project.

Am I missing something here? Option #1 seems flat-out ridiculous... that just can't be the manner in which people use JAXB. Option #2 seems more rational, but still rather inefficient and cumbersome. I really have to take on the overhead of an entirely separate project just to use JAXB?

Are there any more elegant approaches that developers use to reference JAXB-generated classes in the same project where the Maven plugin generates them?

UPDATE: By request, here is the relevant portion of my POM:

<build>
    <plugins>
        <plugin>
            <!-- configure the compiler to compile to Java 1.6 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>       
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- The name of your generated source package -->
                <packageName>com.mypackage</packageName> 
            </configuration>
        </plugin>
    </plugins>  
</build>

When I run mvn clean package, I DO see my JAXB sources being generated beneath the /target subdirectory. However, those generated sources are not being automatically added to the classpath for the compile phase.

POST-RESOLUTION UPDATE: It turns out that my compilation issues had more to do with the fact that I was running in Eclipse, and its Maven integration has some issues with "jaxb2-maven-plugin". See this StackOverflow question for more detail on that issue and its resolution.

like image 476
Steve Perkins Avatar asked Aug 15 '12 15:08

Steve Perkins


People also ask

How do I generate Java classes from XSD using JAXB maven?

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.

What is Maven JAXB2 plugin?

This plugin uses the Java API for XML Binding (JAXB), version 2+, to generate Java classes from XML Schemas (and optionally binding files) and to create XML Schemas from annotated Java classes.

What does Mvn generate sources do?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.


1 Answers

How did you configure your jaxb maven plugin? Normally it runs in the generate-sources lifecycle, which comes before the compile lifecycle. So your JAXB generated classes should already be there when your own code gets compiled, Maven puts them in target/generated-source and puts that folder on the classpath.

Edit: This is my code we use at work (and which works as expected):

<plugin>
            <groupId>com.sun.tools.xjc.maven2</groupId>
            <artifactId>maven-jaxb-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>src/main/resources/<companyname>/xsd</schemaDirectory>
                <includeSchemas>
                    <includeSchema>retrieval.xsd</includeSchema>
                    <includeSchema>storage.xsd</includeSchema>
                </includeSchemas>
            </configuration>
        </plugin>

Apparently we use yet another jaxb plugin... (see also this thread: Difference of Maven JAXB plugins).

like image 195
Nicolas Mommaerts Avatar answered Oct 16 '22 20:10

Nicolas Mommaerts