Skip to content

gradle: add java toolchain if missing #5646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sullis
Copy link
Contributor

@sullis sullis commented Jun 20, 2025

What's changed?

This PR modifies

org.openrewrite.gradle.UpdateJavaCompatibility

It introduces a new recipe option:

Boolean addToolchainIfMissing

When this option is set to true, the recipe will attempt to add a Java toolchain block
to the Gradle build file.

What's your motivation?

I want to be able to add a Java toolchain to a Gradle build file

@@ -147,6 +211,11 @@ public K.CompilationUnit visitCompilationUnit(K.CompilationUnit cu, ExecutionCon
if (getCursor().pollMessage(TARGET_COMPATIBILITY_FOUND) == null) {
c = addCompatibilityTypeToSourceFile(c, "target", ctx);
}
if (Boolean.TRUE.equals(addToolchainIfMissing)) {
if (getCursor().pollMessage(JAVA_TOOLCHAIN_FOUND) == null) {
// todo : c = addJavaToolchainToSourceFile(c, ctx);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TODO comment needs to be addressed.

@@ -135,6 +149,56 @@ private G.CompilationUnit addCompatibilityTypeToSourceFile(G.CompilationUnit c,
}
return c;
}

private G.CompilationUnit addJavaToolchainToSourceFile(G.CompilationUnit c, ExecutionContext ctx) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: is there a better way to implement "addJavaToolchainToSourceFile"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is basically what we've done elsewhere. Another option could be a GroovyTemplate (or KotlinTemplate respectively), but overall it's essentially the same.

Comment on lines +84 to +88
@Option(displayName = "Add Java toolchain if missing",
description = "Adds the specified Java toolchain if one is not found.",
required = false)
@Nullable
Boolean addToolchainIfMissing;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea, but we could possibly use DeclarationStyle as the mechanism to describe how you as a user want the version to be defined. There's already the addIfMissing option, so that could work as a possibility.

@@ -135,6 +149,56 @@ private G.CompilationUnit addCompatibilityTypeToSourceFile(G.CompilationUnit c,
}
return c;
}

private G.CompilationUnit addJavaToolchainToSourceFile(G.CompilationUnit c, ExecutionContext ctx) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is basically what we've done elsewhere. Another option could be a GroovyTemplate (or KotlinTemplate respectively), but overall it's essentially the same.

}
newStatements.add(s);
}
c = c.withStatements(newStatements);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we use ListUtils.concat(before, toolchain) like we do on the other side. It's a little more idiomatic that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants