From b71fcb2e1e4c8aeafc9ece92a13659f47e5c6009 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Fri, 2 Aug 2024 17:18:21 +0800 Subject: [PATCH] fix - Use 'java.import.gradle.java.home' when it's specified (#1552) --- .vscode/launch.json | 2 +- extension/src/util/config.ts | 4 ++++ .../github/badsyntax/gradle/GradleBuildRunner.java | 5 +++++ .../badsyntax/gradle/handlers/GetBuildHandler.java | 11 ++++++++--- proto/gradle.proto | 1 + 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 52a4da376..d03f9b61d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -35,7 +35,7 @@ "request": "attach", "hostName": "localhost", "port": "8089", - "projectName": "com.github.badsyntax.gradle" + "projectName": "gradle-server" }, { "name": "Debug Gradle Server & Extension", diff --git a/extension/src/util/config.ts b/extension/src/util/config.ts index c3469acb1..76dd213f5 100644 --- a/extension/src/util/config.ts +++ b/extension/src/util/config.ts @@ -201,6 +201,7 @@ export function getGradleConfig(): GradleConfig { const gradleUserHome = getConfigJavaImportGradleUserHome(); const gradleJvmArguments = getConfigJavaImportGradleJvmArguments(); const gradleVersion = getConfigJavaImportGradleVersion(); + const javaHome = getConfigJavaImportGradleJavaHome(); if (gradleHome !== null) { gradleConfig.setGradleHome(gradleHome); } @@ -213,6 +214,9 @@ export function getGradleConfig(): GradleConfig { if (gradleVersion !== null) { gradleConfig.setVersion(gradleVersion); } + if (javaHome !== null) { + gradleConfig.setJavaHome(javaHome); + } gradleConfig.setWrapperEnabled(getConfigJavaImportGradleWrapperEnabled()); const javaExtension = vscode.extensions.getExtension("redhat.java"); if (javaExtension) { diff --git a/gradle-server/src/main/java/com/github/badsyntax/gradle/GradleBuildRunner.java b/gradle-server/src/main/java/com/github/badsyntax/gradle/GradleBuildRunner.java index 027ae724f..afc2213b8 100644 --- a/gradle-server/src/main/java/com/github/badsyntax/gradle/GradleBuildRunner.java +++ b/gradle-server/src/main/java/com/github/badsyntax/gradle/GradleBuildRunner.java @@ -2,6 +2,7 @@ import com.github.badsyntax.gradle.exceptions.GradleBuildRunnerException; import com.google.common.base.Strings; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -112,6 +113,10 @@ private void runBuild(ProjectConnection connection) throws GradleBuildRunnerExce build.setJvmArguments(gradleConfig.getJvmArguments()); } + if (!Strings.isNullOrEmpty(gradleConfig.getJavaHome())) { + build.setJavaHome(new File(gradleConfig.getJavaHome())); + } + build.run(); } diff --git a/gradle-server/src/main/java/com/github/badsyntax/gradle/handlers/GetBuildHandler.java b/gradle-server/src/main/java/com/github/badsyntax/gradle/handlers/GetBuildHandler.java index decfe478e..7277f8029 100644 --- a/gradle-server/src/main/java/com/github/badsyntax/gradle/handlers/GetBuildHandler.java +++ b/gradle-server/src/main/java/com/github/badsyntax/gradle/handlers/GetBuildHandler.java @@ -121,6 +121,9 @@ public void run() { action.withCancellationToken(cancellationToken).addProgressListener(progressListener, progressEvents) .setStandardOutput(standardOutputListener).setStandardError(standardErrorListener) .setColorOutput(req.getShowOutputColors()); + if (!Strings.isNullOrEmpty(req.getGradleConfig().getJavaHome())) { + action.setJavaHome(new File(req.getGradleConfig().getJavaHome())); + } GradleProjectModel gradleModel = action.run(); if (gradleModel == null) { throw new Exception("Error occurs in querying custom model."); @@ -193,13 +196,15 @@ private Environment buildEnvironment(ProjectConnection connection) { BuildEnvironment environment = buildEnvironment.get(); org.gradle.tooling.model.build.GradleEnvironment gradleEnvironment = environment.getGradle(); org.gradle.tooling.model.build.JavaEnvironment javaEnvironment = environment.getJava(); + String javaHome = Strings.isNullOrEmpty(req.getGradleConfig().getJavaHome()) + ? javaEnvironment.getJavaHome().getAbsolutePath() + : req.getGradleConfig().getJavaHome(); return Environment.newBuilder() .setGradleEnvironment(GradleEnvironment.newBuilder() .setGradleUserHome(gradleEnvironment.getGradleUserHome().getAbsolutePath()) .setGradleVersion(gradleEnvironment.getGradleVersion())) - .setJavaEnvironment( - JavaEnvironment.newBuilder().setJavaHome(javaEnvironment.getJavaHome().getAbsolutePath()) - .addAllJvmArgs(javaEnvironment.getJvmArguments())) + .setJavaEnvironment(JavaEnvironment.newBuilder().setJavaHome(javaHome) + .addAllJvmArgs(javaEnvironment.getJvmArguments())) .build(); } finally { GradleBuildCancellation.clearToken(req.getCancellationKey()); diff --git a/proto/gradle.proto b/proto/gradle.proto index cb232a87f..f8b314836 100644 --- a/proto/gradle.proto +++ b/proto/gradle.proto @@ -153,6 +153,7 @@ message GradleConfig { bool wrapper_enabled = 4; string version = 5; string java_extension_version = 6; + string java_home = 7; } message GradleBuild { GradleProject project = 1; }