Migrate to Java 11 from Java 8
In this tutorial, we'll use OpenRewrite to perform an automated migration from Java 8 to Java 11. Upgrading an older codebase to Java 11 can be a daunting and time-consuming task. As a developer, you can use OpenRewrite to quickly tackle the most common issues you will encounter with your upgrade.

Example Configuration

The Java 11 migration recipe can be applied by including OpenRewrite's plug-in to your project and including a dependency on rewrite-migrate-java:
Maven
Gradle
pom.xml
1
<plugin>
2
<groupId>org.openrewrite.maven</groupId>
3
<artifactId>rewrite-maven-plugin</artifactId>
4
<version>4.13.0</version>
5
<configuration>
6
<activeRecipes>
7
<recipe>org.openrewrite.java.migrate.Java8toJava11</recipe>
8
</activeRecipes>
9
</configuration>
10
<dependencies>
11
<dependency>
12
<groupId>org.openrewrite.recipe</groupId>
13
<artifactId>rewrite-migrate-java</artifactId>
14
<version>0.10.0</version>
15
</dependency>
16
</dependencies>
17
</plugin>
Copied!
build.gradle
1
plugins {
2
id("java")
3
id("org.openrewrite.rewrite") version("5.12.0")
4
}
5
6
rewrite {
7
activeRecipe("org.openrewrite.java.migrate.Java8toJava11")
8
}
9
10
repositories {
11
mavenCentral() // rewrite-spring is published to Maven Central
12
}
13
14
dependencies {
15
rewrite("org.openrewrite.recipe:rewrite-migrate-java:0.10.0")
16
17
// Other project dependencies
18
}
Copied!
At this point, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.

Before and After

For the full list of changes this recipe will make, see its reference page.
Example Class (Before)
Example Class (After)
1
package org.openrewrite.example;
2
3
import java.math.BigDecimal;
4
5
public class Example {
6
Boolean bool = new Boolean(true);
7
Byte b = new Byte("1");
8
Character c = new Character('c');
9
Double d = new Double(1.0);
10
Float f = new Float(1.1f);
11
Long l = new Long(1);
12
Short sh = new Short("12");
13
short s3 = 3;
14
Short sh3 = new Short(s3);
15
Integer i = new Integer(1);
16
17
void divide() {
18
BigDecimal bd = BigDecimal.valueOf(10);
19
BigDecimal bd2 = BigDecimal.valueOf(2);
20
bd.divide(bd2, BigDecimal.ROUND_DOWN);
21
bd.divide(bd2, 1);
22
bd.divide(bd2, 1, BigDecimal.ROUND_CEILING);
23
bd.divide(bd2, 1, 1);
24
bd.setScale(2, 1);
25
}
26
}
Copied!
1
package org.openrewrite.example;
2
3
import java.math.BigDecimal;
4
5
public class Example {
6
Boolean bool = Boolean.valueOf(true);
7
Byte b = Byte.valueOf("1");
8
Character c = Character.valueOf('c');
9
Double d = Double.valueOf(1.0);
10
Float f = Float.valueOf(1.1f);
11
Long l = Long.valueOf(1);
12
Short sh = Short.valueOf("12");
13
short s3 = 3;
14
Short sh3 = Short.valueOf(s3);
15
Integer i = Integer.valueOf(1);
16
17
void divide() {
18
BigDecimal bd = BigDecimal.valueOf(10);
19
BigDecimal bd2 = BigDecimal.valueOf(2);
20
bd.divide(bd2, RoundingMode.DOWN);
21
bd.divide(bd2, RoundingMode.DOWN);
22
bd.divide(bd2, 1, RoundingMode.CEILING);
23
bd.divide(bd2, 1, RoundingMode.DOWN);
24
bd.setScale(2, RoundingMode.DOWN);
25
}
26
}
Copied!
The above example class demonstrates the two most common code migration tasks when moving to Java 11. There are many additional tasks covered by this recipe that are not represented in this example.
Maven pom (Before)
Maven pom (After)
1
<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">
2
<modelVersion>4.0.0</modelVersion>
3
4
<groupId>org.openrewrite.example</groupId>
5
<artifactId>jaxb-project</artifactId>
6
<version>1.0</version>
7
<name>jaxb-project</name>
8
9
<properties>
10
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11
<java.version>1.8</java.version>
12
<maven-jaxb2-plugin.version>0.14.0</maven-jaxb2-plugin.version>
13
</properties>
14
15
<dependencies>
16
<dependency>
17
<groupId>org.springframework.boot</groupId>
18
<artifactId>spring-boot-starter</artifactId>
19
</dependency>
20
<dependency>
21
<groupId>org.springframework.boot</groupId>
22
<artifactId>spring-boot-starter-web-services</artifactId>
23
</dependency>
24
<dependency>
25
<groupId>org.springframework.boot</groupId>
26
<artifactId>spring-boot-starter-test</artifactId>
27
<scope>test</scope>
28
<exclusions>
29
<exclusion>
30
<groupId>org.junit.vintage</groupId>
31
<artifactId>junit-vintage-engine</artifactId>
32
</exclusion>
33
</exclusions>
34
</dependency>
35
</dependencies>
36
<build>
37
<plugins>
38
<plugin>
39
<groupId>org.jvnet.jaxb2.maven2</groupId>
40
<artifactId>maven-jaxb2-plugin</artifactId>
41
<version>${maven-jaxb2-plugin.version}</version>
42
<executions>
43
<execution>
44
<goals>
45
<goal>generate</goal>
46
</goals>
47
</execution>
48
</executions>
49
<configuration>
50
<args>
51
<arg>-XautoNameResolution</arg>
52
</args>
53
<schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
54
<schemaIncludes>
55
<include>*.wsdl</include>
56
</schemaIncludes>
57
<generateDirectory>${project.basedir}/src/main/java</generateDirectory>
58
</configuration>
59
</plugin>
60
</plugins>
61
</build>
62
</project>
Copied!
1
<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">
2
<modelVersion>4.0.0</modelVersion>
3
4
<groupId>org.openrewrite.example</groupId>
5
<artifactId>jaxb-project</artifactId>
6
<version>1.0</version>
7
<name>jaxb-project</name>
8
9
<properties>
10
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11
<java.version>11</java.version>
12
<maven-jaxb2-plugin.version>2.5.0</maven-jaxb2-plugin.version>
13
</properties>
14
15
<dependencies>
16
<dependency>
17
<groupId>org.springframework.boot</groupId>
18
<artifactId>spring-boot-starter</artifactId>
19
</dependency>
20
<dependency>
21
<groupId>org.springframework.boot</groupId>
22
<artifactId>spring-boot-starter-web-services</artifactId>
23
</dependency>
24
<dependency>
25
<groupId>org.springframework.boot</groupId>
26
<artifactId>spring-boot-starter-test</artifactId>
27
<scope>test</scope>
28
<exclusions>
29
<exclusion>
30
<groupId>org.junit.vintage</groupId>
31
<artifactId>junit-vintage-engine</artifactId>
32
</exclusion>
33
</exclusions>
34
</dependency>
35
<!-- JAXB API -->
36
<dependency>
37
<groupId>jakarta.xml.bind</groupId>
38
<artifactId>jakarta.xml.bind-api</artifactId>
39
<version>2.3.3</version>
40
</dependency>
41
<!-- JAXB runtime -->
42
<dependency>
43
<groupId>org.glassfish.jaxb</groupId>
44
<artifactId>jaxb-runtime</artifactId>
45
<version>2.3.4</version>
46
<scope>runtime</scope>
47
</dependency>
48
</dependencies>
49
<build>
50
<plugins>
51
<plugin>
52
<groupId>org.jvnet.jaxb2.maven2</groupId>
53
<artifactId>maven-jaxb2-plugin</artifactId>
54
<version>${maven-jaxb2-plugin.version}</version>
55
<executions>
56
<execution>
57
<goals>
58
<goal>generate</goal>
59
</goals>
60
</execution>
61
</executions>
62
<configuration>
63
<args>
64
<arg>-XautoNameResolution</arg>
65
</args>
66
<schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
67
<schemaIncludes>
68
<include>*.wsdl</include>
69
</schemaIncludes>
70
<generateDirectory>${project.basedir}/src/main/java</generateDirectory>
71
</configuration>
72
</plugin>
73
<plugin>
74
<groupId>org.apache.maven.plugins</groupId>
75
<artifactId>maven-jdeprscan-plugin</artifactId>
76
<version>3.0.0-alpha-1</version>
77
<configuration>
78
<release>11</release>
79
</configuration>
80
</plugin>
81
</plugins>
82
</build>
83
</project>
Copied!
The JAXB and JAX-WS dependencies will only be added to the project if types from those projects are detected.
If you want to know when dependency management for Gradle will be added to rewrite, follow this issue on our roadmap.

Known Limitations

The rewrite-migrate-java project is in its infancy and we are always looking for feedback on which tasks should be prioritized. If you have a specific use case that is not yet covered by this project, please reach out to our team!
The following is a list of known limitations/issues:
    The current recipe adds the JAXB and JAX-WS artifacts available from the Jakarta project. If your organization uses an alternate runtime, this may not be desirable.
    There are several build tool plugins that are used to generate Java code from WSDL files and this recipe currently does not automate this process. You will need to manually update the related build plugins.
    There are several additional libraries that have been removed from the Java 11 standard library that are not included with this recipe: java.corba, javax.transaction, javax.batch, JavaFX, Web Start. If you require support for one of these libraries, please reach out to our team or create an issue.
    This recipe does not cover all deprecations listed by the jdeprscan tool. Please see the following these issue (#6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16) for a list of those deprecations covered/not covered and submit an issue if you are impacted by a deprecation that is not covered by this recipe.
Last modified 10d ago