创建一个常见的xsd生成类,供其他包使用

Mat*_*att 15 xsd jaxb xjc maven-jaxb2-plugin

我试图使用相同的生成类,但在单独的包中.所以结构看起来应该是这样的:

com.test.common
     -commonType.java
com.test.A
     -objectA.java
com.test.B
     -objectB.java
Run Code Online (Sandbox Code Playgroud)

但我一直这样:

com.test.common
     -commonType.java
com.test.A
     -objectA.java
     -commonType.java
com.test.B
     -objectB.java
     -commonType.java
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

而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>
Run Code Online (Sandbox Code Playgroud)

我有一个常见的绑定文件common.xjb,如下所示:

    <jxb:bindings schemaLocation="../xsd/common.xsd" node="/xsd:schema">
        <jxb:schemaBindings>
            <jxb:package name="com.test.common"/>
        </jxb:schemaBindings>
    </jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

最后我的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>
Run Code Online (Sandbox Code Playgroud)

lex*_*ore 13

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

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

    <jaxb:schemaBindings>
        <jaxb:package name="generatePackage"/>
    </jaxb:schemaBindings>
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用<jaxb:class ref="com.test.common.CommonType"/>commonTypexjb/commons.xjb.

免责声明:我是作者maven-jaxb2-plugin.


bdo*_*han 9

您可以使用-episodeJAXB XJC中的扩展来处理此用例:

XJC调用common.xsd

xjc -d out -episode common.episode common.xsd
Run Code Online (Sandbox Code Playgroud)

XJC调用objectA.xsd

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

xjc -d out objectA.xsd -extension -b common.episode
Run Code Online (Sandbox Code Playgroud)

有关详细示例