Maven JAXB plugin for code generation from XSD
We can use Maven JAXB 2.1 Plugin to easily generate JAXB2 code from XSDs at build.
Here are the maven dependencies for JAXB at pom.xml:
<dependencies> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> </dependency> </dependencies>
and here is the JAXB xjc plugin settings at pom.xml, of course you can change the output directory configuration, I wanted to generate the code at build to target/generated-sources folder:
<build>
<plugins>
<!-- JAXB xjc plugin that invokes the xjc compiler to compile XML schema into Java classes.-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- The schema directory or xsd files. -->
<schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
<!-- The package in which the source files will be generated. -->
<packageName>com.mypackage</packageName>
<!-- The working directory to create the generated java source files. -->
<outputDirectory>${build.directory}/generated-sources/jaxb</outputDirectory>
</configuration>
</plugin>
</plugins>
As a side note for code generation: code generation convention is using “target/generated-sources/” folder, it is added as source folder by default.
After adding your XSD file at “resources/xsd” you can generate your code with mvn jaxb2:xjc or mvn clean install commands.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Leave a comment