Mag*_*lex 2 xjc maven jaxb2-maven-plugin jaxb-episode
我使用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"/>
</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>
Run Code Online (Sandbox Code Playgroud)
<generateEpisode>false</generateEpisode>确保代码是在没有剧集文件的情况下生成的。
我需要将插件的版本升级到2.5.0。在此版本中,generateEpisode已弃用:
从插件版本 2.4 开始,将不再使用此参数。相反,默认情况下会使用所有 JAXB 操作生成剧集文件。
从插件版本 2.4 开始,使用参数 episodeFileName 提供生成的剧集文件的自定义名称(或依赖标准文件名 STANDARD_EPISODE_FILENAME)。
简单地将 更改version为2.5.0会产生以下构建时错误:
引起:java.io.FileNotFoundException: C:\path-to-the-project\target\generated-sources\jaxb\META-INF\JAXB\episode_xjc.xjb
通过切换generateEpisode到true构建是成功的,但是代码是用一个情节文件生成的,我想避免这种情况。(作为旁注,这证明generateEpisode事实上并没有被忽略,尽管文档是这样说的)。
如果可能的话,如何禁用插件版本2.5.0的剧集文件的生成?
经过一些研究,我得出的结论是此功能不再存在。
但是,我发现了两种排除剧集文件的解决方法:
使用JAXB2 Maven 插件 (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>
Run Code Online (Sandbox Code Playgroud)
使用单独的插件删除文件
在原来的插件之后,可以使用Apache Maven AntRun插件来删除文件:
<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>
Run Code Online (Sandbox Code Playgroud)
这需要挂钩到generate-sources阶段,以便在代码生成后直接执行。
| 归档时间: |
|
| 查看次数: |
1775 次 |
| 最近记录: |