Skip to content

Commit

Permalink
fix: set GradleExecution jdk use java.import.gradle.java.home (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiaaming authored May 29, 2024
1 parent 44a4bfc commit 4e76ebc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { GradleExecution } from "./GradleExecution";
import { execAsync } from "../../../util/execAsync";
import { getConfigJavaImportGradleJavaHome } from "../../../util/config";
import { logger } from "../../../logger";

export class GradleLocalInstallation implements GradleExecution {
private gradleHomePath: string;

Expand All @@ -15,13 +18,17 @@ export class GradleLocalInstallation implements GradleExecution {
const command = `${this.gradleHomePath} ${args.join(" ")}`;

try {
const { stdout, stderr } = await execAsync(command);
const jdkPath = getConfigJavaImportGradleJavaHome();
const env = jdkPath ? { ...process.env, JAVA_HOME: jdkPath } : process.env;

const { stdout, stderr } = await execAsync(command, { env });
if (stderr) {
throw new Error(`Error running gradle: ${stderr}`);
logger.error(stderr);
}
return stdout;
} catch (error) {
throw new Error(`Error running gradle: ${error.message}`);
logger.error(error.message);
throw new Error(`Error running gradle local installation: ${error.message}`);
}
}
}
11 changes: 9 additions & 2 deletions extension/src/views/gradleDaemons/services/GradleWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import * as fse from "fs-extra";
import { execAsync } from "../../../util/execAsync";
import { GradleExecution } from "./GradleExecution";
import * as path from "path";
import { getConfigJavaImportGradleJavaHome } from "../../../util/config";
import { logger } from "../../../logger";

export class GradleWrapper implements GradleExecution {
private gradleWrapperPath: string;

constructor(private projectRoot: string) {
const wrapperName = process.platform === "win32" ? "gradlew.bat" : "gradlew";
this.gradleWrapperPath = path.join(projectRoot, wrapperName);
Expand All @@ -17,12 +20,16 @@ export class GradleWrapper implements GradleExecution {

const command = `${this.gradleWrapperPath} ${args.join(" ")}`;
try {
const { stdout, stderr } = await execAsync(command, { cwd: this.projectRoot });
const jdkPath = getConfigJavaImportGradleJavaHome();
const env = jdkPath ? { ...process.env, JAVA_HOME: jdkPath } : process.env;

const { stdout, stderr } = await execAsync(command, { cwd: this.projectRoot, env });
if (stderr) {
throw new Error(`Error running gradle wrapper: ${stderr}`);
logger.error(stderr);
}
return stdout;
} catch (error) {
logger.error(error.message);
throw new Error(`Error running gradle wrapper: ${error.message}`);
}
}
Expand Down

0 comments on commit 4e76ebc

Please sign in to comment.