OpenRewrite Java Migration Recipes

repository·main·Indexed 41 days ago

https://github.com/openrewrite/rewrite-migrate-java

An OpenRewrite module providing automated recipes for migrating Java applications across major LTS versions (Java 8, 11, 17, 21, and 25) and Jakarta EE specifications (EE 9, 10, and 11). It automates updating build files, replacing deprecated APIs, handling the javax to jakarta namespace transition, and adopting new language features for Maven and Gradle projects.

Tokens
1.6K
Snippets
0
Records
7
Agent score
1%

What's inside OpenRewrite Java Migrate

  1. Overview of rewrite-migrate-java

    main
    The rewrite-migrate-java module is an OpenRewrite extension providing automated recipes for migrating Java applications to newer versions of the Java language and Jakarta EE specifications. It supports both Maven and Gradle projects and handles tasks such as updating build files, upgrading plugins, replacing deprecated APIs, and adopting new language features.
  2. Understand the Moderne Source Available License

    main

    The software in this repository is licensed under the Moderne Source Available License. This is not an Open Source license as defined by the OSI; it is a source-available license that grants specific usage rights while imposing restrictions on how the software can be commercialized or offered as a service.

    Key Rights Granted

    • Use, Copy, and Distribute Internally: You may use and copy the software for internal purposes.
    • Prepare Derivative Works: You are permitted to create modifications (derivative works) of the software.
    • Patent License: The licensor grants a license under its patent claims covering the software, subject to the limitations and patent retaliation clauses defined in the agreement.

    Critical Restrictions

    • No Service Provision: You may not make the functionality of the software (or any modified version) available to third parties as a service. This includes offering a product or service where the value derives from the software, or allowing third parties to interact with the software remotely via a network.
    • Prohibited Use Cases: The license explicitly prohibits use by entities such as Sourcegraph (including Sourcegraph Batch Changes), Amazon Q Code Transformer, and Broadcom Application Advisor.
    • Notice Preservation: You must not remove or obscure any licensing, copyright, or other notices from the software.
    • Redistribution Requirements: If you redistribute the software or modifications, you must include a copy of this license agreement and, if modified, include prominent notices stating that you have modified the software.
  3. Migrate to Jakarta EE versions

    main

    To handle the transition from the javax namespace to the jakarta namespace and update API versions, use the following recipes:

    • org.openrewrite.java.migrate.jakarta.JavaxMigrationToJakarta: Migrate to Jakarta EE 9 (handles the javax.* to jakarta.* namespace change).
    • org.openrewrite.java.migrate.jakarta.JakartaEE10: Migrate to Jakarta EE 10.
    • org.openrewrite.java.migrate.jakarta.JakartaEE11: Migrate to Jakarta EE 11.

    These recipes cover specifications including Servlet, JPA, EJB, CDI, Bean Validation, JSON-B, JAX-RS, WebSocket, Mail, and JMS, and update related third-party libraries like Jackson, Jetty, and EclipseLink.

  4. Migrate Java versions using composite recipes

    main

    This module provides composite recipes for migrating between Long-Term Support (LTS) versions of Java. Each subsequent recipe is cumulative, meaning it includes all the changes from the previous version's recipe.

    Available recipes:

    • org.openrewrite.java.migrate.Java8toJava11: Migrate from Java 8 to 11.
    • org.openrewrite.java.migrate.UpgradeToJava17: Migrate from Java 11+ to 17.
    • org.openrewrite.java.migrate.UpgradeToJava21: Migrate from Java 17+ to 21.
    • org.openrewrite.java.migrate.UpgradeToJava25: Migrate from Java 21+ to 25.

    These recipes automate:

    • Replacing deprecated APIs with modern equivalents.
    • Updating build files to target the new Java version.
    • Upgrading build plugins for compatibility.
    • Upgrading third-party libraries for the Java module system.
    • Adopting new language features (e.g., text blocks, pattern matching, switch expressions).
  5. Other available migration recipes

    main

    In addition to Java and Jakarta EE migrations, this module provides recipes for:

    • Guava: Replace Guava utilities with Java standard library equivalents.
    • Lombok: Modernization and best practices.
    • JSpecify: Adoption of JSpecify nullability annotations.
    • DataNucleus: Upgrading DataNucleus versions.
  6. Reference: JAXB and JAX-WS artifact mapping

    main

    When migrating to Java 11+ or Jakarta EE 9, JAXB and JAX-WS APIs must be added as explicit dependencies because they were removed from the JDK. Use the following mapping for reference:

    JAXB Artifacts

    Jakarta EE VersionArtifactPackage NamespaceDescription
    Java EE 8javax.xml.bind:jaxb-api:2.3.xjavax.xml.bindJAXB API
    Jakarta EE 8com.sun.xml.bind:jaxb-impl:2.3.xjavax.xml.bindJAXB Reference Implementation
    Jakarta EE 8jakarta.xml.bind:jakarta.xml.bind-api:2.3.xjavax.xml.bindJAXB API
    Jakarta EE 8org.glassfish.jaxb:jaxb-runtime:2.3.xjavax.xml.bindJAXB Reference Implementation
    Jakarta EE 9jakarta.xml.bind:jakarta.xml.bind-api:3.xjakarta.xml.bindJAXB API
    Jakarta EE 9org.glassfish.jaxb:jaxb-runtime:3.xjakarta.xml.bindJAXB Reference Implementation

    JAX-WS Artifacts

    Jakarta EE VersionArtifactPackage NamespaceDescription
    Java EE 8javax.xml.ws:jaxws-api:2.3.1javax.jwsJAX-WS API
    Jakarta EE 8jakarta.xml.ws:jakarta.xml.ws-api:2.3.xjavax.jwsJAX-WS API
    Jakarta EE 8com.sun.xml.ws:jaxws-rt:2.3.xjavax.jwsJAX-WS Reference Implementation
    Jakarta EE 9jakarta.xml.ws:jakarta.xml.ws-api:2.3.xjakarta.jwsJAX-WS API
    Jakarta EE 9com.sun.xml.ws:jaxws-rt:2.3.xjakarta.jwsJAX-WS Reference Implementation
  7. Disable the persistence unit second-level cache

    main

    The JpaCacheProperties recipe migrates JPA cache properties in persistence.xml files to ensure explicit shared cache mode settings. It handles various configurations including OpenJPA, EclipseLink, and standard javax.persistence properties.

    What it does:

    • Sets an explicit value for the <shared-cache-mode> element or the javax.persistence.sharedCache.mode property.
    • If shared-cache-mode is set to UNSPECIFIED, it replaces it with an appropriate value (e.g., NONE, ALL, DISABLE_SELECTIVE, or ENABLE_SELECTIVE) based on existing openjpa.DataCache settings.
    • For persistence.xml version 1.0, it adds an EclipseLink property <property name="eclipselink.cache.shared.default" value="..."/> instead of the standard element.
    • Cleans up redundant properties, such as deleting openjpa.DataCache if it only contains simple true/false values, or removing javax.persistence.sharedCache.mode if a <shared-cache-mode> element is present.