9
9
import java .nio .file .Files ;
10
10
import java .nio .file .Path ;
11
11
import java .nio .file .Paths ;
12
+ import java .util .AbstractMap ;
12
13
import java .util .ArrayList ;
13
14
import java .util .Arrays ;
14
15
import java .util .List ;
@@ -54,7 +55,7 @@ private static String getGradleCmd() {
54
55
String gradleWrapperExists = new File (projectRootPom , gradleWrapper ).exists () ? "true" : "false" ;
55
56
56
57
if (new File (projectRootPom , gradleWrapper ).exists ()) {
57
- GRADLE_CMD = gradleWrapper ;
58
+ GRADLE_CMD = String . valueOf ( new File ( projectRootPom , gradleWrapper )) ;
58
59
} else {
59
60
GRADLE_CMD = gradle ;
60
61
}
@@ -88,19 +89,44 @@ private static String getGradleCmd() {
88
89
" }\n " +
89
90
"}" ;
90
91
91
- private static boolean commandExists (String command ) {
92
+ private static AbstractMap .SimpleEntry <Boolean , String > commandExists (String command ) {
93
+ StringBuilder output = new StringBuilder ();
92
94
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
+
94
114
int exitCode = process .waitFor ();
95
- return exitCode == 0 ;
115
+ return new AbstractMap .SimpleEntry <>(
116
+ exitCode == 0 ,
117
+ output .toString ().trim ()
118
+ );
96
119
} catch (IOException | InterruptedException exceptions ) {
97
- return false ;
120
+ return new AbstractMap .SimpleEntry <>(
121
+ false ,
122
+ exceptions .getMessage ()
123
+ );
98
124
}
99
125
}
100
126
101
127
private static boolean buildWithTool (String [] buildCommand ) {
102
128
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 );
104
130
105
131
try {
106
132
Process process = processBuilder .start ();
@@ -125,7 +151,7 @@ private static boolean buildWithTool(String[] buildCommand) {
125
151
* @return true if Maven is installed, false otherwise.
126
152
*/
127
153
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" );
129
155
try {
130
156
Process process = processBuilder .start ();
131
157
BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
@@ -227,8 +253,9 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
227
253
File pomFile = new File (projectRoot , "pom.xml" );
228
254
if (pomFile .exists ()) {
229
255
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 ());
232
259
233
260
String [] mavenCommand = {
234
261
MAVEN_CMD , "--no-transfer-progress" , "-f" ,
@@ -239,8 +266,9 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
239
266
return buildWithTool (mavenCommand );
240
267
} else if (new File (projectRoot , "build.gradle" ).exists () || new File (projectRoot , "build.gradle.kts" ).exists ()) {
241
268
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 ());
244
272
245
273
Log .info ("Found build.gradle[.kts] in the project directory. Using Gradle to download dependencies." );
246
274
tempInitScript = Files .writeString (tempInitScript , GRADLE_DEPENDENCIES_TASK );
0 commit comments