创建一个通用的 xsd 生成类以供其他包使用 [英] Create a common xsd generated class to be used by other packages

查看:19
本文介绍了创建一个通用的 xsd 生成类以供其他包使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不同的包中使用相同的生成类.所以结构应该是这样的:

I am trying to use the same generated class but in separate packages. So the structure should look something like this:

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 看起来像这样:

My common.xsd looks like this:

<?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 看起来像

the objectA.xsd looks like

<?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 看起来像:

And objectB.xsd looks like:

<?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,它看起来像这样:

I have a common binding file common.xjb which looks like this:

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

最后我的 Maven 工作看起来像这样:

And finally my maven job looks like this:

        <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>

推荐答案

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

There is a part in the maven-jaxb2-plugin documentation dedicated specifically to the separate schema compilation. But you can also achieve your goal with usual XJC bindings.

  • 不要在插件配置中使用generatePackage,在绑定中使用jaxb:package,例如:

  • Do not use generatePackage in the plugin config, use jaxb:package in bindings, ex.:

<jaxb:schemaBindings>
    <jaxb:package name="generatePackage"/>
</jaxb:schemaBindings>

  • xjb/commons.xjb 上的 commonType 上使用 <jaxb:class ref="com.test.common.CommonType"/>代码>.
  • Use <jaxb:class ref="com.test.common.CommonType"/> on commonType in xjb/commons.xjb.
  • 免责声明:我是 maven-jaxb2-plugin 的作者.

    SO Disclaimer: I'm the author of the maven-jaxb2-plugin.

    这篇关于创建一个通用的 xsd 生成类以供其他包使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆