44import org .apache .maven .shared .invoker .*;
55import org .junit .jupiter .api .Test ;
66
7+ import java .io .BufferedReader ;
78import java .io .File ;
89import java .io .IOException ;
10+ import java .io .InputStreamReader ;
911import java .util .Arrays ;
1012import java .util .Map ;
1113
@@ -90,7 +92,8 @@ public MyProcess(String... arguments) {
9092
9193 public MyProcess (ProcessBuilder builder ) {
9294 this .builder = builder ;
93- builder .inheritIO ();
95+ builder .redirectError (ProcessBuilder .Redirect .PIPE );
96+ builder .redirectInput (ProcessBuilder .Redirect .PIPE );
9497 Map <String , String > environment = builder .environment ();
9598 setValueIgnoreCase (environment , "JAVA_HOME" , System .getProperty ("java.home" ));
9699 }
@@ -109,6 +112,31 @@ public void execNow() throws IOException, InterruptedException {
109112 StackTraceElement stackEl = stackTrace [1 ];
110113 System .out .println (stackEl .toString ()+" -> " +commandToString ());
111114 Process p = builder .start ();
115+ // Inheriting IO doesn't work as good as doing the below:
116+ new Thread (() -> {
117+ try {
118+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (p .getInputStream ()))){
119+ String line = null ;
120+ while ((line = reader .readLine ()) != null ){
121+ System .out .println (line );
122+ }
123+ }
124+ } catch (Exception e ) {
125+ throw new RuntimeException (e );
126+ }
127+ }).start ();
128+ new Thread (() -> {
129+ try {
130+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (p .getErrorStream ()))){
131+ String line = null ;
132+ while ((line = reader .readLine ()) != null ){
133+ System .err .println (line );
134+ }
135+ }
136+ } catch (Exception e ) {
137+ throw new RuntimeException (e );
138+ }
139+ }).start ();
112140 int result = p .waitFor ();
113141 if (result != 0 )
114142 throw new IOException ("Process exited with " + result + " instead of 0! Something probably went wrong." );
0 commit comments