使用 Java EE API 替换已弃用的 JPMS 模块

Replacements for deprecated JPMS modules with Java EE APIs

Java 9 deprecated six modules that contain Java EE APIs and they are going to be removed 即将:

哪些维护的第三方工件提供了这些 API?他们提供这些 API 的能力如何或他们必须提供哪些其他功能并不重要 - 重要的是,它们是否是这些 modules/packages?

的直接替代品

为了更容易收集知识,我用我目前知道的回答,并将答案设为社区wiki。我希望人们会扩展它而不是自己写答案。


在您投票结束之前:

不要使用已弃用的 Java EE 模块,而是使用以下工件。

JAF (java.activation)

JavaBeans Activation Framework(现在是 Jakarta Activation)是一种独立的技术(在 Maven Central 上可用):

<dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>jakarta.activation</artifactId>
    <version>1.2.2</version>
</dependency>

()

CORBA (java.corba)

来自 JEP 320:

There will not be a standalone version of CORBA unless third parties take over maintenance of the CORBA APIs, ORB implementation, CosNaming provider, etc. Third party maintenance is possible because the Java SE Platform endorses independent implementations of CORBA. In contrast, the API for RMI-IIOP is defined and implemented solely within Java SE. There will not be a standalone version of RMI-IIOP unless a dedicated JSR is started to maintain it, or stewardship of the API is taken over by the Eclipse Foundation (the transition of stewardship of Java EE from the JCP to the Eclipse Foundation includes GlassFish and its implementation of CORBA and RMI-IIOP).

JTA (java.transaction)

单机版:

<dependency>
    <groupId>jakarta.transaction</groupId>
    <artifactId>jakarta.transaction-api</artifactId>
    <version>1.3.3</version>
</dependency>

(Source)

JAXB (java.xml.bind)

由于 Java EE 已更名为 Jakarta EE,JAXB 现在由新工件提供:

<!-- API -->
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.3</version>
</dependency>

<!-- Runtime -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.3</version>
    <scope>runtime</scope>
</dependency>

<!-- Alternative runtime -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.3</version>
    <scope>runtime</scope>
</dependency>

JAXB Reference Implementation page.

备选运行时是

schemagenxjc 也可以作为独立 JAXB 发行版的一部分从那里下载。

另见

JAX-WS (java.xml.ws)

参考实现:

<!-- API -->
<dependency>
    <groupId>jakarta.xml.ws</groupId>
    <artifactId>jakarta.xml.ws-api</artifactId>
    <version>2.3.3</version>
</dependency>

<!-- Runtime -->
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>2.3.3</version>
</dependency>

Standalone distribution download(包含wsgenwsimport)。

常用注释(java.xml.ws.annotation)

Java Commons Annotations(在 Maven Central 上可用):

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>1.3.5</version>
</dependency>

()

用于 JDK9 的 JAXB (java.xml.bind)

我在 jdk9/10 EA

上的桌面应用程序完美运行
<properties>
    <jaxb-api.version>2.3.0</jaxb-api.version>
</properties>

<!-- JAXB 2.3.0 for jdk9+ -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>${jaxb-api.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>${jaxb-api.version}</version>
</dependency>
<!-- JAXB needs javax.activation module (jdk9) -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>javax.activation-api</artifactId>
    <version>1.2.0</version>
</dependency>

似乎 jaxws-ri 传递依赖于 commonj.sdo:commonj.sdo:jar:2.1.1.v201112051852 显然可以从存储库 http://download.eclipse.org/rt/eclipselink/maven.repo

中找到

我需要为基于 Spring Boot 2 的应用程序替换 JAX-WS (java.xml.ws) 和 JAXB (java.xml.bind),最后得到这些 JAR (Gradle 构建):

// replacements for deprecated JDK module java.xml.ws
runtimeOnly 'javax.xml.ws:jaxws-api:2.3.0' // javax.xml.ws.* classes
runtimeOnly 'javax.jws:jsr181-api:1.0-MR1' // for javax.jws.* classes

// replacement for deprecated JDK module java.xml.bind
runtimeOnly 'javax.xml.bind:jaxb-api'
runtimeOnly 'org.glassfish.jaxb:jaxb-runtime:2.3.0.1'
runtimeOnly 'org.glassfish:javax.json:1.1.2'
runtimeOnly 'org.eclipse:yasson:1.0.1'

(您可能需要 compile 或其他范围,runtimeOnly 对我们来说已经足够了。)

我注意到 https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core is described as "Old" and using this answer 选择了基于 org.glassfish 的东西,这些东西也带来了 org.eclipse.yasson

现在的情况真的很乱,它确实有效,但是任何人都应该如何确定它是最好的替代品,对吗?

我发现解决这些问题的 JAXB 部分的最简单方法是在我的根 pom 或我的 bom 中使用依赖管理:

    <project ...>
      <dependencyManagement>
        <dependencies>
          <!-- ... -->
          <!-- Gone from jvm in java11 -->
          <dependency>
          <groupId>com.sun.xml.bind</groupId>
          <artifactId>jaxb-ri</artifactId>
          <version>2.4.0-b180830.0438</version>
          <scope>import</scope>
          <type>pom</type>
        </dependency>
        <!-- ... -->
      </dependencies>
    </dependencyManagement>
    </project>

并且在 jdk11 上编译失败的模块中:

    <!-- ... -->
    <dependencies>
      <!-- Gone from jvm in java11 -->
      <dependency>
         <groupId>javax.xml.bind</groupId>
         <artifactId>jaxb-api</artifactId>
      </dependency>
      <dependency>
         <groupId>com.sun.xml.bind</groupId>
         <artifactId>jaxb-impl</artifactId>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>org.glassfish.jaxb</groupId>
         <artifactId>jaxb-runtime</artifactId>
         <scope>runtime</scope>
      </dependency>
      <!-- ... -->
    </dependencies>  
    <!-- ... -->

此外,将 org.jvnet.jaxb2.maven2:maven-jaxb2-plugin 的版本更新为 0.14.0 为我解决了所有 jaxb 生成问题。

只是上述答案的微小变化(改进)--- 此处仅针对 JAXB 进行了举例说明。可以添加具有 runtime 范围的依赖项,并且只有在有效需要时(即在版本 >= 9 的 JRE 中为 运行 构建时 --- 这里以 v11 为例):

<profile>
        <id>when-on-jdk-11</id>
        <activation>
            <jdk>11</jdk>
        </activation>

        <properties>
            <!-- missing artefacts version properties -->
            <jaxb-api.version>2.3.1</jaxb-api.version>
            <jaxb-impl.version>2.3.2</jaxb-impl.version> <!-- one might let it the same with the jaxb-api.version -->
        </properties>

        <dependencies>
            <!-- runtime dependencies to avoid JAXB related CNF exceptions when running on Java 11 (e.g.: ClassNotFoundException: javax.xml.bind.annotation.XmlType) -->
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>${jaxb-api.version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-runtime</artifactId>
                <version>${jaxb-impl.version}</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </profile>

我已经使用 JDK 11.0.3 尝试了上述大部分建议,但没有成功。我最终发现唯一可行的解​​决方案如下。也许还有其他选项也有效,但版本的选择似乎很关键。例如,将 com.sun.xml.ws:rt 更改为 2.3.2 会导致模块 javax.jws 不再可用。

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.4.0-b180830.0438</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>rt</artifactId>
        <version>2.3.1</version>
    </dependency> 

我在 spring mvc 项目中使用 jdk 11 + ant + ivy。 我收到错误“包javax.jws不存在”所以我添加了javax.jws-api-1.1.jarclasspath 并且成功了! 只需从 https://repo1.maven.org/maven2/javax/jws/javax.jws-api/1.1/javax.jws-api-1.1.jar 下载 jar 并将其添加到 build.xml

中的类路径

或者将其添加到您的 pom.xml:

<dependency>
  <groupId>javax.jws</groupId>
  <artifactId>javax.jws-api</artifactId>
  <version>1.1</version>
</dependency>

如果您在 Talend 中遇到此问题(例如 7.x),您可以在项目的默认 POM.xml 中添加:

<dependencies>
    <dependency>
        <groupId>javax.xml.soap</groupId>
        <artifactId>javax.xml.soap-api</artifactId>
        <version>1.4.0</version>
    </dependency>
</dependencies>

测试:

  • 采用JDK 8.0.275.1-热点:OK
  • 采用JDK 11.0.9.101-热点:OK
  • 采用JDK 15.0.1.9-hotspot : KO(但这是另一个问题:不兼容的条件操作数类型Exception和TDieException)
  • Zulu-8.50.0.1017:好的
  • Zulu-11.43.1015:好的

如果您遇到同样的问题,请将以下依赖项添加到 pom.xml

<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>2.3.3</version>
</dependency>

然后使用JAVA 8作为备用JRE。有关详细信息,请参阅对我有用的 to this video

到2022年还在经历这件事确实很痛苦! 我尝试了上面的许多建议,但只能让它与以下依赖项一起工作。

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>
 
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
 
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.1</version>
</dependency>
  
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.25.0-GA</version>
</dependency>

注意:不要试图更新依赖项,就这样吧,它对我有用。