Skip to content

Commit ff1184b

Browse files
committed
Throw a more discriptive error message when build commands fail.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent 0dcde52 commit ff1184b

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

src/main/java/com/ibm/cldk/utils/BuildProject.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
1111
import java.nio.file.Paths;
12+
import java.util.AbstractMap;
1213
import java.util.ArrayList;
1314
import java.util.Arrays;
1415
import java.util.List;
@@ -88,13 +89,38 @@ private static String getGradleCmd() {
8889
" }\n" +
8990
"}";
9091

91-
private static boolean commandExists(String command) {
92+
private static AbstractMap.SimpleEntry<Boolean, String> commandExists(String command) {
93+
StringBuilder output = new StringBuilder();
9294
try {
9395
Process process = new ProcessBuilder().directory(new File(projectRootPom)).command(command, "--version").start();
96+
// Read the output stream
97+
BufferedReader reader = new BufferedReader(
98+
new InputStreamReader(process.getInputStream())
99+
);
100+
String line;
101+
while ((line = reader.readLine()) != null) {
102+
output.append(line).append("\n");
103+
}
104+
105+
// Read the error stream
106+
BufferedReader errorReader = new BufferedReader(
107+
new InputStreamReader(process.getErrorStream())
108+
);
109+
while ((line = errorReader.readLine()) != null) {
110+
output.append(line).append("\n");
111+
}
112+
113+
94114
int exitCode = process.waitFor();
95-
return exitCode == 0;
115+
return new AbstractMap.SimpleEntry<>(
116+
exitCode == 0,
117+
output.toString().trim()
118+
);
96119
} catch (IOException | InterruptedException exceptions) {
97-
return false;
120+
return new AbstractMap.SimpleEntry<>(
121+
false,
122+
exceptions.getMessage()
123+
);
98124
}
99125
}
100126

@@ -227,8 +253,9 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
227253
File pomFile = new File(projectRoot, "pom.xml");
228254
if (pomFile.exists()) {
229255
Log.info("Found pom.xml in the project directory. Using Maven to download dependencies.");
230-
if (!commandExists(MAVEN_CMD))
231-
throw new IllegalStateException("Could not find a valid maven command. Could not find " + MAVEN_CMD + " in the project directory or in the system PATH.");
256+
AbstractMap.SimpleEntry<Boolean, String> mavenCheck = commandExists(MAVEN_CMD);
257+
if (!mavenCheck.getKey())
258+
throw new IllegalStateException("Unable to execute Maven command. Attempt failed with message\n" + mavenCheck.getValue());
232259

233260
String[] mavenCommand = {
234261
MAVEN_CMD, "--no-transfer-progress", "-f",
@@ -239,8 +266,9 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
239266
return buildWithTool(mavenCommand);
240267
} else if (new File(projectRoot, "build.gradle").exists() || new File(projectRoot, "build.gradle.kts").exists()) {
241268
Log.info("Found build.gradle or build.gradle.kts in the project directory. Using gradle to download dependencies.");
242-
if (!commandExists(GRADLE_CMD))
243-
throw new IllegalStateException("Could not find a valid Gradle command. Could not find " + GRADLE_CMD + " in the project directory or in the system PATH.");
269+
AbstractMap.SimpleEntry<Boolean, String> gradleCheck = commandExists(GRADLE_CMD);
270+
if (!gradleCheck.getKey())
271+
throw new IllegalStateException("Could not execute Gradle command. Attempt failed with message\n" + gradleCheck.getValue());
244272

245273
Log.info("Found build.gradle[.kts] in the project directory. Using Gradle to download dependencies.");
246274
tempInitScript = Files.writeString(tempInitScript, GRADLE_DEPENDENCIES_TASK);

0 commit comments

Comments
 (0)