There is a useful maven plugin to generate all our JAXB classes for us. This plugin can be executed during the generate-sources phase of our maven build. It can be very useful, especially in the first stages of our project when the design of our XSD may change frequently. We can just add the plugin configuration to the pom.xml of our project.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>

    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Xfluent-api</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>net.java.dev.jaxb2-commons</groupId>
                <artifactId>jaxb-fluent-api</artifactId>
                <version>2.1.8</version>
            </plugin>
        </plugins>
        <bindingDirectory>src/main/resources/</bindingDirectory>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

We can use the Fluent Api plugin to provide method chaining for bean setter, which makes it easier for us to create bean instances . See: http://java.net/projects/jaxb2-commons/pages/Fluent-api. To customize our JAXB generation we specify a bindingdirectory. In this bindingdirectory we can put a .xjb file which contains our specific JAXB generation settings.

<jxb:bindings version="1.0"
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  jxb:extensionBindingPrefixes="xjc">

    <jxb:bindings schemaLocation="OurTestXsd.xsd" node="/xs:schema">
        <jxb:schemaBindings>
        <jxb:package name="nl.jdriven.sample.domain">
             </jxb:package>
            <jxb:nameXmlTransform>
        <jxb:typeName prefix="XML"/>
        <jxb:anonymousTypeName prefix="XML"/>
            </jxb:nameXmlTransform>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>

In the jxb:bindings schemaLocation we refer to our xsd file as source for our generated JAXB classes. To specify the package prefix we can use the jxb:package tag . We can use the jxb:nameXmlTransform tag to customize the name of our (anonymous) types.

shadow-left