【发布时间】:2017-01-20 04:16:34
【问题描述】:
我在使用 JAXB2 从 XSD 创建 Java 类时遇到问题。
如果 JAXB 可以处理,请告诉我。
场景:
1) 项目 A 具有具有不同名称空间的 a.xsd 和 b.xsd
b.xsd 使用带有导入名称空间标签的 a.xsd。
2) 项目 B 具有 c.xsd 并通过导入 b.xsd 来使用 b.xsd。c.xsd 正在使用目录在作为依赖项添加的 Maven JAR 中查找 b.xsd。
问题:
Project A 构建良好,但 Project B 抛出错误,因为它找不到 b.xsd 内部使用的 a.xsd。
错误
[错误] 解析架构时出错。位置 [http://www.example.com/test2/test2.xsd{15,39}]。 org.xml.sax.SAXParseException;系统ID:http://www.example.com/test2/test2.xsd;行号:15;列号:39; src-resolve:无法将名称“addme:address”解析为(n)“元素声明”组件。 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
项目 A
a.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/address" xmlns="http://www.example.com/address"
elementFormDefault="qualified">
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="street" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
b.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/test2"
xmlns="http://www.example.com/test2"
xmlns:addme="http://www.example.com/address"
elementFormDefault="qualified">
<xs:import namespace="http://www.example.com/address" schemaLocation="http://www.example.com/address/address.xsd"/>
<xs:element name="test2">
<xs:complexType>
<xs:sequence>
<xs:element ref="addme:address" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
项目 B
c.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com/customer"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com/customer"
xmlns:test="http://www.example.com/test2" elementFormDefault="qualified">
<xs:import namespace="http://www.example.com/test2"
schemaLocation="http://www.example.com/test2/test2.xsd" />
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element ref="test:test2" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
项目 B 的目录文件
REWRITE_SYSTEM "http://www.example.com/test2" "maven:com.test.projectA:projectA:jar::!"
项目 A POM sn-p
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
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">
<properties>
<xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir>
<generated.source.location>${basedir}/target/generated-sources/src</generated.source.location>
</properties>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generateDirectory>${generated.source.location}</generateDirectory>
<schemaDirectory>${xsd.build.dir}</schemaDirectory>
<episode>true</episode>
<addIfExistsToEpisodeSchemaBindings>true</addIfExistsToEpisodeSchemaBindings>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
</args>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
<execution>
<id>attach-sources</id>
<phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
**PROJECT B POM**
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" .>
<properties>
<xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir>
<generated.source.location>${basedir}/target/generated-sources/src</generated.source.location>
</properties>
<dependencies>
<dependency>
<groupId>com.test.projectA</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generateDirectory>${generated.source.location}</generateDirectory>
<schemaDirectory>${xsd.build.dir}</schemaDirectory>
<catalog>src/main/resources/catalog.cat</catalog>
<useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
</args>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
<execution>
<id>attach-sources</id>
<phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
-
我看不到
http://www.example.com/address的目录条目。它可能试图从 b.xsd 中指定的位置加载它,即http://www.example.com/address/address.xsd。 -
感谢@teppic。实际上我之前尝试过没有运气,我也是这么想的。基本上,当我构建具有 a 和 b xsd 的 projecA 时,构建良好,问题出现在具有 c xsd ->references b xsd -> 引用 a xsd 的项目中。 a 和 b 作为依赖项提供,可能在解析时无法从“c”级别获取“a”,但不确定是否有任何特定方法可以告诉系统或目录中......