4
4
import org .apache .maven .shared .invoker .*;
5
5
import org .junit .jupiter .api .Test ;
6
6
7
+ import java .io .BufferedReader ;
7
8
import java .io .File ;
8
9
import java .io .IOException ;
10
+ import java .io .InputStreamReader ;
9
11
import java .util .Arrays ;
10
12
import java .util .Map ;
11
13
@@ -90,7 +92,8 @@ public MyProcess(String... arguments) {
90
92
91
93
public MyProcess (ProcessBuilder builder ) {
92
94
this .builder = builder ;
93
- builder .inheritIO ();
95
+ builder .redirectError (ProcessBuilder .Redirect .PIPE );
96
+ builder .redirectInput (ProcessBuilder .Redirect .PIPE );
94
97
Map <String , String > environment = builder .environment ();
95
98
setValueIgnoreCase (environment , "JAVA_HOME" , System .getProperty ("java.home" ));
96
99
}
@@ -109,6 +112,31 @@ public void execNow() throws IOException, InterruptedException {
109
112
StackTraceElement stackEl = stackTrace [1 ];
110
113
System .out .println (stackEl .toString ()+" -> " +commandToString ());
111
114
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 ();
112
140
int result = p .waitFor ();
113
141
if (result != 0 )
114
142
throw new IOException ("Process exited with " + result + " instead of 0! Something probably went wrong." );
0 commit comments