Sunday, April 19, 2009

Maven JAXB Generation Part II

I've already done a posting on using Maven to generate JAXB with the Fluent API and this adds making the JAXB beans serializable and setters for lists. Add the file jaxb-bindings.xjb to the 'src/main/resources' with the config below and JAXB beans will implement serializable.

Also by default, collections only have getters. The JAXB 2.1 Collection setter Injector Plugin will also generate a setter for any collection. I found this useful while serializing JAXB beans between Java and ActionScript with BlazeDS (while using Spring BlazeDS Integration. BlazeDS only serializes something if it has a getter and a setter. Unfortunately I couldn't find this in a Maven repo, but you can install it locally or into your Nexus server.




<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xfluent-api</arg>
<arg>-Xcollection-setter-injector</arg>
</args>
<schemaDirectory>src/main/resources</schemaDirectory>
<plugins>
<plugin>
<groupId>net.java.dev.jaxb2-commons</groupId>
<artifactId>jaxb-fluent-api</artifactId>
<version>2.1.8</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<!-- Had to manually install in repo, not in main java.net repo. -->
<dependency>
<groupId>org.jvnet.jaxb2-commons</groupId>
<artifactId>collection-setter-injector</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>

src/main/resources/jaxb-bindings.xjb
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<jxb:bindings>
<jxb:globalBindings>
<jxb:serializable/>
</jxb:globalBindings>
</jxb:bindings>

</jxb:bindings>