免责声明:我是maven-jaxb2-plugin的作者。
TL;DR 这里有一个test project,它演示了如何做到这一点。
这是可能的,但有点毛,所以请多多包涵。
如果a.xsd、b.xsd和c.xsd在同一个命名空间,a.xsd和b.xsd不能导入c.xsd,只能包含时间>它。我们希望将每个 XSD 生成到自己的包中,例如 test.a、test.b 和 test.c,并在同一个 Maven 项目中完成。
为此,我们需要三个单独的maven-jaxb2-plugin 执行,每个都配置有自己的架构和目标包。例如:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>xjc-a</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>test.a</generatePackage>
<generateDirectory>${project.build.directory}/xjc-a</generateDirectory>
<schemaIncludes>
<includes>a.xsd</includes>
</schemaIncludes>
</configuration>
</execution>
<!-- xjc-b and xjc-c follow -->
</executions>
</plugin>
It is is important to use different target directories for separate executions.
好的,这将创建三个包含三个目标包的目标目录。下一个问题是来自c.xsd 的类将在test.a 和test.b 中生成,这是我们想要避免的。
为了实现这一点,我们必须告诉 XJC 使用来自test.c 的类来处理来自c.xsd 的类型。这实际上是 episode 文件的用途。该文件通常在META-INF\sun-jaxb.episode 下生成,它包含已处理模式中所有类型的绑定。这是为c.xsd 生成的示例:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
<bindings xmlns:tns="urn:test" if-exists="true" scd="x-schema::tns">
<schemaBindings map="false">
<package name="test.c"/>
</schemaBindings>
<bindings if-exists="true" scd="~tns:CType">
<class ref="test.c.CType"/>
</bindings>
</bindings>
</bindings>
剧集文件实际上是一个普通的绑定文件。所以可以直接在编译中使用:
<execution>
<id>xjc-a</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>test.a</generatePackage>
<generateDirectory>${project.build.directory}/xjc-a</generateDirectory>
<schemaIncludes>
<includes>a.xsd</includes>
</schemaIncludes>
<bindings>
<binding>
<fileset>
<directory>${project.build.directory}/xjc-c/META-INF</directory>
<includes>
<include>sun-jaxb.episode</include>
</includes>
</fileset>
</binding>
</bindings>
</configuration>
</execution>
只剩下一个小问题。 XJC生成的剧集文件也包含这个片段:
<schemaBindings map="false">
<!-- ... -->
</schemaBindings>
它实际上是说“不要不为给定命名空间中的模式生成代码”。如果a.xsd 或b.xsd 位于不同的命名空间中,这将不是问题。但由于它们在同一个命名空间中,因此该片段将有效地关闭 a.xsd 或 b.xsd 的所有代码生成。
要解决这个问题,我们可以对为c.xsd 生成的sun-jaxb.episode 进行后处理。这可以通过一个简单的 XSLT 来完成:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="jaxb:schemaBindings"/>
</xsl:stylesheet>
此 XSLT 应在 c.xsd 的代码之后运行,但在 a.xsd 和 b.xsd 的代码生成之前运行。这可以通过将这些执行放入不同的阶段来实现(generate-sources、process-sources、generate-resources)。
以下是完整的pom.xml:
<?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>org.jvnet.jaxb2.maven2</groupId>
<artifactId>divide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.2.11</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>${project.build.directory}/xjc-c/META-INF</dir>
<outputDir>${project.build.directory}/xjc-c/META-INF</outputDir>
<includes>
<include>sun-jaxb.episode</include>
</includes>
<stylesheet>src/main/xslt/removeJaxbSchemaBindings.xslt</stylesheet>
</transformationSet>
</transformationSets>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.3</version>
<executions>
<execution>
<id>xjc-c</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<generatePackage>test.c</generatePackage>
<generateDirectory>${project.build.directory}/xjc-c</generateDirectory>
<schemaIncludes>
<includes>c.xsd</includes>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>xjc-a</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<generatePackage>test.a</generatePackage>
<generateDirectory>${project.build.directory}/xjc-a</generateDirectory>
<schemaIncludes>
<includes>a.xsd</includes>
</schemaIncludes>
<bindings>
<binding>
<fileset>
<directory>${project.build.directory}/xjc-c/META-INF</directory>
<includes>
<include>sun-jaxb.episode</include>
</includes>
</fileset>
</binding>
</bindings>
</configuration>
</execution>
<execution>
<id>xjc-b</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<generatePackage>test.b</generatePackage>
<generateDirectory>${project.build.directory}/xjc-b</generateDirectory>
<schemaIncludes>
<includes>b.xsd</includes>
</schemaIncludes>
<bindings>
<binding>
<fileset>
<directory>${project.build.directory}/xjc-c/META-INF</directory>
<includes>
<include>sun-jaxb.episode</include>
</includes>
</fileset>
</binding>
</bindings>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>