![](/img/trans.png)
[英]maven-jaxb2-plugin: Error in parsing schema location even when xsd and xjb files are in same location
[英]maven-jaxb2-plugin reusing commons XSD within same project
我有一个项目,它有一个模式A和B,都在同一名称空间内。 两者都导入模式C,它也使用相同的命名空间。 如何为A和B生成JAXB类以分离包,同时重用从生成的C到commons包的JAXB类?
我已经知道我应该使用剧集并使用为模式C生成的剧集作为模式A和B的单独执行的绑定文件。问题是我不知道如何引用这个生成的剧集文件。
这是一个例子:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>generate-sources-C</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.mymodel.commons</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/xjc-commons</generateDirectory>
<schemas>
<schema><url>src/main/resources/xsd/mymodel/c.xsd</url></schema>
</schemas>
</configuration>
</execution>
<execution>
<id>generate-sources-A</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.mymodel.a</generatePackage>
<schemas>
<schema><url>src/main/resources/xsd/mymodel/a.xsd</url></schema>
</schemas>
</configuration>
</execution>
<execution>
<id>generate-sources-B</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.mymodel.b</generatePackage>
<schemas>
<schema><url>src/main/resources/xsd/mymodel/b.xsd</url></schema>
</schemas>
</configuration>
</execution>
</executions>
</plugin>
这会导致在下面创建一个剧集文件:
target/generated-sources/xjc-commons/META-INF/sun-jaxb.episode
如何在A和B的执行中引用此剧集/绑定文件? 使用Episodes只提到如何从其他jar依赖项中引用一个剧集文件(或者我根本不理解它,这更有可能)。
我已经看到一个较旧的答案建议将它作为参数-b
传递给XJC,但这似乎对我没有任何作用。 我仍然最终生成了三次与C相同的类。
免责声明:我是maven-jaxb2-plugin的作者 。
TL; DR这是一个测试项目 ,演示如何执行此操作。
这是可能的,但有点毛茸茸,所以请耐心等待。
如果a.xsd
, b.xsd
和c.xsd
在同一名称空间中,则a.xsd
和b.xsd
无法导入 c.xsd
,它们只能包含它。 我们想要将每个XSD生成到它自己的包中,比如test.a
, test.b
和test.c
,并在同一个Maven项目中完成。
为此,我们需要三个单独执行的maven-jaxb2-plugin
,每个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>
好的,这会创建三个目标目录,包含三个目标包。 接下来的问题是c.xsd
中的类将在test.a
和test.b
生成,我们要避免这些类。
为此,我们必须告诉XJC使用test.c
中的类来c.xsd
类型。 这实际上是剧集文件的用途。 此文件通常在META-INF\\sun-jaxb.episode
下生成,它包含已处理模式中所有类型的绑定。 这是为c.xsd
生成的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
进行c.xsd
。 这可以通过简单的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.