Skip to content

Commit ca26543

Browse files
timtebeekclaudegithub-actions[bot]
authored
Change Docker image version in Java upgrades (#980)
* Change Docker image version in Java upgrades * Extract UpgradeDockerImageVersion to separate recipe Move Docker image version upgrade logic from UpgradeJavaVersion into its own dedicated recipe class with corresponding tests. Co-Authored-By: Claude Opus 4.5 <[email protected]> * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Change Docker image version in Java upgrades * Apply suggestions from code review * Add Debian and Ubuntu codename suffixes for Docker image upgrades Add support for more Docker image tag suffixes: - Ubuntu: bionic (18.04) - Debian: bookworm, bullseye, buster - Debian slim variants: slim-bookworm, slim-bullseye, slim-buster - Generic: -slim Co-Authored-By: Claude Opus 4.5 <[email protected]> --------- Co-authored-by: Claude Opus 4.5 <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 6e525a1 commit ca26543

File tree

6 files changed

+185
-15
lines changed

6 files changed

+185
-15
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
implementation("org.openrewrite:rewrite-json")
5454
implementation("org.openrewrite:rewrite-maven")
5555
implementation("org.openrewrite:rewrite-gradle")
56+
implementation("org.openrewrite:rewrite-docker")
5657
implementation("org.openrewrite.recipe:rewrite-github-actions:$rewriteVersion")
5758
implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion")
5859
implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion")
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.openrewrite.Option;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.docker.ChangeFrom;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
@EqualsAndHashCode(callSuper = false)
28+
@Value
29+
public class UpgradeDockerImageVersion extends Recipe {
30+
31+
@Option(displayName = "Java version",
32+
description = "The Java version to upgrade to.",
33+
example = "11")
34+
Integer version;
35+
36+
String displayName = "Upgrade Docker image Java version";
37+
String description = "Upgrade Docker image tags to use the specified Java version. " +
38+
"Updates common Java Docker images including eclipse-temurin, amazoncorretto, azul/zulu-openjdk, " +
39+
"and others. Also migrates deprecated images (openjdk, adoptopenjdk) to eclipse-temurin.";
40+
41+
@Override
42+
public List<Recipe> getRecipeList() {
43+
List<Recipe> recipes = new ArrayList<>();
44+
if (version == null) { // for uninitialized version
45+
return recipes;
46+
}
47+
// Deprecated images -> migrate to eclipse-temurin
48+
String[] deprecatedImages = {"openjdk", "adoptopenjdk"};
49+
String[] currentImages = {
50+
"eclipse-temurin", "amazoncorretto", "azul/zulu-openjdk",
51+
"bellsoft/liberica-openjdk-debian", "bellsoft/liberica-openjdk-alpine",
52+
"bellsoft/liberica-openjdk-centos", "ibm-semeru-runtimes", "sapmachine"
53+
};
54+
// Common tag suffixes to preserve when upgrading current images
55+
// Longer suffixes must come before shorter ones to match correctly
56+
String[] commonSuffixes = {
57+
// Alpine
58+
"-jdk-alpine", "-jre-alpine",
59+
// Ubuntu LTS
60+
"-jdk-noble", "-jre-noble",
61+
"-jdk-jammy", "-jre-jammy",
62+
"-jdk-focal", "-jre-focal",
63+
"-jdk-bionic", "-jre-bionic",
64+
// Debian
65+
"-jdk-slim-bookworm", "-jre-slim-bookworm",
66+
"-jdk-slim-bullseye", "-jre-slim-bullseye",
67+
"-jdk-slim-buster", "-jre-slim-buster",
68+
"-jdk-bookworm", "-jre-bookworm",
69+
"-jdk-bullseye", "-jre-bullseye",
70+
"-jdk-buster", "-jre-buster",
71+
// Other Linux
72+
"-jdk-centos7", "-jre-centos7",
73+
"-jdk-ubi9-minimal", "-jre-ubi9-minimal",
74+
// Windows
75+
"-jdk-nanoserver", "-jre-nanoserver",
76+
"-jdk-windowsservercore", "-jre-windowsservercore",
77+
// Generic suffixes (must come last)
78+
"-alpine", "-slim",
79+
"-jdk", "-jre"
80+
};
81+
for (int oldVersion = 8; oldVersion < version; oldVersion++) {
82+
// Deprecated images: match specific suffixes first to preserve them
83+
for (String image : deprecatedImages) {
84+
for (String suffix : commonSuffixes) {
85+
recipes.add(new ChangeFrom(image, oldVersion + suffix, null, null, "eclipse-temurin", version + suffix, null, null));
86+
}
87+
}
88+
// Deprecated images: fall back to wildcard for remaining patterns
89+
for (String image : deprecatedImages) {
90+
recipes.add(new ChangeFrom(image, oldVersion + "*", null, null, "eclipse-temurin", version.toString(), null, null));
91+
}
92+
// Current images: match specific suffixes first to preserve them
93+
for (String image : currentImages) {
94+
for (String suffix : commonSuffixes) {
95+
recipes.add(new ChangeFrom(image, oldVersion + suffix, null, null, null, version + suffix, null, null));
96+
}
97+
}
98+
// Current images: fall back to wildcard for remaining patterns
99+
for (String image : currentImages) {
100+
recipes.add(new ChangeFrom(image, oldVersion + "*", null, null, null, version.toString(), null, null));
101+
}
102+
}
103+
return recipes;
104+
}
105+
}

src/main/java/org/openrewrite/java/migrate/UpgradeJavaVersion.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public class UpgradeJavaVersion extends Recipe {
4343
String displayName = "Upgrade Java version";
4444

4545
String description = "Upgrade build plugin configuration to use the specified Java version. " +
46-
"This recipe changes `java.toolchain.languageVersion` in `build.gradle(.kts)` of gradle projects, " +
47-
"or maven-compiler-plugin target version and related settings. " +
48-
"Will not downgrade if the version is newer than the specified version.";
46+
"This recipe changes `java.toolchain.languageVersion` in `build.gradle(.kts)` of gradle projects, " +
47+
"or maven-compiler-plugin target version and related settings. " +
48+
"Will not downgrade if the version is newer than the specified version.";
4949

5050
@Override
5151
public List<Recipe> getRecipeList() {
@@ -54,7 +54,8 @@ public List<Recipe> getRecipeList() {
5454
new UpdateMavenProjectPropertyJavaVersion(version),
5555
new org.openrewrite.jenkins.UpgradeJavaVersion(version, null),
5656
new UpdateJavaCompatibility(version, null, null, false, null),
57-
new UpdateSdkMan(String.valueOf(version), null)
57+
new UpdateSdkMan(String.valueOf(version), null),
58+
new UpgradeDockerImageVersion(version)
5859
);
5960
}
6061

@@ -63,7 +64,7 @@ public List<Recipe> getRecipeList() {
6364
*
6465
* @return Zero estimated time.
6566
*/
66-
Duration estimatedEffortPerOccurrence = Duration.ofMinutes( 0 );
67+
Duration estimatedEffortPerOccurrence = Duration.ofMinutes(0);
6768

6869
@Override
6970
public TreeVisitor<?, ExecutionContext> getVisitor() {

0 commit comments

Comments
 (0)