使用JAXB 2.1将多个模式编译到不同的包中

Pri*_*iya 11 jaxb

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

Multiple <schemaBindings> are defined for the target namespace ""
Run Code Online (Sandbox Code Playgroud)

当我尝试使用绑定文件将其编译到不同的包中时.请告诉我是否有办法将它们编译成不同的包.我正在使用jaxb 2.1

小智 6

是的,有一种方法.
假设:

xsd/common/common.xsd
xsd/foo/foo.xsd 
Run Code Online (Sandbox Code Playgroud)

在公共目录中common.xjb:

<jxb:schemaBindings>
    <jxb:package name="mypkg.common">
    </jxb:package>
</jxb:schemaBindings> 
Run Code Online (Sandbox Code Playgroud)

在foo目录中foo.xjb:

<jxb:schemaBindings>
    <jxb:package name="mypkg.foo">
     </jxb:package>
</jxb:schemaBindings> 
Run Code Online (Sandbox Code Playgroud)

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

你需要确保common.xsd有一个targetNameSpace是从不同foo.xsdtargetNameSpace.


mmo*_*sen 6

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

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


LE *_*oît 5

我遇到了同样的问题,但还没有解决,但恐怕不可能将XSD生成到differents包中:

每个名称空间具有多个<jaxb:schemaBindings>是不合法的,因此不可能在同一目标名称空间中将两个模式编译为不同的Java包。

来自此页末尾的编译器限制

但是如果有人找到解决方法,请通知我们