writing JAXB extension plugins

Thursday, November 01, 2012

JAXB extension plugins (1) give the user more power than simple binding alterations, plus can encourage clean schemas by keeping the code extensions out of the xsd's. But finding up-to-date documentation on this can be a pain, most predates maven.

In hindsight, the process is actually quite simple and there are some good references, but it still takes collation of quite a few sites to make it work. Here I hope to capture them all for a cohesive approach.

Note that 'plugin' is a ubiquitous term and here can mean many things. For disambiguation I've added a postscript number to each use of the term. See footnotes at the bottom for meanings.

JAXB runtime

This post assumes you're using maven. And if so, the JAXB plugin (2) to use is maven-jaxb2-plugin. Add the latest version to your pom http://mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/maven-jaxb2-plugin. Then get your schema generation working OK as documented plenty of places elsewhere on the web. If you're looking at this page then you're probably well past that step anyway.

Plugin project skeleton

Create a new maven project to hold your plugin (1). For this example we'll call it my-jaxb-plugin.

In the pom add a dependency on jaxb xjc, eg:
<dependency>
<groupid>com.sun.xml.bind</groupid>
<artifactid>jaxb-xjc</artifactid>
<version>2.2.6</version>
</dependency>

Create a new class, call it MyPlugin1 in package com.eg, extending from com.sun.tools.xjc.Plugin. For now just implement the required methods returning "XmyJaxb" from getOptionName(), and implement run() as System.out.println("***working!"); (or similar :).

Now create new folders META-INF/services on the classpath (eg src/main/resources). Add a text file in there called com.sun.tools.xjc.Plugin.In this file is a list of all the plugins that this project provides, one per line. So for now add your new plugin class:
com.eg.MyPlugin1

Run a maven install on the project to make it available (not needed later if you use workspace resolution with eclipse maven plugin (3))

Employ the plugin (1)

Return to the pom of your original JAXB project. For the plugin (2) definition for maven-jaxb2-plugin, add or edit a <configuration> tag. Set <extension> to true, and add an <args> section with <arg>-debug> and <arg>-XmyJaxb>. Then a nested <plugins><plugin> definition pointing to your plugin project. The result should be something like:
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-debug</arg>
<arg>-XmyJaxb</arg>
</args>
<plugins>
<plugin>
<groupId>nz.govt.police</groupId>
<artifactId>esbschema-jaxb-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
Maven build your JAXB project and your should see in the console output your ***working! comment. Congrats
The -debug arg can be removed later as needed. You may also find it useful to use -X after your mvn install command to get verbose output from the maven build.

More

For taking your plugin (1) further, read the key post from 2005, resources incl javadoc for jaxb-xjc, and the sun codemodel javadocs which you'll be using to modify the code generation.

Footnotes

(1) A JAXB plugin - the reason you're reading this!
(2) A plugin to the maven build tool
(3) A plugin to the eclipse IDE