Tuesday, April 6, 2010

Maven JAXB 2.0 schemagen goal

I've been building a RESTful API client using Sun's jersey JAX-RS reference implementation. My approach has been to create a domain model out of Java POJOs. It felt like wasted effort to create an XSD that was going to be derived from a fluid RESTful API that I have no control over. It's easier to use the JAXB annotations to control marshalling and unmarshalling behavior from API calls. The XSD seemed like an artifact that added no value... Except that I now have a use case where customers would like to provide me with XML data to transmit up to said API. Life is easier if customers can provide me with the data in a format I can just get for free. In order to create a data specification, I wanted to generate an XSD from my domain model using schemagen. Figuring this is something I'll want to do on a regular basis, and it's something other developers on the team may need to do in the future, I decided to use the Maven JAXB schemagen plugin, which is very poorly documented. However, I found a sample POM configuration that got me through quickly. Thanks to Mark for sharing! Just in case, I'll repost here.


<pluginrepositories>
<pluginrepository>
<id>maven-repository.dev.java.net</id>
<name>Java.net Maven 2 Repository</name>
<url>http://download.java.net/maven/2</url>
</pluginrepository>
</pluginrepositories>

<plugin>
<groupid>com.sun.tools.jxc.maven2</groupid>
<artifactid>maven-jaxb-schemagen-plugin</artifactid>
<version>1.2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>

<configuration>
<project>${project}</project>
<destdir>${project.build.directory}/schemas</destdir>
<srcdir>${project.build.sourceDirectory}/za/co/jumpingbean/dal/model/core/</srcdir>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupid>javax.xml.bind</groupid>
<artifactid>jaxb-api</artifactid>
<version>2.2</version>
</dependency>
<dependency>
<groupid>com.sun.xml.bind</groupid>
<artifactid>jaxb-xjc</artifactid>
<version>2.2</version>
</dependency>
<dependency>
<groupid>com.sun.xml.bind</groupid>
<artifactid>jaxb-impl</artifactid>
<version>2.2</version>
</dependency>
<dependency>
<groupid>com.sun.xml.bind</groupid>
<artifactid>jaxb-xjc</artifactid>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>