Skip to content

Commit 6cd73c9

Browse files
committed
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.
1 parent 23be80c commit 6cd73c9

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

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

Lines changed: 32 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,37 @@ 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+
if (javaFeatureVersion >= 24) {
149+
return "8.14.3";
150+
}
151+
return "8.12";
152+
}
153+
154+
private static int javaSpecificationVersion() {
155+
String version = System.getProperty("java.specification.version", "8");
156+
if (version.startsWith("1.")) {
157+
version = version.substring(2);
158+
}
159+
try {
160+
return Integer.parseInt(version);
161+
} catch (NumberFormatException e) {
162+
return 8;
163+
}
164+
}
165+
135166
/**
136167
* Everywhere except within openrewrite/rewrite itself the plugin will be resolved from some artifact repository
137168
* But within openrewrite/rewrite it comes from a build directory.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 java8FallsBackToBaselineDistribution() {
26+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(8)).isEqualTo("8.12");
27+
}
28+
29+
@Test
30+
void java23StaysOnBaselineDistribution() {
31+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(23)).isEqualTo("8.12");
32+
}
33+
34+
@Test
35+
void java24RequiresGradle814() {
36+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(24)).isEqualTo("8.14.3");
37+
}
38+
39+
@Test
40+
void java25RequiresGradle91() {
41+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(25)).isEqualTo("9.1.0");
42+
}
43+
44+
@Test
45+
void futureJavaVersionsResolveToLatestKnownDistribution() {
46+
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(99)).isEqualTo("9.1.0");
47+
}
48+
}

0 commit comments

Comments
 (0)