Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

jaxb - Create a common xsd generated class to be used by other packages

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

But i keep getting this:

com.test.common
     -commonType.java
com.test.A
     -objectA.java
     -commonType.java
com.test.B
     -objectB.java
     -commonType.java

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>

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>

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>

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>

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

  • Do not use generatePackage in the plugin config, use jaxb:package in bindings, ex.:

    <jaxb:schemaBindings>
        <jaxb:package name="generatePackage"/>
    </jaxb:schemaBindings>
    
  • Use <jaxb:class ref="com.test.common.CommonType"/> on commonType in xjb/commons.xjb.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...