Skip to content

Commit c4774bb

Browse files
authored
Pick Gradle distribution based on client JVM version (#7534)
* Pick Gradle distribution based on client JVM version The Tooling API daemon defaults to the same JVM as the client, so the hardcoded 8.12 fallback fails on Java 24+ and Java 25+. Map the running Java version to the lowest Gradle distribution that can host its daemon (8.12 for <=23, 8.14.3 for 24, 9.1.0 for 25+) instead. * Apply review: collapse <Java 25 branch to single 8.14.3 default 8.14.3 supports Java 8 through 24, so a separate 8.12 baseline isn't needed. Only Java 25+ still requires the Gradle 9 line.
1 parent 240bead commit c4774bb

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

rewrite-gradle-tooling-model/model/src/main/java/org/openrewrite/gradle/toolingapi/OpenRewriteModelBuilder.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
8282
} else if (Files.exists(projectDir.toPath().resolve("gradle/wrapper/gradle-wrapper.properties"))) {
8383
connector.useBuildDistribution();
8484
} else {
85-
connector.useGradleVersion("8.12");
85+
connector.useGradleVersion(defaultGradleVersion());
8686
}
8787
connector
8888
// Uncomment to hit breakpoints inside OpenRewriteModelBuilder in unit tests
@@ -132,6 +132,34 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
132132
}
133133
}
134134

135+
/**
136+
* Pick a Gradle distribution compatible with the JVM running the Tooling API client.
137+
* The daemon defaults to the same JVM as the client, so the chosen distribution must support that Java version.
138+
* See <a href="https://docs.gradle.org/current/userguide/compatibility.html">Gradle compatibility matrix</a>.
139+
*/
140+
static String defaultGradleVersion() {
141+
return defaultGradleVersion(javaSpecificationVersion());
142+
}
143+
144+
static String defaultGradleVersion(int javaFeatureVersion) {
145+
if (javaFeatureVersion >= 25) {
146+
return "9.1.0";
147+
}
148+
return "8.14.3";
149+
}
150+
151+
private static int javaSpecificationVersion() {
152+
String version = System.getProperty("java.specification.version", "8");
153+
if (version.startsWith("1.")) {
154+
version = version.substring(2);
155+
}
156+
try {
157+
return Integer.parseInt(version);
158+
} catch (NumberFormatException e) {
159+
return 8;
160+
}
161+
}
162+
135163
/**
136164
* Everywhere except within openrewrite/rewrite itself the plugin will be resolved from some artifact repository
137165
* But within openrewrite/rewrite it comes from a build directory.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.gradle.toolingapi;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
class OpenRewriteModelBuilderTest {
23+
24+
@Test
25+
void java8UsesGradle814() {
26+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(8)).isEqualTo("8.14.3");
27+
}
28+
29+
@Test
30+
void java24UsesGradle814() {
31+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(24)).isEqualTo("8.14.3");
32+
}
33+
34+
@Test
35+
void java25RequiresGradle91() {
36+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(25)).isEqualTo("9.1.0");
37+
}
38+
39+
@Test
40+
void futureJavaVersionsResolveToLatestKnownDistribution() {
41+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(99)).isEqualTo("9.1.0");
42+
}
43+
}

0 commit comments

Comments
 (0)