44import java .io .File ;
55import java .io .IOException ;
66import java .io .InputStreamReader ;
7- import java .util .ArrayList ;
8- import java .util .Arrays ;
9- import java .util .List ;
107
118import org .apache .commons .lang3 .StringUtils ;
9+ import org .apache .commons .lang3 .SystemUtils ;
1210import org .apache .maven .plugin .MojoExecutionException ;
1311import org .codehaus .plexus .util .cli .CommandLineException ;
1412import org .codehaus .plexus .util .cli .Commandline ;
1513
1614public class CommandUtils {
1715
18- private static Object [] expandArray (Object [] array ) {
19- List <Object > args = new ArrayList <Object >();
20- for (Object o : array ) {
21- if (o .getClass ().isArray ()) {
22- Object [] a = (Object []) o ;
23- args .addAll (Arrays .asList (a ));
24- } else {
25- args .add (o );
26- }
27- }
28- return args .toArray ();
29- }
30-
3116 private static void createArguments (Commandline command , Object ... arguments ) {
3217 for (Object argument : arguments ) {
3318 if (argument instanceof File )
@@ -42,13 +27,17 @@ else if (argument.getClass().isArray())
4227 public static String execute (File workingDirectory , String executable , Object ... arguments ) throws MojoExecutionException {
4328 StringBuffer outputBuffer = new StringBuffer ();
4429 try {
45-
46- arguments = expandArray (arguments );
47-
30+
4831 Commandline command = new Commandline ();
4932 command .setWorkingDirectory (workingDirectory );
50- command .setExecutable (executable );
51- createArguments (command , arguments );
33+
34+ if (SystemUtils .IS_OS_WINDOWS ) {
35+ command .setExecutable (executable );
36+ createArguments (command , arguments );
37+ } else {
38+ command .setExecutable ("/bin/bash" );
39+ createArguments (command , "-c" , StringUtils .join (arguments , " " ));
40+ }
5241
5342 Logger .info ("Executing command: " + StringUtils .join (command .getCommandline (), " " ));
5443
@@ -71,42 +60,7 @@ public static String execute(File workingDirectory, String executable, Object ..
7160
7261 return outputBuffer .toString ();
7362 }
74-
75- public static String execute2 (File workingDirectory , String executable , Object ... arguments ) throws MojoExecutionException {
76- StringBuffer outputBuffer = new StringBuffer ();
77- try {
78-
79- arguments = expandArray (arguments );
80-
81- Commandline command = new Commandline ();
82- command .setWorkingDirectory (workingDirectory );
83- command .setExecutable (executable );
84- createArguments (command , arguments );
85-
86- String line = StringUtils .join (command .getCommandline (), " " );
87-
88- System .out .println (Arrays .asList (command .getCommandline ()));
89-
90- Process process = Runtime .getRuntime ().exec (line );
91-
92- BufferedReader output = new BufferedReader (new InputStreamReader (process .getInputStream ()));
93- BufferedReader error = new BufferedReader (new InputStreamReader (process .getErrorStream ()));
94-
95- while (process .isAlive ()) {
96- if (output .ready ()) outputBuffer .append (Logger .info (output .readLine ()) + "\n " );
97- if (error .ready ()) Logger .error (error .readLine ());
98- }
99-
100- output .close ();
101- error .close ();
102-
103- } catch (IOException e ) {
104- throw new MojoExecutionException (e .getMessage (), e );
105- }
10663
107- return outputBuffer .toString ();
108- }
109-
11064 public static String execute (String executable , Object ... arguments ) throws MojoExecutionException {
11165 return execute (new File ("." ), executable , arguments );
11266 }
0 commit comments