2121import javax .inject .Named ;
2222import javax .inject .Singleton ;
2323
24+ import java .io .BufferedReader ;
2425import java .io .IOException ;
26+ import java .io .InputStream ;
27+ import java .io .InputStreamReader ;
2528import java .io .UncheckedIOException ;
2629import java .nio .file .Path ;
2730import java .nio .file .Paths ;
3033import java .util .Optional ;
3134
3235import 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 ;
3636import org .slf4j .Logger ;
3737import org .slf4j .LoggerFactory ;
3838
@@ -70,15 +70,12 @@ private Path getCanonicalPath(Path path) {
7070
7171 private String getSpecForPath (Path path ) {
7272 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+
8279 if (version .startsWith ("javac " )) {
8380 version = version .substring (6 );
8481 if (version .startsWith ("1." )) {
@@ -88,12 +85,26 @@ private String getSpecForPath(Path path) {
8885 }
8986 return version ;
9087 } else {
91- logger .warn ("Unrecognized output form " + path + " -version - " + version );
92- return null ;
88+ logger .warn ("Unrecognized output from {}: {}" , processBuilder .command (), version );
9389 }
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 ());
97106 }
107+
108+ return output .toString ();
98109 }
99110}
0 commit comments