tkalvin
tkalvin

Reputation: 189

How to configure jaxb/xjc to generate only one class instead of duplicates from imported complexType in more xsd-s?

Suppose I have 2 xsd-s. The first declares type A. The second imports the first xsd (import namespace, xmlns:ns=... etc.) and declares type B, which extends type A (base="ns:B"). Like this:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="A">
        <xs:restriction base="xs:string" />
    </xs:simpleType>
</xs:schema>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation=".\XSD1.xsd" />
    <xs:simpleType name="B">
        <xs:restriction base="A" />
    </xs:simpleType>
</xs:schema>

I use cxf-xjc maven plugin for code generation, with the following config:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-xjc-plugin</artifactId>
    <version>${cxf.version}</version>
    <configuration>
        <extensions>
            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${cxf.version}</extension>
        </extensions>
    </configuration>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>xsdtojava</goal>
            </goals>
            <configuration>
                <xsdOptions>
                    <xsdOption>
                        <xsd>src/main/resources/XSD1.xsd</xsd>
                        <packagename>org.example</packagename>
                    </xsdOption>
                    <xsdOption>
                        <xsd>src/main/resources/XSD2.xsd</xsd>
                        <packagename>org.example.child</packagename>
                    </xsdOption>
                </xsdOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

I want to generate the parent and child classes into separate packages, but this way the XJC generates the parent class into both packages which I don't want.

Is it possible to tell JAXB/XJC not to do this but make only one parent class (A) and import that from the other package in the child class (B) instead?

Upvotes: 5

Views: 529

Answers (0)

Related Questions