12

Are you aware of any good JAXB Plugin which generated Builder pattern classes for the generated JAXB classes? Composing domain using JAXB generated classes is really nasty. I saw a plugin someone wrote back in 2010 but it doesn't use the newest maven plugin jaxb2-maven-plugin, and it also requires you to specify bindings for each schema type which is not robust.

Asaf Mesika
  • 1,533
  • 4
  • 17
  • 33
  • Did you mean's generate the ObjectFactory class? – Liping Huang Dec 10 '13 at 08:18
  • 1
    If `jaxb-fluent-api` could be somehow configured with `cxf-xjc-plugin` then it might make for a great solution but I'm not sure if that's even possible. Other than that it sounds like you may have already stumbled upon retepTools here: http://blog.retep.org/2010/05/18/implementing-builders-with-jaxb-generated-objects/2/ – pulkitsinghal Dec 12 '13 at 23:17
  • This plugin generates immutable classes and optionally builders - https://github.com/sabomichal/immutable-xjc – Michal Sabo Apr 30 '17 at 21:09

2 Answers2

10

Yes, there is now a plugin to generate fluent builders for JAXB-generated classes. There is a github project on

https://github.com/mklemm/jaxb2-rich-contract-plugin

It contains a couple of useful JAXB plugins. You can download source and binaries from github, or get maven artifacts from The Central Repository

Hope this helps. If you have any questions, just ask me, I'm the one who started it.

Mirko Klemm
  • 1,928
  • 1
  • 22
  • 22
4

The following snippet from a pom.xml file, uses maven cxf-xjc-plugin to generate the JAXB classes and also leverages jaxb-fluent-api to tack-on fluent interfaces ... which aren't exactly a complete builder pattern on their own ... but I think they leave room for folks to make decent headway in that direction.

        <!-- Used to generate source code based on XSD (schema) file -->
        <!-- http://cxf.apache.org/cxf-xjc-plugin.html -->
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-xjc-plugin</artifactId>
            <version>2.7.7</version>
            <configuration>
                <extensions>
                    <extension>net.java.dev.jaxb2-commons:jaxb-fluent-api:2.1.8</extension>
                </extensions>
            </configuration>
            <executions>
                <execution>
                    <id>generate-xsd-sources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xsdtojava</goal>
                    </goals>
                    <configuration>
                        <sourceRoot>${basedir}/target/generated-sources/cxf-xjc/</sourceRoot>
                        <xsdOptions>
                            <xsdOption>
                                <xsd>${basedir}/src/main/wsdl/your.xsd</xsd>
                                <packagename>com.your.package.name</packagename>
                                <extensionArgs>
                                    <extensionArg>-Xfluent-api</extensionArg>
                                </extensionArgs>
                            </xsdOption>
                        </xsdOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
pulkitsinghal
  • 3,273
  • 8
  • 40
  • 72