Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
} else if (Files.exists(projectDir.toPath().resolve("gradle/wrapper/gradle-wrapper.properties"))) {
connector.useBuildDistribution();
} else {
connector.useGradleVersion("8.12");
connector.useGradleVersion(defaultGradleVersion());
}
connector
// Uncomment to hit breakpoints inside OpenRewriteModelBuilder in unit tests
Expand Down Expand Up @@ -132,6 +132,34 @@ public static OpenRewriteModel forProjectDirectory(File projectDir, @Nullable Fi
}
}

/**
* Pick a Gradle distribution compatible with the JVM running the Tooling API client.
* The daemon defaults to the same JVM as the client, so the chosen distribution must support that Java version.
* See <a href="https://docs.gradle.org/current/userguide/compatibility.html">Gradle compatibility matrix</a>.
*/
static String defaultGradleVersion() {
return defaultGradleVersion(javaSpecificationVersion());
}

static String defaultGradleVersion(int javaFeatureVersion) {
if (javaFeatureVersion >= 25) {
return "9.1.0";
}
return "8.14.3";
}

private static int javaSpecificationVersion() {
String version = System.getProperty("java.specification.version", "8");
if (version.startsWith("1.")) {
version = version.substring(2);
}
try {
return Integer.parseInt(version);
} catch (NumberFormatException e) {
return 8;
}
}

/**
* Everywhere except within openrewrite/rewrite itself the plugin will be resolved from some artifact repository
* But within openrewrite/rewrite it comes from a build directory.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.gradle.toolingapi;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class OpenRewriteModelBuilderTest {

@Test
void java8UsesGradle814() {
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(8)).isEqualTo("8.14.3");
}

@Test
void java24UsesGradle814() {
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(24)).isEqualTo("8.14.3");
}

@Test
void java25RequiresGradle91() {
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(25)).isEqualTo("9.1.0");
}

@Test
void futureJavaVersionsResolveToLatestKnownDistribution() {
assertThat(OpenRewriteModelBuilder.defaultGradleVersion(99)).isEqualTo("9.1.0");
}
}