Two classes have the same XML type name maven jaxb plugin , Spring

1.6k views Asked by At

I have some control over some of my schemas so my question is a little different than the ones similar to this. I have three schemas, c.xsd imports b.xsd, b.xsd imports a.xsd. All three have different namespaces.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="ANamespace" targetNamespace="ANamespace" version="1.0" jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">
<xs:complexType name="ClassA">
    <xs:sequence>
        <xs:element name="classAFieldName" type="xs:string" minOccurs="0">
        </xs:element>
    </xs:sequence>
</xs:complexType>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="BNamespace" targetNamespace="BNamespace" version="1.0" jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">


<xs:import namespace="ANamespace" schemaLocation="a.xsd"/>

<xs:complexType name="ClassB">
    <xs:sequence>
        <xs:element name="classBFieldName" type="xs:string" minOccurs="0">
        </xs:element>
    </xs:sequence>
</xs:complexType>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="CNamespace" targetNamespace="CNamespace" version="1.0" jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">


<xs:import namespace="BNamespace" schemaLocation="b.xsd"/>

<xs:complexType name="ClassC">
    <xs:sequence>
        <xs:element name="classCFieldName" type="xs:string" minOccurs="0">
        </xs:element>
    </xs:sequence>
</xs:complexType>

Class A is generated twice, once in org.testing.common and once in org.testing.c and both of them both have the same namespace @XmlType(name = "ClassA", namespace = "ANamespace". However, Spring doesn't know which one to use. Do I really need ClassA generated twice?

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.wfs.fi.stp</groupId>
<artifactId>testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>testing</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.jvnet.jaxb2.maven2</groupId>
                                <artifactId>maven-jaxb2-plugin</artifactId>
                                <versionRange>[0.13.1,)</versionRange>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <configurator>
                                    <id>org.eclipselabs.m2e.jaxb2.connector.Jaxb2JavaProjectConfigurator</id>
                                </configurator>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.1</version>
            <executions>
                <execution>
                    <id>A</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>src/main/resources</schemaDirectory>
                        <schemaIncludes>
                            <include>a.xsd</include>
                        </schemaIncludes>
                        <bindingIncludes>
                            <include>a.xjb</include>
                        </bindingIncludes>
                        <generatePackage>org.testing.common</generatePackage>
                        <generateDirectory>${project.build.directory}/common_generated</generateDirectory>
                        <episode>true</episode>
                        <episodeFile>${project.build.directory}/common.episode</episodeFile>
                        <extension>true</extension>
                        <verbose>true</verbose>
                        <properties>
                            <property>
                                <name>javax.xml.accessExternalSchema</name>
                                <value>all</value>
                            </property>
                            <property>
                                <name>javax.xml.accessExternalDTD</name>
                                <value>all</value>
                            </property>
                        </properties>
                        <args>
                            <arg>-npa</arg>
                        </args>
                        <disableXmlSecurity>true</disableXmlSecurity>
                        <accessExternalSchema>all</accessExternalSchema>
                        <accessExternalDTD>all</accessExternalDTD>
                    </configuration>
                </execution>
                <execution>
                    <id>B</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>src/main/resources</schemaDirectory>
                        <schemaIncludes>
                            <include>b.xsd</include>
                        </schemaIncludes>
                        <generatePackage>org.testing.b</generatePackage>
                        <generateDirectory>${project.build.directory}/b_generated</generateDirectory>
                        <bindingIncludes>
                            <include>b.xjb</include>
                        </bindingIncludes>
                        <episode>true</episode>
                        <args>
                            <arg>-b</arg>
                            <arg>${project.build.directory}/common.episode</arg>
                            <arg>-npa</arg>
                        </args>
                        <episodeFile>${project.build.directory}/b.episode</episodeFile>
                        <extension>true</extension>
                        <verbose>true</verbose>
                        <disableXmlSecurity>true</disableXmlSecurity>
                        <accessExternalSchema>all</accessExternalSchema>
                        <accessExternalDTD>all</accessExternalDTD>
                    </configuration>
                </execution>

                <execution>
                    <id>C</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>src/main/resources</schemaDirectory>
                        <schemaIncludes>
                            <include>c.xsd</include>
                        </schemaIncludes>
                        <generatePackage>org.testing.c</generatePackage>
                        <generateDirectory>${project.build.directory}/c_generated</generateDirectory>
                        <bindingIncludes>
                            <include>c.xjb</include>
                        </bindingIncludes>
                        <args>
                            <arg>-b</arg>
                            <arg>${project.build.directory}/b.episode</arg>

                            <arg>-npa</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics</artifactId>
                                <version>0.6.4</version>
                            </plugin>
                        </plugins>
                        <extension>true</extension>
                        <verbose>true</verbose>
                        <disableXmlSecurity>true</disableXmlSecurity>
                        <accessExternalSchema>all</accessExternalSchema>
                        <accessExternalDTD>all</accessExternalDTD>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

1

There are 1 answers

4
teppic On

Run the generation goal once and specify your namespace to package mapping in your binding file instead of in the plugin config:

<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
        version="2.1">

    <jaxb:bindings schemaLocation="a.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="org.testing.common"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>
    <jaxb:bindings schemaLocation="b.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="org.testing.b"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>
    <jaxb:bindings schemaLocation="c.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="org.testing.c"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>

</jaxb:bindings>