创建一个通用的 xsd 生成类以供其他包使用

问题描述 投票:0回答:3

我正在尝试使用相同的生成类,但在单独的包中。所以结构应该是这样的:

com.test.common
     -commonType.java
com.test.A
     -objectA.java
com.test.B
     -objectB.java

但我不断收到这个:

com.test.common
     -commonType.java
com.test.A
     -objectA.java
     -commonType.java
com.test.B
     -objectB.java
     -commonType.java

我的 common.xsd 看起来像这样:

<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
    targetNamespace="http://test.com/magic/common"
    xmlns="http://test.com/magic/common"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.0">

    <xs:complexType name="CommonType">
        <xs:sequence>
            <xs:element name="name" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

objectA.xsd 看起来像

<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
    targetNamespace="http://test.com/magic/objectA"
    xmlns:common="http://test.com/magic/common"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.0">

    <xs:complexType name="ObjectA">
        <xs:sequence>
            <xs:element name="size" type="xs:string" />
            <xs:element name="commonA" type="common:CommonType" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

objectB.xsd 看起来像:

<?xml version="1.0"?>
<xs:schema elementFormDefault="qualified" version="1.0"
    targetNamespace="http://test.com/magic/objectB"
    xmlns:common="http://test.com/magic/common"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.0">

    <xs:complexType name="ObjectB">
        <xs:sequence>
            <xs:element name="version" type="xs:string" />
            <xs:element name="commonB" type="common:CommonType" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

我有一个公共绑定文件 common.xjb,如下所示:

    <jxb:bindings schemaLocation="../xsd/common.xsd" node="/xsd:schema">
        <jxb:schemaBindings>
            <jxb:package name="com.test.common"/>
        </jxb:schemaBindings>
    </jxb:bindings>

最后我的 Maven 工作如下所示:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <args>
                    <arg>-Xequals</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.3</version>
                    </plugin>
                </plugins>
                <episode>true</episode>
                <extension>true</extension>
                <verbose>true</verbose>
                <generateDirectory>src/main/java</generateDirectory>
            </configuration>
            <executions>
                <execution>
                    <id>common</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatePackage>com.test.common</generatePackage>
                        <schemaIncludes>
                            <includeSchema>xsd/common.xsd</includeSchema>
                        </schemaIncludes>
                    </configuration>
                </execution>
                <execution>
                    <id>login</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatePackage>com.test.A</generatePackage>
                        <bindingIncludes>
                            <includeBinding>xjb/commons.xjb</includeBinding>
                        </bindingIncludes>
                        <schemaIncludes>
                            <includeSchema>xsd/objectA.xsd</includeSchema>
                        </schemaIncludes>
                    </configuration>
                </execution>
                <execution>
                    <id>alert</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatePackage>com.test.B</generatePackage>
                        <bindingIncludes>
                            <includeBinding>xjb/commons.xjb</includeBinding>
                        </bindingIncludes>
                        <schemaIncludes>
                            <includeSchema>xsd/objectB.xsd</includeSchema>
                        </schemaIncludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
xsd jaxb xjc maven-jaxb2-plugin
3个回答
13
投票

maven-jaxb2-plugin
文档中有一部分专门用于单独的模式编译。但您也可以使用常用的 XJC 绑定来实现您的目标。

  • 不要在插件配置中使用

    generatePackage
    ,在绑定中使用
    jaxb:package
    ,例如:

    <jaxb:schemaBindings>
        <jaxb:package name="generatePackage"/>
    </jaxb:schemaBindings>
    
  • <jaxb:class ref="com.test.common.CommonType"/>
    中的
    commonType
    上使用
    xjb/commons.xjb

SO 免责声明:我是

maven-jaxb2-plugin
的作者。


10
投票

您可以使用 JAXB XJC 中的

-episode
扩展来处理此用例:

XJC征集common.xsd

xjc -d out -episode common.episode common.xsd

XJC 调用 objectA.xsd

第一步生成的剧集文件实际上是一个 JAXB 外部绑定文件,其中包含模式类型和现有类之间的链接。这会阻止 XJC 重新生成这些类。

xjc -d out objectA.xsd -extension -b common.episode

详细示例


0
投票

使用 JAXB 3 和

maven-jaxb-plugin
版本
3.1.0
的解决方案。

你必须制作4个XJB文件:

  • common.xjb
    commonClasses.xjb
    为常见类型
  • objectA.xjb
    适用于
    objectA.xsd
  • 的特定类型
  • objectB.xjb
    适用于
    objectB.xsd
  • 的特定类型

common.xjb
将常用类型放入
com.test.common
Java 包中:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="3.0" xmlns="https://jakarta.ee/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="http://test.com/magic/common">
    <schemaBindings>
      <package name="com.test.common"/>
    </schemaBindings>
  </bindings>
</bindings>

commonClasses.xjb
将指示
objectA
objectB
重用
com.test.common
Java 包中的常见类型:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="3.0" xmlns="https://jakarta.ee/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="https://namespace1">
    <bindings scd="~tns:CommonType1">
      <class ref="com.test.common.CommonType1"/>
    </bindings>
    <bindings scd="~tns:CommonType2">
      <class ref="com.test.common.CommonType2"/>
    </bindings>
    <!-- ... -->
  </bindings>
</bindings>

请注意,

maven-jaxb-plugin
可以通过在
commonClasses.xjb
上执行来为您生成
common.xsd
文件。执行后查看
target/classes/META-INF/JAXB
目录。也许您甚至可以重用
target/
中生成的 XJB 文件,但我还没有尝试过。

objectA.xjb
会将
objectA.xsd
特定类型放入
com.test.A
:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="3.0" xmlns="https://jakarta.ee/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="http://test.com/magic/objectA">
    <schemaBindings>
      <package name="com.test.A"/>
    </schemaBindings>
  </bindings>
</bindings>

objectB.xjb
会将
objectB.xsd
特定类型放入
com.test.B
:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="3.0" xmlns="https://jakarta.ee/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="http://test.com/magic/objectB">
    <schemaBindings>
      <package name="com.test.B"/>
    </schemaBindings>
  </bindings>
</bindings>

pom.xml
内容更新为较新的
maven-jaxb-plugin

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>3.1.0</version>

        <!-- Common configuration for all executions -->
        <configuration>
          <clearOutputDir>false</clearOutputDir>
        </configuration>

        <executions>
          <execution>
            <id>xjc-common</id>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <sources>
                <source>common.xsd</source>
              </sources>
              <xjbSources>
                <xjbSource>common.xjb</xjbSource>
              </xjbSources>
            </configuration>
          </execution>

          <execution>
            <id>xjc-objectB</id>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <sources>
                <source>objectA.xsd</source>
              </sources>
              <xjbSources>
                <xjbSource>commonClasses.xjb</xjbSource>
                <xjbSource>objectA.xjb</xjbSource>
              </xjbSources>
            </configuration>
          </execution>

          <execution>
            <id>xjc-objectB</id>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <sources>
                <source>objectB.xsd</source>
              </sources>
              <xjbSources>
                <xjbSource>commonClasses.xjb</xjbSource>
                <xjbSource>objectB.xjb</xjbSource>
              </xjbSources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

鉴于您有

common
objectA
objectB
XML 类型驻留在不同的 XML 命名空间中,也许您可以将
common.xjb
objectA.xjb
objectB.xjb
合并到 1 个 XJB 文件中,完全避免
common-classes.jxb
,并在 1 次执行中运行所有内容,但我还没有尝试过。

您可以尝试以下合并的XJB文件,我们称之为

merged.xjb

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="3.0" xmlns="https://jakarta.ee/xml/ns/jaxb">
  <bindings scd="x-schema::tns" xmlns:tns="http://test.com/magic/common">
    <schemaBindings>
      <package name="com.test.common"/>
    </schemaBindings>
  </bindings>
  <bindings scd="x-schema::tns" xmlns:tns="http://test.com/magic/objectA">
    <schemaBindings>
      <package name="com.test.A"/>
    </schemaBindings>
  </bindings>
  <bindings scd="x-schema::tns" xmlns:tns="http://test.com/magic/objectB">
    <schemaBindings>
      <package name="com.test.B"/>
    </schemaBindings>
  </bindings>
</bindings>

以及以下

pom.xml
块:

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>3.1.0</version>

        <executions>
          <execution>
            <id>xjc-merged</id>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <sources>
                <source>common.xsd</source>
                <source>objectA.xsd</source>
                <source>objectB.xsd</source>
              </sources>
              <xjbSources>
                <xjbSource>merged.xjb</xjbSource>
              </xjbSources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
© www.soinside.com 2019 - 2024. All rights reserved.