我的项目包含A.xsd,它从B.xsd导入模式如下,这是另一个项目的一部分:
<xsd:import namespace="http://com.test.schema/common/Context" schemaLocation="http://com.test.schema/common/Context/B.xsd"/>
我正在尝试使用包含B.xsd的项目中的剧集,以便在解析A.xsd时不会重新生成与B.xsd相关的类。所以我引用this和this来提出以下配置:这是pom.xml
<dependencies>
<dependency>
<groupId>com.bar.foo</groupId>
<artifactId>schema-b</artifactId>
<version>1.2</version>
</dependency>
<dependencies>
.
.
.
.
.
<build>
<plugins>
<dependency>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<extension>true</extension>
<episodes>
<episode>
<groupId>com.bar.foo</groupId>
<artifactId>schema-b</artifactId>
</episode>
</episodes>
<catalog>src/main/resources/catalog.cat</catalog>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/schemas</directory>
<includes>
<include>A.xsd</include>
<include>...</include>
<include>...</include>
</includes>
</fileset>
</schema>
</schemas>
<bindingDirectory>${basedir}/src/main/schemas</bindingDirectory>
<bindingIncludes>
<include>*.xjb</include>
</bindingIncludes>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.0</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.0</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这是目录文件:
PUBLIC "http://com.test.schema/common/Context" "maven:com.bar.foo:schema-a:jar::1.2!"
xjb文件中有一些配置可确保将XmlRootElement写入某些生成的类中:
<jxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.1" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:annox="http://annox.dev.java.net" extensionBindingPrefixes="xjc">
<jxb:globalBindings>
<xjc:simple />
</jxb:globalBindings>
<jxb:bindings
schemaLocation="A.xsd">
<jxb:bindings node="//xsd:complexType[@name='ADataType']">
<jxb:class name="AData" />
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
name="AData" />
</annox:annotate>
</jxb:bindings>
</jxb:bindings>
尽管在目录文件中提供了xjc执行的剧集和B.xsd的模式的位置,但是生成了B.xsd的类。
问题是目录文件引用的maven工件未被拾取。我在maven构建日志中看到以下错误:
Malformed URL on system identifier: maven:com.bar.foo:schema-b:jar::1.2!
PUBLIC: http://com.test.schema/common/Context
maven:com.bar.foo:schema-a:jar::1.2!
任何人都可以帮助告诉我为什么我为包含B.xsd的工件打出这个格式错误的URL错误?任何帮助将非常感激。
免责声明:我是maven-jaxb2-plugin的作者。
首先,你可能在这里混合A
和B
。你说A
进口B
然后你使用schema-a
神器作为插曲。如果导入B
,你应该使用schema-b
作为插曲,以便在编译B
时不再生A
的东西。
但我认为这可能只是问题中的一个小错误。
这里有两个方面 - 剧集和目录。
剧集允许您跳过已经在其他地方生成的类的生成。因此,如果在编译schema-b
时使用schema-a
工件,则XJC不应为schema-b
生成类。你不需要那些目录,它是独立的。
有时XJC仍然会产生很少的剩余 - 即使你使用了一集。我经常得到ObjectFactory
,也许会产生一些枚举或顶级元素。我相信这是XJC中的一个问题,我在maven-jaxb2-plugin中无能为力。
因此,作为一种解决方法,我只使用maven-antrun-plugin
删除不必要的生成的东西。
如果你得到生成的所有B
东西,那么你应该检查schema-b
神器是否真的有生成的剧集文件。检查JAR中是否有META-INF/sun-jaxb.episode
。有关剧集文件的一些琐事,请参阅this answer。
因此,如果您正确配置了正确的剧集工件,您不应该生成B
事物,您不需要这样的目录。
您需要的目录是避免在编译时下载http://com.test.schema/common/Context/B.xsd
。您可以使用目录指向其他位置。我认为你的问题在于你将http://com.test.schema/common/Context
引用到maven:com.bar.foo:schema-a:jar::1.2!
,这显然不会指向架构资源。
如果你有像这样的导入
<xsd:import namespace="http://com.test.schema/common/Context" schemaLocation="http://com.test.schema/common/Context/B.xsd"/>
那你应该重写如下:
PUBLIC "http://com.test.schema/common/Context" "maven:com.bar.foo:schema-b:jar::1.2!/common/Context/B.xsd"
假设schema-b
工件包含/common/Context/B.xsd
下的模式。请注意,它映射名称空间,而不是模式位置。
您还可以使用REWRITE_SYSTEM
重写架构位置。例如:
REWRITE_SYSTEM "http://com.test.schema" "maven:com.bar.foo:schema-b:jar::1.2!"
如果您有像http://com.test.schema/common/Context/B.xsd
这样的网址,它会被重写为maven:com.bar.foo:schema-b:jar::1.2!/common/Context/B.xsd
。这将指向/common/Context/B.xsd
JAR内的资源schema-b
。
另一个提示 - 如果您在项目中使用schema-b
作为依赖项,则可以省略该版本。
以下是来自现实世界项目的目录示例:
https://github.com/highsource/ogc-schemas/blob/master/schemas/src/main/resources/ogc/catalog.cat
它包含重写如:
REWRITE_SYSTEM "http://schemas.opengis.net" "maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc"