maven jaxb plugin configuration to generate XSD schema files

Here is how to configure your project in order to generate XSD schema files from Java JAXB annotated classes

<plugin>
 <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.5</version>
  <executions>
   <execution>
    <goals>
     <goal>schemagen</goal>
    </goals>
    <phase>generate-sources</phase>
    <configuration>
     <outputDirectory>${project.build.directory}/schemas</outputDirectory>
     <workDirectory>${project.build.directory}/generated-sources/jaxb</workDirectory>
    <includes>
     <include>${path_to_pojo_package}/*.java</include>
    </includes>
    <transformSchemas>
     <transformSchema>
      <uri>${namespace}</uri>
      <toFile>${xsd_filename}</toFile>
     </transformSchema>
    </transformSchemas>
   </configuration>
  </execution>
 </executions>
</plugin>

This will generate an XSD file named ${xsd_filename} in the target/schemas folder.

We chose the generate-source phase in order to make the schema file available for the sources (particularly for the test sources), in conjunction with the following resources configuration to add the schema to the generated artifact:

<resources>
 <resource>
  <directory>src/main/resources</directory>
 </resource>
 <resource>
 <directory>${project.build.directory}/schemas</directory>
  <targetPath>schemas</targetPath>
 </resource>
</resources>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s