maven - 如何在 jaxb2-maven-plugin 版本 2.5.0 中排除剧集文件的生成?

标签 maven xjc jaxb2-maven-plugin jaxb-episode

我使用 xjc jaxb2-maven-plugin 的目标从一组 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>

还有一个名为 example.xsd 的文件(任何有效的 xsd 文件都可以)在 src/main/resources/ 文件夹中:

<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"/>
  </xsd:sequence>
  <xsd:attribute name="OrderDate" type="xsd:date"/>
 </xsd:complexType>

 <xsd:complexType name="USAddress">
  <xsd:sequence>
   <xsd:element name="name"   type="xsd:string"/>
   <xsd:element name="street" type="xsd:string"/>
   <xsd:element name="city"   type="xsd:string"/>
   <xsd:element name="state"  type="xsd:string"/>
   <xsd:element name="zip"    type="xsd:integer"/>
  </xsd:sequence>
  <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
 </xsd:complexType>
</xsd:schema>

<generateEpisode>false</generateEpisode>确保生成的代码没有 episode文件。

我需要升级插件版本到2.5.0 .在这个版本中, generateEpisode 已弃用:

From plugin version 2.4, this parameter will not be used. Instead, episode files are generated by default with all JAXB operations.

Starting with plugin version 2.4, use the parameter episodeFileName to provide a custom name of the generated episode File (or rely on the standard file name STANDARD_EPISODE_FILENAME).

只需更改 version2.5.0 会出现以下构建时错误:

Caused by: java.io.FileNotFoundException: C:\path-to-the-project\target\generated-sources\jaxb\META-INF\JAXB\episode_xjc.xjb

通过切换generateEpisodetrue构建成功,但代码是用剧集文件生成的,我想避免这种情况。 (作为旁注,这证明 generateEpisode 事实上没有被忽略,尽管文档怎么说)。

如果可能的话,如何使用 2.5.0 版插件禁用剧集文件的生成?

最佳答案

经过一些研究,我得出的结论是此功能不再存在。

但是,我找到了两种排除剧集文件的解决方法:

使用 JAXB2 Maven Plugin (maven-jaxb2-plugin)而不是 jaxb2-maven-plugin

JAXB2 Maven Plugin 是一个类似的插件,它仍然支持没有情节文件的生成:

<build>
  <plugins>
    <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>0.14.0</version>
      <executions>
        <execution>
          <id>xjc</id>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
            <episode>false</episode> <!-- skips episode file generation -->
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

使用单独的插件删除文件

在原始插件之后,Apache Maven AntRun plugin可用于删除文件:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <delete>
            <fileset dir="${project.build.directory}/generated-sources/jaxb/META-INF/JAXB" includes="episode*.xjb" />
          </delete>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

这需要挂接到 generate-sources 阶段,以便在代码生成后直接执行。

关于maven - 如何在 jaxb2-maven-plugin 版本 2.5.0 中排除剧集文件的生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62304622/

相关文章:

java - FHIR JAXB XJC 代码生成

maven - 是否有从 pom.xml 中设置 -D 参数的通用方法?

java - 如何在 JAXB2 maven 插件版本 2.4 中定义模式目录?

java - mvn 包后缺少 resteasy 提供程序

java - 用于创建 JAX-WS 网络服务的简单 Maven 设置

java - Spring 父级的依赖项中缺少版本

xml - 复杂扩展 xs :anyType allow textual content?

java - XJC 不在 xs :union 内生成枚举

maven - 从复杂类生成 xsd 时,Jaxb2 maven 插件出错

java - Spring @ContextConfiguration 如何为xml放置正确的位置