1 package org.apache.continuum.utils.shell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.shared.release.ReleaseResult;
23 import org.apache.maven.shared.release.exec.MavenExecutorException;
24 import org.apache.maven.shared.release.exec.TeeConsumer;
25 import org.codehaus.plexus.util.StringUtils;
26 import org.codehaus.plexus.util.cli.CommandLineException;
27 import org.codehaus.plexus.util.cli.CommandLineUtils;
28 import org.codehaus.plexus.util.cli.Commandline;
29 import org.codehaus.plexus.util.cli.StreamConsumer;
30 import org.codehaus.plexus.util.cli.WriterStreamConsumer;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.stereotype.Service;
34
35 import java.io.File;
36 import java.io.FileWriter;
37 import java.io.Writer;
38 import java.util.Arrays;
39 import java.util.Map;
40
41
42
43
44
45 @Service( "shellCommandHelper" )
46 public class DefaultShellCommandHelper
47 implements ShellCommandHelper
48 {
49 private static final Logger log = LoggerFactory.getLogger( DefaultShellCommandHelper.class );
50
51
52
53
54
55 public ExecutionResult executeShellCommand( File workingDirectory, String executable, String arguments, File output,
56 long idCommand, Map<String, String> environments )
57 throws Exception
58 {
59 Commandline cl = new Commandline();
60
61 Commandline.Argument argument = cl.createArgument();
62
63 argument.setLine( arguments );
64
65 return executeShellCommand( workingDirectory, executable, argument.getParts(), output, idCommand,
66 environments );
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 protected Commandline createCommandline( File workingDirectory, String executable, String[] arguments,
81 long idCommand, Map<String, String> environments )
82 throws Exception
83 {
84 Commandline cl = new Commandline();
85
86 cl.setPid( idCommand );
87
88 cl.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
89
90 if ( environments != null && !environments.isEmpty() )
91 {
92 for ( String key : environments.keySet() )
93 {
94 String value = environments.get( key );
95 cl.addEnvironment( key, value );
96 }
97 }
98
99 cl.addSystemEnvironment();
100
101 cl.setExecutable( executable );
102
103 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
104
105 if ( arguments != null )
106 {
107 for ( String argument : arguments )
108 {
109 cl.createArgument().setValue( argument );
110 }
111 }
112
113 return cl;
114 }
115
116 public ExecutionResult executeShellCommand( File workingDirectory, String executable, String[] arguments,
117 File output, long idCommand, Map<String, String> environments )
118 throws Exception
119 {
120
121 Commandline cl = createCommandline( workingDirectory, executable, arguments, idCommand, environments );
122
123 log.info( "Executing: " + cl );
124 log.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
125 log.debug( "EnvironmentVariables " + Arrays.asList( cl.getEnvironmentVariables() ) );
126
127
128
129
130
131
132
133 Writer writer = new FileWriter( output );
134
135 StreamConsumer consumer = new WriterStreamConsumer( writer );
136
137 int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
138
139 writer.flush();
140
141 writer.close();
142
143
144
145
146
147 return new ExecutionResult( exitCode );
148 }
149
150 public boolean isRunning( long idCommand )
151 {
152 return CommandLineUtils.isAlive( idCommand );
153 }
154
155 public void killProcess( long idCommand )
156 {
157 CommandLineUtils.killProcess( idCommand );
158 }
159
160 public void executeGoals( File workingDirectory, String executable, String goals, boolean interactive,
161 String arguments, ReleaseResult relResult, Map<String, String> environments )
162 throws Exception
163 {
164 Commandline cl = new Commandline();
165
166 Commandline.Argument argument = cl.createArgument();
167
168 argument.setLine( arguments );
169
170 executeGoals( workingDirectory, executable, goals, interactive, argument.getParts(), relResult, environments );
171 }
172
173 public void executeGoals( File workingDirectory, String executable, String goals, boolean interactive,
174 String[] arguments, ReleaseResult relResult, Map<String, String> environments )
175 throws Exception
176 {
177 if ( executable == null )
178 {
179 executable = "mvn";
180 }
181
182 Commandline cl = createCommandline( workingDirectory, executable, arguments, -1, environments );
183
184 if ( goals != null )
185 {
186
187 String[] tokens = StringUtils.split( goals, ", " );
188
189 for ( String token : tokens )
190 {
191 cl.createArgument().setValue( token );
192 }
193 }
194
195 cl.createArgument().setValue( "--no-plugin-updates" );
196
197 if ( !interactive )
198 {
199 cl.createArgument().setValue( "--batch-mode" );
200 }
201
202 StreamConsumer stdOut = new TeeConsumer( System.out );
203
204 StreamConsumer stdErr = new TeeConsumer( System.err );
205
206 try
207 {
208 relResult.appendInfo( "Executing: " + cl.toString() );
209 log.info( "Executing: " + cl.toString() );
210
211 int result = CommandLineUtils.executeCommandLine( cl, stdOut, stdErr );
212
213 if ( result != 0 )
214 {
215 throw new MavenExecutorException( "Maven execution failed, exit code: \'" + result + "\'", result,
216 stdOut.toString(), stdErr.toString() );
217 }
218 }
219 catch ( CommandLineException e )
220 {
221 throw new MavenExecutorException( "Can't run goal " + goals, stdOut.toString(), stdErr.toString(), e );
222 }
223 finally
224 {
225 relResult.appendOutput( stdOut.toString() );
226 }
227 }
228 }