本文介绍了创建一个常见的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>


  • 使用< jaxb:class ref =com.test.common。 commonType 中的CommonType/> in xjb / commons.xjb

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

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

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

    08-20 19:21