问题描述
我想通过XSDs
生成java
类,我想在另一个文件中使用/包含一个XSD
文件,但是当我将它们包含在另一个XSDs
中时,两个软件包都生成了相同的java
类.我也在使用maven-jaxb2-plugin
插件
I want to generate java
classes through XSDs
, I want to use/include one XSD
file in another but when I include them in another XSDs
same java
class is generated in both the packages. I am also using maven-jaxb2-plugin
plugin
推荐答案
执行使用所谓的片段分开(也称为模块化)模式编译.也就是说,如果要将模式A导入到模式B中并为模式B生成类,则首先要创建一个单独的Maven项目,以便像往常一样使用maven-jaxb2-plugin将模式A编译为一个单独的Maven工件. (这是假设模式A是独立的,即不导入任何其他模式;否则,您必须在导入的模式上重复相同的过程.)结果,您将获得带有生成的类A.jar的仅来自模式A ,最重要的是, META -INF/sun-jaxb.episode 文件.该文件提供有关现有XSD到Java绑定的信息,这将告诉maven-jaxb2-plugin(实际上是maven-jaxb2-plugin底层的xjc工具)已经从架构中生成了什么,因此可以防止它重新生成相同的类.
Do separate - aka modular - schema compilation using so-called episodes. That is to say, if you want to import schema A into schema B and generate classes for schema B, you first create a separate Maven project in order to compile schema A to a separate Maven artifact with maven-jaxb2-plugin as usual. (This is assuming the schema A stands alone, i.e. does not import any other schema; else you have to repeat the same process on the imported schema(s).) As a result, you get A.jar with the generated classes only from schema A, and most importantly, a META-INF/sun-jaxb.episode file. This file provides information about the existing XSD-to-Java bindings, and this will tell the maven-jaxb2-plugin (in fact the xjc tool underlying maven-jaxb2-plugin) what has already been generated from the schema, and therefore prevent it to re-generate the same classes again.
然后创建另一个Maven项目,以编译模式B,其中A.jar的maven工件在其Maven依赖项中.这次,在maven-jaxb2-plugin的配置中,将配置参数useDependenciesAsEpisodes
设置为true
.这将告诉插件使用所有依赖项中的.episode
文件(如果存在).您可以在插件的 GitHub Wiki 上找到一个基本示例.以下是来自 AuthzForce 项目(XACML实现)的真实示例,其中OASIS XACML标准架构(xacml-core-v3-schema-wd-17.xsd
)导入W3C标准XML名称空间模式(xml.xsd
).因此,您有一个针对xml.xsd的Maven项目/工件 ,以及另一种XACML模式,其中的相关部分POM看起来像这样:
Then you create another Maven project in order to compile schema B, with maven artifact of A.jar among its Maven dependencies. This time, in the configuration of maven-jaxb2-plugin, you set the configuration parameter useDependenciesAsEpisodes
to true
. This will tell the plugin to use the .episode
files from all dependencies (when there is any). You can find a basic example on the plugin's GitHub wiki. Below is a real-life example from AuthzForce project, an XACML implementation, where the OASIS XACML standard schema (xacml-core-v3-schema-wd-17.xsd
) imports the W3C standard XML namespace schema (xml.xsd
). Therefore, you have one Maven project/artifact for the xml.xsd, and another one for the XACML schema, where the relevant parts of the POM look like this:
<project ...>
...
<dependencies>
...
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${artifactId.prefix}-xmlns-model</artifactId>
<version>${project.parent.version}</version>
</dependency>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<extension>true</extension>
<strict>false</strict>
<useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
...
<catalog>src/main/jaxb/catalog.xml</catalog>
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaIncludes>
<include>xacml-core-v3-schema-wd-17.xsd</include>
</schemaIncludes>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您注意到,还有一个目录参数指向XML目录文件.这也很重要,因为此目录将使插件能够直接在其maven工件中找到A.xsd文件(在我的示例中为xml.xsd),因此您无需在项目B或其他地方重复该文件.在我的示例中,由于XACML模式按如下所示导入xml.xsd:
You notice that there is also a catalog parameter pointing to the XML catalog file. This is also important because this catalog will enable the plugin to locate the A.xsd file (xml.xsd in my example) directly in its maven artifact, so that you don't need to duplicate the file in project B or elsewhere. In my example, since the XACML schema imports xml.xsd as follows:
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>
... catalog.xml
必须看起来像这样:
... the catalog.xml
must look like this:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<system systemId="http://www.w3.org/2001/xml.xsd" uri="maven:org.ow2.authzforce:authzforce-ce-xmlns-model:jar!/xml.xsd" />
</catalog>
您会注意到uri
参数从其Maven工件引用了xml.xsd
.有关此语法的更多信息以引用Maven工件资源,请参阅 maven-jaxb2-plugin的Wiki .
You notice that the uri
parameter references the xml.xsd
from its Maven artifact. For more info on this syntax to refer to Maven artifact resources, refer to the maven-jaxb2-plugin's wiki.
通常,为了在管理模式位置时提供最大的灵活性和简便性,我建议仅在模式导入中指定名称空间. (否schemaLocation
.)例如,首选:
In general, to allow maximum flexibility and simplicity in managing schema locations, I recommend to specify only the namespace in schema imports. (No schemaLocation
.) For example, prefer this:
<xs:import namespace="http://www.w3.org/XML/1998/namespace" />
...在这种情况下,catalog.xml应如下所示:
... in which case the catalog.xml should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<public publicId="http://www.w3.org/XML/1998/namespace" uri="maven:org.ow2.authzforce:authzforce-ce-xmlns-model:jar!/xml.xsd" />
</catalog>
(在我的示例中并非如此,因为标准委员会的官方XACML模式使用schemaLocation
,因此最好像原始版本一样保留它.)
(This is not the case in my example exceptionally, because the official XACML schema from the standard committee uses a schemaLocation
, so it's preferable to keep it as is like the original.)
这篇关于如何创建常见的XSD生成的Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!