Maven无法使用嵌套模式生成JAXB类

时间:2015-04-27 01:58:40

标签: java maven xsd jaxb maven-jaxb2-plugin

我试图让Maven使用嵌套的xml架构生成JAXB绑定:schema A导入架构B.架构B导入架构C.

如果模式A中的对象引用模式B中不依赖于模式C对象的对象,那么一切都会很好地构建。如果模式B中的对象引用模式C中的对象,则它会中断。也就是说,深入一级是有效的。深入两点不会。

一旦我在模式A中添加一个引用模式B中对象的对象,模式B中的对象又引用了模式C中的对象,我得到一个org.xml.sax.SAXParseException:rc-resolve:无法解析名称'schemaB:objectInSchemaB'到(n)'类型定义'组件。

如果省略顶级模式A,我也可以成功构建,并从编译模式B导入模式C开始。再次,问题似乎是嵌套导入的深度不止一个。

我被困住了!

JAXB版本:

检测到JAXB API版本[2.2]。 pluginArtifacts:[org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:maven-plugin:0.9.0:,org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-core:jar:0.9.0:compile, com.sun.org.apache.xml.internal:resolver:jar:20050927:compile,org.sonatype.plexus:plexus-build-api:jar:0.0.7:compile,junit:junit:jar:4.8.1: compile,org.codehaus.plexus:plexus-utils:jar:1.5.15:compile,org.jvnet.jaxb2.maven2:maven-jaxb22-plugin:jar:0.9.0:compile,javax.xml.bind:jaxb- api:jar:2.2.7:compile,com.sun.xml.bind:jaxb-impl:jar:2.2.7:compile,com.sun.xml.bind:jaxb-core:jar:2.2.7:compile, com.sun.istack:istack-commons-runtime:jar:2.16:compile,com.sun.xml.fastinfoset:FastInfoset:jar:1.2.12:compile,javax.xml.bind:jsr173_api:jar:1.0:compile, com.sun.xml.bind:jaxb-xjc:jar:2.2.7:compile,org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.2:compile] specVersion要求:2.2

下面是我的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaIncludes>
                            <schemaInclude>**/*.xsd</schemaInclude>
                        </schemaIncludes>
                        <strict>true</strict>
                        <verbose>true</verbose>
                        <extension>true</extension>
                        <removeOldOutput>true</removeOldOutput>
                        <specVersion>2.2</specVersion>
                        <episode>true</episode>
                        <episodeFile>${project.build.directory}/generated-sources/xjc/META-INF/jaxb-example.episode</episodeFile>
                    </configuration>
                    <id>jaxb-generate-example</id>
                </execution>
            </executions>
            <configuration>
                <catalog>src/main/resources/jaxb/catalog.xml</catalog>
                <catalogResolver>org.jvnet.jaxb2.maven2.resolver.tools.ClasspathCatalogResolver</catalogResolver>
                <forceRegenerate>true</forceRegenerate>
                <generateDirectory>${project.build.directory}/generated-sources/xjc</generateDirectory>
                <verbose>true</verbose>
                <args>
                    <arg>-enableIntrospection</arg>
                </args>
            </configuration>
        </plugin>
    </plugins>
</build>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

下面的“架构A”。如果我注释掉baseSystemConfig对象,那么架构就会编译。

 <?xml version="1.0"?>   
 <xsd:schema version="1.0"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           targetNamespace="http://ws.zwake.com/schema/zwakkiCentralCommon"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:zwakkiBase="http://ws.zwake.com/schema/zwakeBase"
           jaxb:version="2.1">
    <!-- Schema B import -->
    <xsd:import namespace="http://ws.zwake.com/schema/zwakeBase" schemaLocation="../ZwakeBase/zwakeBase.xsd"/>

    <xsd:annotation>
        <xsd:appinfo>
            <jaxb:globalBindings typesafeEnumMemberName="generateName"/>
        </xsd:appinfo>
    </xsd:annotation> 

    <xsd:element name="RetrieveBaseSystemConfigsRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="credentials" type="zwakkiBase:Credentials"/>
                <xsd:element name="restrictToBaseSystemIds" type="xsd:string" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element> 
    <xsd:element name="RetrieveBaseSystemConfigsResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="baseSystemConfig" type="zwakkiBase:ZwakeConfig" maxOccurs="unbounded"/>
                <xsd:element name="responseStatus" type="zwakkiBase:SystemError" minOccurs="1"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

1 个答案:

答案 0 :(得分:1)

我找到了原因:我在嵌套模式中指向了一个元素而不是一个complexType(&#39;一个类型&#39;正如编译错误所说的那样)。这导致编译失败。我需要使用ref =而不是type =,或者更改架构B定义。

所以问题最初看起来太明显了。感谢Xstian确认否则它应该一直在工作。这让我找到了找到它的正确途径。