如何创建常见的XSD生成的Java类 [英] how to create common xsd generated java classes

查看:130
本文介绍了如何创建常见的XSD生成的Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过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

推荐答案

执行 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 针对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类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆