从 maven-jaxb2-plugin 0.14.0 迁移到 jaxb2-maven-plugin 2.5.0

Sán*_*ETH 9 java migrate maven-jaxb2-plugin jaxb2-maven-plugin

分享一下我的移民经历

<plugin>
   <groupId>org.jvnet.jaxb2.maven2</groupId>
   <artifactId>maven-jaxb2-plugin</artifactId>
   <version>0.14.0</version>
Run Code Online (Sandbox Code Playgroud)

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>jaxb2-maven-plugin</artifactId>
   <version>2.5.0</version>
Run Code Online (Sandbox Code Playgroud)

我们使用原始插件从 WSDL 文件生成源代码。有人请求使用 Java 11 而不是 Java 8,更改后原始插件生成了警告。

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/D:/mr/org/glassfish/jaxb/jaxb-runtime/2.3.0/jaxb-runtime-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Run Code Online (Sandbox Code Playgroud)

由于这个问题仍然被报告为 maven-jaxb2-plugin 中的错误,并且自 2018 年以来该插件没有更新,因此我们决定移动 mojo 插件。

原始执行情况如下:

<execution>
    <id>uniqa</id>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <schemaLanguage>WSDL</schemaLanguage>
        <schemaDirectory>src/main/resources/brokeredinsurance/uniqa/out</schemaDirectory>
        <schemaIncludes>
            <include>*.wsdl</include>
        </schemaIncludes>
        <generatePackage>at.porschebank.brokeredinsurance.uniqa.out</generatePackage>
        <generateDirectory>
            ${project.build.directory}/generated-sources/brokeredinsurance/uniqa/out
        </generateDirectory>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.5</version>
            </plugin>
        </plugins>
        <bindingDirectory>src/main/resources</bindingDirectory>
    </configuration>
</execution>
Run Code Online (Sandbox Code Playgroud)

迁移新插件后

<execution>
    <id>uniqa</id>
    <goals>
        <goal>xjc</goal>
    </goals>
    <configuration>
        <sourceType>wsdl</sourceType>
        <sources>
            <source>src/main/resources/brokeredinsurance/uniqa/out</source>
        </sources>
        <packageName>at.porschebank.brokeredinsurance.uniqa.out</packageName>
        <outputDirectory>
            ${project.build.directory}/generated-sources/brokeredinsurance/uniqa/out
        </outputDirectory>
        <xjbSources>
            <xjbSource>src/main/resources/jaxb-bindings.xjb</xjbSource>
        </xjbSources>
    </configuration>
</execution>  
Run Code Online (Sandbox Code Playgroud)

在新插件中,我们有不同的标签,但从上面的代码中可以明显看出它们的映射。
由于新插件使用不同,我必须goal在新执行中更改。所以它从generatexjc。请参阅此处的插件文档什么是 JAXB2 Maven 插件?关于 xjc 这里jaxb2:testXjc
我们不再使用该<schemaInclude>标签,因为我们的目录只包含 wsdl 文件。此外,我们删除了额外的插件定义,因为新插件不需要它。

总结
迁移到新插件非常容易,但需要一些时间来弄清楚标签的映射。但是,我在迁移过程中遇到了一个问题,因为我们在src/main/resources根目录中包含 xjb 文件的文件夹中拥有所有内容。
文档说<xjbSource>也可以包含目录,什么是有效的,但是当我仅定义文件夹时,出现以下错误:

org.xml.sax.SAXParseException: not an external binding file. The root element must be {http://java.sun.com/xml/ns/jaxb}bindings but it is {http://schemas.xmlsoap.org/wsdl/}definitions
Run Code Online (Sandbox Code Playgroud)

这是显而易见的,因为我们在同一文件夹(子文件夹中)也有 wsdl 文件,并且插件在初始化期间遍历整个内容,并首先找到 wsdl 文件。
因此,就我而言,解决方案是定义文件的路径。之后它立即生成类。

小智 1

我知道这个问题有点老了,但原始插件今年已更新,有很多变化:

  • 一些与maven-jaxb2-plugin相关的存储库已合并到主存储库中,更名为jaxb-tools(jaxb2-basics是其中之一)
  • 版本已对齐,以便更轻松地将 jaxb-plugins 的良好版本与 maven 插件的版本作为目标
  • 该插件已针对 JDK8/JDK11/JDK17 以及 JDK21(所有 LTS java 版本)进行了测试
  • 我们还进行了升级以支持 Jakarta EE9 (JAXB3) 和 EE10 (JAXB4),最新的 JDK11 基线
  • 许多错误已得到修复,我们积极支持该插件的 JAXB2 / JAXB4 版本

如果您有兴趣,可以阅读并遵循迁移指南,因为我们做了一些groupId移动(所有内容现在都在org.jvnet.jaxbgroupId 下发布)并进行了一些artifactId重命名。

对于您的配置,如果仍在 JAXB2 下,您可以移至

<plugin>
   <groupId>org.jvnet.jaxb</groupId>
   <artifactId>jaxb-maven-plugin</artifactId>
   <version>2.0.9</version>
Run Code Online (Sandbox Code Playgroud)

并在配置部分中,将 jaxb2-basics 引用更改为以下内容

<execution>
    <configuration>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>2.0.9</version>
            </plugin>
        </plugins>
Run Code Online (Sandbox Code Playgroud)