栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Maven-jaxb2-plugin在同一项目中重用公共XSD

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Maven-jaxb2-plugin在同一项目中重用公共XSD

免责声明: 我是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
,每个执行均配置有自己的架构和目标包。例如:

        <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>

对不同的执行使用不同的目标目录很重要。

好的,这将创建具有三个目标软件包的三个目标目录。下一个问题是from

c.xsd
中将生成类,
test.a
并且
test.b
我们希望避免此类。

为此,我们必须告诉XJC对from

test.c
的类型使用from 类
c.xsd
。这实际上是 情节 文件的用途。该文件通常在下生成,
meta-INFsun-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
or的所有代码生成
b.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.prehaus.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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506300.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号