Java – manage JAXB generated classes in the Maven project
I have a maven based project in which I try to add some JAXB classes automatically generated by the "jaxb2 Maven plugin" Maven plug-in But my first clip puts me in a loop dependency loop:
>Because these JAXB classes have not been generated, there are compilation errors in other sources referencing them. > Because these other sources have compilation errors, these JAXB classes will not be generated
There seem to be two obvious possibilities to solve this problem:
Comment out broken references so that the project generates and automatically generates JAXB classes Then copy these generated sources from / target to / SRC / main / Java so that referencing them will not cause compilation errors. > Create a completely independent project that contains only JAXB Make it a dependency in my main project
Did I miss anything here? The options #1 look ridiculous, just the way people use JAXB Options #2 seem more rational, but still inefficient and cumbersome Do I really need to spend the cost of a completely independent project to use JAXB?
In the same project where Maven plug-ins generate them, do developers use more elegant methods to reference JAXB generated classes?
Update: upon request, here is the relevant part 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 the MVN clean package, I see that my JAXB source is generated under the / target subdirectory However, these generated sources are not automatically added to the classpath at compile time
Post solution update: it turns out that my compilation problem is related to the fact that I run in eclipse, and its Maven integration has some problems with "jaxb2 Maven plugin" For more details about this problem and its solution, see this stackoverflow question
Solution
I suggest that you split the JAXB generated class (API) and your BL class (Implementation) into two Maven projects, each with a separate POM XML, and has the main root POM. Com of compilation order xml. Then you can build an API Jar, Maven will install it in the local repo, and then you can use it as the dependency of your implementation So it looks like:
-API\ --pom.xml - for api,jaxb generation -IMPL\ --pom.xml - for impl,api dependency is here pom.xml - main pom.xml with references to the projects above