Skip to content

Commit 4bee03d

Browse files
committed
Update Gradle commands to remove -b flag
1 parent 9b431ca commit 4bee03d

1 file changed

Lines changed: 42 additions & 13 deletions

File tree

src/util/commandUtils.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* IBM Confidential
3-
* Copyright IBM Corp. 2024
3+
* Copyright IBM Corp. 2024, 2026
44
*/
55

66
import * as Path from "path";
@@ -52,16 +52,16 @@ export async function getCommandForMaven(pomPath: string, command: string, termi
5252
*/
5353
export async function getCommandForGradle(buildGradlePath: string, command: string, terminalType?: String, customCommand?: string): Promise<string> {
5454
let gradleCmdStart = await gradleCmd(buildGradlePath);
55+
const projectDir = Path.dirname(buildGradlePath);
5556

5657
if (gradleCmdStart === "gradle") {
57-
return formDefaultCommand(gradleCmdStart, buildGradlePath, command, "-b=", customCommand);
58+
return formGradleCommand(projectDir, "gradle", command, customCommand);
5859
}
5960
//checking the OS type for command customization
6061
if (isWin()) {
61-
return getGradleCommandForWin(gradleCmdStart, buildGradlePath, command, terminalType, customCommand);
62+
return getGradleCommandForWin(gradleCmdStart, projectDir, command, terminalType, customCommand);
6263
} else {
63-
gradleCmdStart = Path.join(gradleCmdStart, "gradlew");
64-
return formDefaultCommandWithPath(gradleCmdStart, buildGradlePath, command, "-b=", customCommand);
64+
return formGradleCommand(projectDir, "./gradlew", command, customCommand);
6565
}
6666
}
6767

@@ -158,20 +158,17 @@ async function getLocalMavenWrapperDir(projectFolder: string): Promise<string |
158158
/**
159159
* Returns the gradle command for windows OS based on the terminal configured
160160
*/
161-
function getGradleCommandForWin(gradleCmdStart: string, buildGradlePath: string, command: string, terminalType?: String, customCommand?: string): string {
161+
function getGradleCommandForWin(gradleCmdStart: string, projectDir: string, command: string, terminalType?: String, customCommand?: string): string {
162162
switch (terminalType) {
163163
case ShellType.GIT_BASH:
164-
gradleCmdStart = Path.join(gradleCmdStart, "gradlew");
165-
return formDefaultCommandWithPath(gradleCmdStart, buildGradlePath, command, "-b=", customCommand); //Bash
164+
return formGradleCommand(projectDir, "./gradlew", command, customCommand); //Bash
166165
case ShellType.POWERSHELL:
167-
gradleCmdStart = Path.join(gradleCmdStart, "gradlew.bat");
168-
return formPowershellCommand(gradleCmdStart, buildGradlePath, command, "-b=", customCommand);
166+
return formGradlePowershellCommand(projectDir, "gradlew.bat", command, customCommand);
169167
case ShellType.WSL:
170-
return formLinuxBasedCommand(toDefaultWslPath(gradleCmdStart), command, "./gradlew ", customCommand); //Wsl
168+
return formGradleCommand(toDefaultWslPath(projectDir), "./gradlew", command, customCommand); //Wsl
171169
default:
172170
// The default case is ShellType CMD or OTHERS
173-
gradleCmdStart = Path.join(gradleCmdStart, "gradlew.bat");
174-
return formDefaultCommandWithPath(gradleCmdStart, buildGradlePath, command, "-b=", customCommand);
171+
return formGradleWindowsCommand(projectDir, "gradlew.bat", command, customCommand);
175172
}
176173
}
177174

@@ -235,6 +232,38 @@ function formDefaultCommandWithPath(projectPath: string, buildFilePath: String,
235232
return "\"" + projectPath + "\" " + `${command}` + ` ${cmdOption}"${buildFilePath}"`;
236233
}
237234

235+
/**
236+
* Returns the Gradle command by cd'ing into the project directory
237+
* This approach is compatible with all Gradle versions including Gradle 9+
238+
* where the -b option was removed
239+
*/
240+
function formGradleCommand(projectDir: string, gradleCmd: string, command: string, customCommand?: string): string {
241+
if (customCommand) {
242+
return `cd "${projectDir}" && ${gradleCmd} ${command} ${customCommand}`;
243+
}
244+
return `cd "${projectDir}" && ${gradleCmd} ${command}`;
245+
}
246+
247+
/**
248+
* Returns the Gradle command for PowerShell by cd'ing into the project directory
249+
*/
250+
function formGradlePowershellCommand(projectDir: string, gradleCmd: string, command: string, customCommand?: string): string {
251+
if (customCommand) {
252+
return `cd "${projectDir}"; .\\${gradleCmd} ${command} ${customCommand}`;
253+
}
254+
return `cd "${projectDir}"; .\\${gradleCmd} ${command}`;
255+
}
256+
257+
/**
258+
* Returns the Gradle command for Windows CMD by cd'ing into the project directory
259+
*/
260+
function formGradleWindowsCommand(projectDir: string, gradleCmd: string, command: string, customCommand?: string): string {
261+
if (customCommand) {
262+
return `cd "${projectDir}" && ${gradleCmd} ${command} ${customCommand}`;
263+
}
264+
return `cd "${projectDir}" && ${gradleCmd} ${command}`;
265+
}
266+
238267
/**
239268
* Reused from vscode-maven - currentWindowsShell()
240269
* https://github.com/microsoft/vscode-maven/blob/main/src/mavenTerminal.ts

0 commit comments

Comments
 (0)