Duplicate class exception when creating two Builders with jaxb2-rich-contract-plugin

570 Views Asked by At

I use the maven jaxb plugin "jaxb2-rich-contract-plugin" (https://github.com/mklemm/jaxb2-rich-contract-plugin) to generate java classes. I want to be able to use the builder pattern so I use the plugin "fluent-builder".

I have two XSDs and I want to create one builder from each XSD.

In the first version I use one common configuration for the two XSDs and get an exception at start up: "java: duplicate class: com.kscs.util.jaxb.Copyable". If I remove the copy-plugin I get the exception "java duplicate class: com.kscs.util.jaxb.Buildable".

In the second version I have one configuration for each XSD/execution but I still get the same exception at start up: "java duplicate class: com.kscs.util.jaxb.Buildable". As visible in the configuration for the extended-order I try in many different ways to rename the builder and other stuff but it has no effect.

Version 1:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <extensions>true</extensions>
    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Ximmutable</arg>
            <arg>-Xcopy</arg>
                <arg>-partial=n</arg>
                <arg>-generateTools=y</arg>
                <arg>-constructor=n</arg>
                <arg>-narrow=y</arg>
            <arg>-Xfluent-builder</arg>
                <arg>-copyPartial=n</arg>
                <arg>-narrow=y</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>net.codesup.util</groupId>
                <artifactId>jaxb2-rich-contract-plugin</artifactId>
                <version>2.0.1</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
            <id>generate-order</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaIncludes>
                    <include>xsd/order.xsd</include>
                </schemaIncludes>
                <bindingIncludes>
                    <include>xsd/bindings.xjc</include>
                </bindingIncludes>
                <generatePackage>com.company.order.model.xsd</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/xsd</generateDirectory>
            </configuration>
        </execution>
        <execution>
            <id>generate-extended-order</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaIncludes>
                    <include>xsd/extended.xsd</include>
                </schemaIncludes>
                <bindingIncludes>
                    <include>xsd/bindings.xjc</include>
                </bindingIncludes>
                <generatePackage>com.company.order.model.extended.xsd</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/extended-xsd
                </generateDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

Version 2:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <extensions>true</extensions>
    <configuration>
        <extension>true</extension>
        <plugins>
            <plugin>
                <groupId>net.codesup.util</groupId>
                <artifactId>jaxb2-rich-contract-plugin</artifactId>
                <version>2.0.1</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
            <id>generate-order</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Ximmutable</arg>
                    <arg>-Xcopy</arg>
                        <arg>-partial=n</arg>
                        <arg>-generateTools=y</arg>
                        <arg>-constructor=n</arg>
                        <arg>-narrow=y</arg>
                    <arg>-Xfluent-builder</arg>
                        <arg>-copyPartial=n</arg>
                        <arg>-narrow=y</arg>
                </args>
                <schemaIncludes>
                    <include>xsd/order-capture.xsd</include>
                </schemaIncludes>
                <bindingIncludes>
                    <include>xsd/bindings.xjc</include>
                </bindingIncludes>
                <generatePackage>com.company.order.model.xsd</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/xsd</generateDirectory>
            </configuration>
        </execution>
        <execution>
            <id>generate-extended-order</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <args>
                    <arg>-Ximmutable</arg>
                    <arg>-Xfluent-builder</arg>
                        <arg>-copyPartial=n</arg>
                        <arg>-narrow=y</arg>
                        <arg>-builderClassName=extendedBuilder</arg>
                        <arg>-newCopyBuilderMethodName=extencedMethod</arg>
                        <arg>-builderInterfaceName=extendedInterface</arg>
                        <arg>-builderFieldSuffix=extendedSuffix</arg>
                        <arg>-newBuilderMethodName=extendedName</arg>
                        <arg>-buildMethodName=extendedBuildName</arg>
                </args>
                <schemaIncludes>
                    <include>xsd/extended.xsd</include>
                </schemaIncludes>
                <bindingIncludes>
                    <include>xsd/bindings.xjc</include>
                </bindingIncludes>
                <generatePackage>com.company.order.model.extended.xsd</generatePackage>
                <generateDirectory>${project.build.directory}/generated-sources/extended-xsd
                </generateDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
1

There are 1 best solutions below

0
On

I was able to solve this issue similar to what you've written, with excluding the tools in all but one execution:

<args>
    <arg>-Xfluent-builder</arg>
    <arg>-generateTools=n</arg>
</args>

In the general configuration, I have declared defaults for all schemas:

<configuration>
    <extension>true</extension>
    <args>
        <arg>-Xfluent-builder</arg>
    </args>

In the meantime, a new version of the contract-plugin has been released, which I'm also using (fixes some related issues):

<plugin>
    <groupId>net.codesup.util</groupId>
    <artifactId>jaxb2-rich-contract-plugin</artifactId>
    <version>2.1.0</version>
</plugin>

I also had to use xjb-mappings to get the desired package names, but that's another story.