Skip to content

Combining PojoBuilder and JAXB

drekbour edited this page Mar 4, 2019 · 1 revision

Brief example showing how to generate PojoBuilders for JAXB-generated classes (using Maven). All snippets are summarised from https://github.com/drekbour/pojobuilder-jaxb-example.

Maven pom.xml

The following plugin definition enables xjc code-generation with the Annox plugin that can add or remove annotations.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Xannotate</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>1.1.0</version>
            </plugin>
        </plugins>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>net.karneim</groupId>
            <artifactId>pojobuilder</artifactId>
            <version>4.2.2</version>
        </dependency>
    </dependencies>
</plugin>

JAXB bindings.xjb

Add the PojoBuilder definition using annox:annotate:

<jaxb:bindings
        version="2.1"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:annox="http://annox.dev.java.net"
        jaxb:extensionBindingPrefixes="annox">
    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="a.b.c.model"/>
        </jaxb:schemaBindings>
        <jaxb:bindings node="//xs:complexType[@name='Person']">
            <annox:annotate>@net.karneim.pojobuilder.GeneratePojoBuilder(withCopyMethod=true, withFactoryMethod="*")</annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>