Jul 29 2009

maven-jaxb-plugin

Category: Java,JAXB,Maven,XMLRussell Pitre @ 8:25 pm

I’ve recently started an extranet based webapp that will be consuming a web service hosted on an intranet webapp.   On both the server and client sides i’m using Spring Web Services.  Spring WS provides a nice Object/XML (OXM) mapping abstraction layer that supports a few different oxm frameworks like JAXB, Castor, and XStream.  I haven’t had experience with any of them before this project so I chose JAXB only because it seems to be the most widely used.

A best practice is top-down, or contract first development when designing web services.  A really good place to start with SOA development is Web Service Contract Design and Versioning for SOA by Thomas Erl.

The first step i took was to define the data model for the messages with xml schema. I discovered a great Maven plugin to generate source code from the XSDs for the web service.  Here’s the snippet that i used in my pom.xml

<plugin>
	<groupId>com.sun.tools.xjc.maven2</groupId>
	<artifactId>maven-jaxb-plugin</artifactId>
	<executions>
		<execution>
			<phase>generate-sources</phase>
			<goals>
				<goal>generate</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<schemaDirectory>src/main/resources/com/mycompany/xml/schema</schemaDirectory>
		<includeSchemas>
			<includeSchema>**/*.xsd</includeSchema>
		</includeSchemas>
		<includeBindings>
			<includeBinding>**/*.xjb</includeBinding>
		</includeBindings>
		<verbose>true</verbose>
		<removeOldOutput>true</removeOldOutput>
	</configuration>
</plugin>

Leave a Reply