Compiling multiple schemas into different packages using JAXB 2.1

前端 未结 5 1045
猫巷女王i
猫巷女王i 2021-01-02 03:40

I have a CommonTypes.xsd which I\'m including in my all other XSDs using xs:include. I get

Multiple  are defined for the target namesp         


        
相关标签:
5条回答
  • 2021-01-02 04:12

    Can be done as mentioned in jaxb maven plugin usage page in case of having Multiple schemas with different configuration.

    Separate packages can be configured for each schema.

    <packageName>se.west</packageName>
    

    complete example configuration below:

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

    0 讨论(0)
  • 2021-01-02 04:15

    Yeah, there is a way.
    Assuming:

    xsd/common/common.xsd
    xsd/foo/foo.xsd 
    

    In the common directory place common.xjb:

    <jxb:schemaBindings>
        <jxb:package name="mypkg.common">
        </jxb:package>
    </jxb:schemaBindings> 
    

    In the foo directory place foo.xjb:

    <jxb:schemaBindings>
        <jxb:package name="mypkg.foo">
         </jxb:package>
    </jxb:schemaBindings> 
    

    In the build.xml file, create one xjc task for each:

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

    You need to make sure that common.xsd has a targetNameSpace that is different from foo.xsd's targetNameSpace.

    0 讨论(0)
  • 2021-01-02 04:18

    I know it is an old post, but, as there is no answer for the exact question, here is my proposal:

    As mmoossen explained, the trick is to specify different namespaces for the XSDs. But, adding a namespace attribute in the jxb:bindings tag doesn't work:

    <jxb:bindings namespace="http://www.openapplications.org/oagis/9/unqualifieddatatypes/1.1" schemaLocation="oagi/Common/UNCEFACT/ATG/CoreComponents/UnqualifiedDataTypes.xsd" >
    

    Instead of that, you need to add a targetNamespace attribute to the xs:schema tags of your XSDs:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified" attributeFormDefault="unqualified"
        targetNamespace="some.namespace"
        version="1.0">
    

    Once done, you will be able to have 1 external customization file (.xjb) declaring different schemaBindings, each of them possibly using a different package:

    <?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>
    
    0 讨论(0)
  • 2021-01-02 04:24

    I've meet the same problem and haven't solve it yet, but I'm afraid that it can't be possible to generate XSD into differents packages :

    It is not legal to have more than one <jaxb:schemaBindings> per namespace, so it is impossible to have two schemas in the same target namespace compiled into different Java packages

    from Compiler Restrictions at the end of this page

    but if some one find some work around, just inform us please

    0 讨论(0)
  • 2021-01-02 04:27

    As stated already by Ben there is no way to do that if they have the same namespace. But how to do it if you do have different namespaces?

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

    but be sure you do not use the command line parameter -p, since that will override your config.

    0 讨论(0)
提交回复
热议问题