我使用JAXB来使用这个API来方便地使用XJC(XML-to-Java)编译器通过命名引用从XML Schema生成的对象模型.它抽象了JAXB上下文的创建,并通过各种背景魔法和反射找到了ObjectFactory方法.它的基本要点是你总是定义一个通用模式,然后任何数字(也可能是0)模式"扩展"一般模式,每个模式产生自己的数据模型.通用模式带有可重用的定义,扩展它的定义使用它们来组成自己的模型.
我现在遇到了我想为多个项目重用通用模式的情况.一般类型定义应该在项目中保持相同,并且一些代码将针对从这些类生成的抽象类构建.所以我需要先为一些通用模式生成类,然后生成那些扩展并单独使用它们的类.我正在使用Maven进行构建过程.
我遇到的问题是从扩展模式中的通用模式解析类型定义.
假设我的通用模式名为"general.xsd",如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.foobar.com/general"
xmlns:gen="http://www.foobar.com/general"
elementFormDefault="qualified" attributeFormDefault="qualified">
<!-- Element (will usually be root) -->
<xs:element name="transmission" type="gen:Transmission" />
<!-- Definition -->
<xs:complexType name="Transmission" abstract="true">
<xs:sequence>
<!-- Generic parts of a transmission would be in here... -->
</xs:sequence>
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
接下来是一个绑定文件来做一些命名自定义并设置输出的包名称:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<!-- Bindings for the general schema -->
<bindings schemaLocation="general.xsd" node="/xs:schema">
<schemaBindings>
<package name="com.foobar.models.general"/>
</schemaBindings>
<bindings node="//xs:complexType[@name='Transmission']">
<!-- Some customization of property names here... …Run Code Online (Sandbox Code Playgroud) 我有2个模式A,B.我在B中重用了一些A元素.
我不使用命名空间.
我正在使用
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.9.0</version>
Run Code Online (Sandbox Code Playgroud)
我已经在模式B中定义了模式A的包含:
<xs:include schemaLocation="classpath:my.schema.A.xsd"/>
Run Code Online (Sandbox Code Playgroud)
和目录为
REWRITE_SYSTEM "classpath:my.schema.A.xsd" "maven:my.schema:schema-a!/A.xsd"
Run Code Online (Sandbox Code Playgroud)
jaxb配置如下:
<configuration>
<generatePackage>my.schema.b</generatePackage>
<schemaIncludes>
<includes>B.xsd</includes>
</schemaIncludes>
<episodes>
<episode>
<groupId>my.schema</groupId>
<artifactId>schema-a</artifactId>
</episode>
</episodes>
<catalog>src/main/catalog/catalog.cat</catalog>
</configuration>
Run Code Online (Sandbox Code Playgroud)
问题在于,每当我指定episode依赖项时,架构都不会生成任何类,即使它包含一些我希望为其生成类的B元素.
[INFO] Parsing input schema(s)...
[INFO] Compiling input schema(s)...
[INFO] Cleaning package directories.
[INFO] Finished execution.
Run Code Online (Sandbox Code Playgroud)
当我删除这一集时,它运作良好,并为模式A生成类 - 我确实想避免.
你有什么建议吗?
一个样本发表在Jaxb情节汇编中
我有一个basic.xsd和另外两个A.xsd和B.xsd。A.xsd并B.xsd转换为两个不同的 java 包,因此我需要同一插件的两个 Maven 执行。
两个 XSD 都引用basic.xsd一些共享类。如果basic.xsd来自不同的项目,我可以通过使用episodes防止重复的类来很好地解决这个问题。
但我怎样才能参考当前的项目呢?
我第一次执行该插件是仅生成basic.xsd其自己的 java 命名空间中的类。之后 和 的执行者A.xsd应该B.xsd了解 生成的内容basic.xsd。
我可以以某种方式指出生成的剧集basic.xsd吗?
有点像
<episodes><episodeFile>basicXSD.episode</episodeFile</episodes>会很好,但据我所知,我只能添加依赖项...:-(
我使用jaxb2-maven-plugin的xjc目标从一组 xsd 文件生成 Java 类。
一个最小的、完整的和可验证的例子是一个带有以下 pom.xml 文件的 Maven 项目:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>jaxb2-maven-episode-test</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/</source>
</sources>
<generateEpisode>false</generateEpisode>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
在src/main/resources/文件夹中有一个名为example.xsd的文件(任何有效的 xsd 文件都可以):
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/PurchaseOrderSchema.xsd"
targetNamespace="http://tempuri.org/PurchaseOrderSchema.xsd"
elementFormDefault="qualified">
<xsd:element name="PurchaseOrder" type="tns:PurchaseOrderType"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="ShipTo" type="tns:USAddress" maxOccurs="2"/>
<xsd:element name="BillTo" type="tns:USAddress"/> …Run Code Online (Sandbox Code Playgroud)