使用JAXB2.1将多个模式编译到不同的包中

使用JAXB2.1将多个模式编译到不同的包中,jaxb,Jaxb,我有一个CommonTypes.xsd,我使用xs:include将它包含在我的所有其他xsd中。我明白了 Multiple <schemaBindings> are defined for the target namespace "" 为目标命名空间“”定义了多个 当我尝试使用绑定文件将其编译成不同的包时。请告诉我是否有办法将它们编译成不同的包。我使用的是jaxb 2.1,我遇到了同样的问题,但还没有解决,但恐怕无法将XSD生成到不同的包中: 每个名称空间拥有多个模式是不合法

我有一个CommonTypes.xsd,我使用xs:include将它包含在我的所有其他xsd中。我明白了

Multiple <schemaBindings> are defined for the target namespace ""
为目标命名空间“”定义了多个
当我尝试使用绑定文件将其编译成不同的包时。请告诉我是否有办法将它们编译成不同的包。我使用的是jaxb 2.1,我遇到了同样的问题,但还没有解决,但恐怕无法将XSD生成到不同的包中:

每个名称空间拥有多个模式是不合法的,因此不可能将同一目标名称空间中的两个模式编译到不同的Java包中

但是如果有人找到工作,请告诉我们是的,有办法。
假设:

xsd/common/common.xsd
xsd/foo/foo.xsd 
在公共目录中放置
common.xjb

<jxb:schemaBindings>
    <jxb:package name="mypkg.common">
    </jxb:package>
</jxb:schemaBindings> 
<jxb:schemaBindings>
    <jxb:package name="mypkg.foo">
     </jxb:package>
</jxb:schemaBindings> 
build.xml
文件中,为每个任务创建一个xjc任务:

<xjc destdir="${app.src}" readOnly="true" package="mypkg.common">
    <schema dir="${app.schema}/common" includes="*.xsd" />
    <binding dir="${app.schema}/common" includes="*.xjb" />
</xjc>
<xjc destdir="${app.src}" readOnly="true" package="mypkg.foo">
    <schema dir="${app.schema}/foo" includes="*.xsd" />
    <binding dir="${app.schema}/foo" includes="foo.xjb" />
</xjc>


您需要确保
common.xsd
targetNameSpace
foo.xsd
targetNameSpace
不同

如Ben所述,如果它们具有相同的名称空间,则无法实现这一点。 但是如果您有不同的名称空间,该如何做呢

<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
        <jxb:schemaBindings>
            <jxb:package name="com.test.oagi.udt"/>
        </jxb:schemaBindings>
    </jxb:bindings>
    <jxb:bindings namespace="http://www.openapplications.org/oagis/9/codelists" schemaLocation="oagi/Common/OAGi/Components/CodeLists.xsd" >
        <jxb:schemaBindings>
            <jxb:package name="com.test.oagi.cl"/>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>


但是请确保不要使用命令行参数-p,因为这将覆盖您的配置。

我知道这是一篇老文章,但是,由于没有确切的问题答案,我的建议如下:

正如mmoossen所解释的,诀窍是为xsd指定不同的名称空间。 但是,在
jxb:bindings
标记中添加
namespace
属性不起作用:

<jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
完成后,您将能够有1个外部自定义文件(.xjb)声明不同的
schemaBindings
,每个文件可能使用不同的包

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    targetNamespace="some.namespace"
    version="1.0">
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
               jaxb:extensionBindingPrefixes="xjc annox inherit">


    <jaxb:bindings schemaLocation="MyFirstXSD.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="com.test.a" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="MySecondXSD.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="com.test.b" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="MyThirdXSD.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="com.test.c" />
        </jaxb:schemaBindings>
    </jaxb:bindings>

</jaxb:bindings>

在具有不同配置的多个模式的情况下,可以按照jaxb maven插件使用页面中提到的方法进行

可以为每个架构配置单独的包

<packageName>se.west</packageName>
se.west
完成下面的示例配置:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
    <execution>
        <id>xjc-schema1</id>
        <goals>
            <goal>xjc</goal>
        </goals>
        <configuration>
            <!-- Use all XSDs under the west directory for sources here. -->
            <sources>
                <source>src/main/xsds/west</source>
            </sources>

            <!-- Package name of the generated sources. -->
            <packageName>se.west</packageName>
        </configuration>
    </execution>
    <execution>
        <id>xjc-schema2</id>
        <goals>
            <goal>xjc</goal>
        </goals>
        <configuration>
            <!-- Use all XSDs under the east directory for sources here. -->
            <sources>
                <source>src/main/xsds/east</source>
            </sources>

            <!-- Package name of the generated sources. -->
            <packageName>se.east</packageName>

            <!--
                Don't clear the output directory before generating the sources.
                Clearing the output directory removes the se.west schema from above.
            -->
            <clearOutputDir>false</clearOutputDir>
        </configuration>
    </execution>
</executions>

org.codehaus.mojo
jaxb2 maven插件
${project.version}
xjc-schema1
xjc
src/main/xsds/west
西南
xjc-schema2
xjc
src/main/xsds/east
东南东
假的

请在您的问题中添加一个示例绑定文件。单击,是否无法拥有一个xjb文件?我们有20个XSD,所以我讨厌有20个xjb文件。是的-你可以在一个.xjb文件中有多个。@t如果
引用相同的
targetnamespace
,则不可能。