使用JAXB 2.2进行单独的模式编译

时间:2014-02-22 16:38:47

标签: java maven jaxb xsd

我正在拼命尝试使用JAXB 2.2和JAXB2 Maven Plugin 0.8.3进行单独的模式编译。我已经关注official documentation,但它没有透露适当的XSD应该是什么样子。

请注意,默认情况下会通过父级生成剧集文件。

我有以下项目布局:

parent
   |-- model-a
   `-- model-b (depends on model-a)

model-a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="urn:net.sf.michael-o:model-a" xmlns:tns="urn:net.sf.michael-o:model-a"
  elementFormDefault="qualified">

  <xs:complexType name="ItemRequest">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="id" type="xs:NCName" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

</xs:schema>

来自POM的片段:

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-model</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <schemaDirectory>src/xsd</schemaDirectory>
        <generatePackage>net.sf.michaelo.model_a</generatePackage>
      </configuration>
    </execution>
  </executions>
</plugin>

现在这里是model-b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="urn:net.sf.michael-o:model-b" xmlns:tns="urn:net.sf.michael-o:model-b"
  elementFormDefault="qualified" xmlns:model-a="urn:net.sf.michael-o:model-a">

  <xs:import namespace="urn:net.sf.michael-o:model-a" />

  <xs:element name="PartRequest" type="model-a:ItemRequest" />
</xs:schema>

Eclipse的XML目录已经使用了import namespace的URI。

来自POM的片段:

<build>
  <plugins>
    <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <executions>
        <execution>
          <id>generate-model</id>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <schemaDirectory>src/xsd</schemaDirectory>
            <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
            <episodes>
              <episode>
                <groupId>net.sf.michael-o.jaxb-jaxws-testing</groupId>
                <artifactId>jaxb-model-a</artifactId>
              </episode>
            </episodes>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
<dependencies>
  <dependency>
    <groupId>net.sf.michael-o.jaxb-jaxws-testing</groupId>
    <artifactId>jaxb-model-a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

不幸的是,这失败了:

[ERROR] Error while parsing schema(s).Location [ file:/D:/workspace-4.2/jaxb-jaxws-testing-parent/jaxb-model-b/src/xsd/model-b.xsd{8,62}].
org.xml.sax.SAXParseException; systemId: file:/D:/workspace-4.2/jaxb-jaxws-testing-parent/jaxb-model-b/src/xsd/model-b.xsd; lineNumber: 8; columnNumber: 62; src-resolve: Name "model-a:ItemRequest" kann nicht als "type definition"-Komponente aufgelöst werden.

事实非常明显,model-a.xsd不可用,但是它不是让剧集文件而不是真正的架构文件的全部意义吗?通过提供原始文件使这种荒谬。此问题也适用于wsimport我要导入的模式文件(JAXB模型),它是在单独的JAR中构建的。

任何提示?

1 个答案:

答案 0 :(得分:0)

由于两个xsd文件都在同一目录中,因此您必须在model-b.xsd中添加schemaLocation,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="urn:net.sf.michael-o:model-b" xmlns:tns="urn:net.sf.michael-o:model-b"
  elementFormDefault="qualified" xmlns:model-a="urn:net.sf.michael-o:model-a">

  <xs:import namespace="urn:net.sf.michael-o:model-a" schemaLocation="model-a.xsd"/>

  <xs:element name="PartRequest" type="model-a:ItemRequest" />
</xs:schema>

如果无法修改文件,jaxb插件可以读取xsd文件from a catalog的位置。但是我还没有设法让它与目录一起使用。