99import java .nio .file .Files ;
1010import java .nio .file .Path ;
1111import java .nio .file .Paths ;
12+ import java .util .AbstractMap ;
1213import java .util .ArrayList ;
1314import java .util .Arrays ;
1415import java .util .List ;
@@ -54,7 +55,7 @@ private static String getGradleCmd() {
5455 String gradleWrapperExists = new File (projectRootPom , gradleWrapper ).exists () ? "true" : "false" ;
5556
5657 if (new File (projectRootPom , gradleWrapper ).exists ()) {
57- GRADLE_CMD = gradleWrapper ;
58+ GRADLE_CMD = String . valueOf ( new File ( projectRootPom , gradleWrapper )) ;
5859 } else {
5960 GRADLE_CMD = gradle ;
6061 }
@@ -88,19 +89,44 @@ 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 {
93- Process process = new ProcessBuilder (command , "--version" ).start ();
95+ 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
101127 private static boolean buildWithTool (String [] buildCommand ) {
102128 Log .info ("Building the project using " + buildCommand [0 ] + "." );
103- ProcessBuilder processBuilder = new ProcessBuilder (buildCommand );
129+ ProcessBuilder processBuilder = new ProcessBuilder (). directory ( new File ( projectRootPom )). command ( buildCommand );
104130
105131 try {
106132 Process process = processBuilder .start ();
@@ -125,7 +151,7 @@ private static boolean buildWithTool(String[] buildCommand) {
125151 * @return true if Maven is installed, false otherwise.
126152 */
127153 private static boolean isMavenInstalled () {
128- ProcessBuilder processBuilder = new ProcessBuilder (MAVEN_CMD , "--version" );
154+ ProcessBuilder processBuilder = new ProcessBuilder (). directory ( new File ( projectRootPom )). command ( MAVEN_CMD , "--version" );
129155 try {
130156 Process process = processBuilder .start ();
131157 BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
@@ -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. I did 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. I did 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