Thursday, 27 October 2011

JAXB Maven Plugin

It is used to generate Java classes from XML Schema (XSD). If you are using Maven, generating Java classes is very easy. Here is what you need to do: Add the following dependencies:

<dependency>
 <groupId>com.sun.xml.bind</groupId>
 <artifactId>jaxb-impl</artifactId>
 <version>2.2.4-1</version>
</dependency>
<dependency>
 <groupId>org.jvnet.jaxb2_commons</groupId>
 <artifactId>jaxb2-basics-runtime</artifactId>
 <version>0.6.0</version>
</dependency>
and then add the following under the build section in pom.xml:

<plugin>
 <groupId>org.jvnet.jaxb2.maven2</groupId>
 <artifactId>maven-jaxb2-plugin</artifactId>
 <version>0.8.0</version>
 <executions>
  <execution>
   <goals>
    <goal>generate</goal>
   </goals>
  </execution>
 </executions>
 <configuration>
  <extension>true</extension>
  <args>
   <arg>-XtoString</arg>
   <arg>-Xequals</arg>
   <arg>-XhashCode</arg>
  </args>
  <plugins>
   <plugin>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics</artifactId>
    <version>0.6.0</version>
   </plugin>
  </plugins>
  <schemaDirectory>${basedir}/src/main/xsd</schemaDirectory>
  <generateDirectory>${basedir}/src/main/java</generateDirectory>
 </configuration>
</plugin>
Please note that -XtoString, -Xequals, -XhashCode etc are the arguments passed to the XJC compiler which override the toString(), equals() and hashCode() methods respectively of the generated classes. schemaDirectory : The location to your XSDs. generateDirectory : Generated code will be placed under this package.

No comments:

Post a Comment