21
21
import javax .inject .Named ;
22
22
import javax .inject .Singleton ;
23
23
24
+ import java .io .BufferedReader ;
24
25
import java .io .IOException ;
26
+ import java .io .InputStream ;
27
+ import java .io .InputStreamReader ;
25
28
import java .io .UncheckedIOException ;
26
29
import java .nio .file .Path ;
27
30
import java .nio .file .Paths ;
30
33
import java .util .Optional ;
31
34
32
35
import org .apache .maven .toolchain .Toolchain ;
33
- import org .codehaus .plexus .util .cli .CommandLineException ;
34
- import org .codehaus .plexus .util .cli .CommandLineUtils ;
35
- import org .codehaus .plexus .util .cli .Commandline ;
36
36
import org .slf4j .Logger ;
37
37
import org .slf4j .LoggerFactory ;
38
38
@@ -70,15 +70,12 @@ private Path getCanonicalPath(Path path) {
70
70
71
71
private String getSpecForPath (Path path ) {
72
72
try {
73
- Commandline cl = new Commandline (path .toString ());
74
- cl .createArg ().setValue ("-version" );
75
- CommandLineUtils .StringStreamConsumer out = new CommandLineUtils .StringStreamConsumer ();
76
- CommandLineUtils .StringStreamConsumer err = new CommandLineUtils .StringStreamConsumer ();
77
- CommandLineUtils .executeCommandLine (cl , out , err );
78
- String version = out .getOutput ().trim ();
79
- if (version .isEmpty ()) {
80
- version = err .getOutput ().trim ();
81
- }
73
+ ProcessBuilder processBuilder = new ProcessBuilder (path .toString (), "-version" );
74
+ processBuilder .redirectErrorStream (true );
75
+ Process process = processBuilder .start ();
76
+ String version = readOutput (process .getInputStream ()).trim ();
77
+ process .waitFor ();
78
+
82
79
if (version .startsWith ("javac " )) {
83
80
version = version .substring (6 );
84
81
if (version .startsWith ("1." )) {
@@ -88,12 +85,26 @@ private String getSpecForPath(Path path) {
88
85
}
89
86
return version ;
90
87
} else {
91
- logger .warn ("Unrecognized output form " + path + " -version - " + version );
92
- return null ;
88
+ logger .warn ("Unrecognized output from {}: {}" , processBuilder .command (), version );
93
89
}
94
- } catch (CommandLineException | IndexOutOfBoundsException e ) {
95
- logger .warn ("Failed to execute: " + path + " - " + e .getMessage ());
96
- return null ;
90
+ } catch (IndexOutOfBoundsException | IOException e ) {
91
+ logger .warn ("Failed to execute: {} - {}" , path , e .getMessage ());
92
+ } catch (InterruptedException e ) {
93
+ Thread .currentThread ().interrupt ();
94
+ }
95
+
96
+ return null ;
97
+ }
98
+
99
+ private String readOutput (InputStream inputstream ) throws IOException {
100
+ BufferedReader br = new BufferedReader (new InputStreamReader (inputstream ));
101
+
102
+ StringBuilder output = new StringBuilder ();
103
+ String line ;
104
+ while ((line = br .readLine ()) != null ) {
105
+ output .append (line + System .lineSeparator ());
97
106
}
107
+
108
+ return output .toString ();
98
109
}
99
110
}
0 commit comments