Friday, November 21, 2008

generating serializable classes with jaxb-maven2-plugin

So I told my pal Danny about the power and the glory that is the JAXB plugin for Maven2 and the sly way you can generate an xsd from an xml exemplar.

He had a lot of fun then he said: "how can I make the generated classes implement serializable?"

To which I responded "..."

Mystery!

Turns out, it is super easy! You can use an xjb file like so:

<jxb:bindings
version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc"
>
<jxb:bindings schemaLocation="myCool.xsd" node="/xs:schema">
<jxb:globalBindings>
<xjc:serializable uid="-6026937020915831338"/>
</jxb:globalBindings>
</jxb:bindings>
</jxb:bindings>

I ripped this off from here...

Just dump it in yer src/main/resources directory and modify yer pom.xml to look something like:

<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generatePackage>com.cashcow.payme.xml</generatePackage>
<extension>true</extension>
</configuration>
</plugin>

The end! Go, JAXB! Go, Maven!

No comments:

Post a Comment