首页 > 解决方案 > maven-jaxb2-plugin 两个具有相同元素的 xsd

问题描述

我有两个 xsd(样本被简化):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://someurl.com" xmlns="http://someurl.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified">
    <xs:element name="Request">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Users" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="User" type="UserType" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="UserType" type="xs:string" minOccurs="0" maxOccurs="1">
        </xs:element>
</xs:schema>

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://otherurl.com" xmlns="http://otherurl.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified">
    <xs:element name="Request">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Users" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="User" type="UserType" minOccurs="0" maxOccurs="unbounded"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="UserType" type="xs:string" minOccurs="0" maxOccurs="1">
        </xs:element>
</xs:schema>

我需要将 Request 元素生成到不同的包或具有不同类名的相同包中(如果可能的话)。并且所有子元素都放在同一个包中,因此例如它不会生成两个名为 UserType 的类。

这就是我现在在 pom.xml 中生成它的方式(但在这种情况下它的问题是因为生成了两个不同的 UserType 类):

<execution>
                    <id>xsd1</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>

    <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                        <schemaIncludes>
                            <include>xsd1.xsd</include>
                        </schemaIncludes>

   <bindingDirectory>src/main/resources/xsd</bindingDirectory>
                        <bindingIncludes>
                            <include>edu.xjb</include>
                        </bindingIncludes>
                        <generateDirectory>${project.basedir}/target/generated-sources/jaxb/schemas/xsd1</generateDirectory>
                        <generatePackage>mainpackage.package1</generatePackage>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics</artifactId>
                                <version>0.9.4</version>
                            </plugin>
                        </plugins>
                        <args>
                            <arg>-Xequals</arg>
                            <arg>-XhashCode</arg>
                            <arg>-XtoString</arg>
                        </args>
                    </configuration>
                </execution>

<execution>
                        <id>xsd2</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                            <schemaIncludes>
                                <include>xsd2.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>src/main/resources/xsd</bindingDirectory>
                            <bindingIncludes>
                                <include>edu.xjb</include>
                            </bindingIncludes>
                            <generateDirectory>${project.basedir}/target/generated-sources/jaxb/schemas/xsd2</generateDirectory>
                            <generatePackage>mainpackage.package2</generatePackage>
                            <plugins>
                                <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                    <artifactId>jaxb2-basics</artifactId>
                                    <version>0.9.4</version>
                                </plugin>
                            </plugins>
                            <args>
                                <arg>-Xequals</arg>
                                <arg>-XhashCode</arg>
                                <arg>-XtoString</arg>
                            </args>
                        </configuration>
                    </execution>

标签: javajaxbpom.xmlmaven-jaxb2-plugin

解决方案


推荐阅读