Thursday, March 8, 2012

Configuring maven to autogenerate Serializable JAXB classes

 First you need to create a bindings file:

<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<jxb:globalBindings>
  <jxb:serializable uid="1"/>
</jxb:globalBindings>

</jxb:bindings>
Save this file under "src/main/resources/" of your maven module. You can name it whatever you want e.g. jaxb-bindings.xjb.

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      <id>Request</id>
      <goals>
      <goal>generate</goal>
      </goals>
      <configuration>
      <schemaDirectory>src/main/resources/xsd</schemaDirectory>
      <schemaIncludes>
      <include>*/*.xsd</include>
      </schemaIncludes>
      <generatePackage>mygeneratedfiles</generatePackage>
      <generateDirectory>src/main/java</generateDirectory>
      <bindingDirectory>src/main/resources</bindingDirectory>
      <bindingIncludes>
      <bindingInclude>jaxb-bindings.xjb</bindingInclude>
      </bindingIncludes>
      </configuration>
    </execution>
  </executions>
</plugin>