Tuesday, February 5, 2008

Schemagen maven plugin for jaxb

So you've annotated all those java classes and now you want to pump out an xsd. Oh wait, you gotta do this in maven. Well, you're in luck, there is a schemagen maven plugin (see here), but it's not documented very well.
Here's a working example of using the plugin in your pom file:

<plugins>
.........
<plugin>
<groupId>com.sun.tools.jxc.maven2</groupId>
<artifactId>maven-jaxb-schemagen-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<destdir>${project.build.directory}/classes</destdir>
<schemas>
<schema>
<namespace>http://www.company.com/ILikeRest</namespace>
<file>api.xsd</file>
</schema>
</schemas>
<excludes>
<exclude>**/*Adapter.java</exclude>
</excludes>
<includes>
<include>**/com/company/jaxb/*</include>
</includes>
<srcdir>${project.build.sourceDirectory}</srcdir>
<verbose>true</verbose>
</configuration>
</plugin>
.........
</plugins>


Here I'm dropping it into the classes directory where it will end up in my artifact (a jar file in this case), so the xsd will travel with the jaxb objects inside the jar.