1 package org.apache.maven.shared.invoker;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.InputStream;
24
25 import org.apache.maven.shared.utils.cli.CommandLineException;
26 import org.apache.maven.shared.utils.cli.CommandLineUtils;
27 import org.apache.maven.shared.utils.cli.Commandline;
28
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31
32
33
34
35
36
37 @Named
38 @Singleton
39 public class DefaultInvoker
40 implements Invoker
41 {
42
43 public static final String ROLE_HINT = "default";
44
45 private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();
46
47 private static final InvocationOutputHandler DEFAULT_OUTPUT_HANDLER = new SystemOutHandler();
48
49 private File localRepositoryDirectory;
50
51 private InvokerLogger logger = DEFAULT_LOGGER;
52
53 private File workingDirectory;
54
55 private File mavenHome;
56
57 private File mavenExecutable;
58
59 private InvocationOutputHandler outputHandler = DEFAULT_OUTPUT_HANDLER;
60
61 private InputStream inputStream;
62
63 private InvocationOutputHandler errorHandler = DEFAULT_OUTPUT_HANDLER;
64
65
66 public InvocationResult execute( InvocationRequest request )
67 throws MavenInvocationException
68 {
69 MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder();
70
71 if ( logger != null )
72 {
73 cliBuilder.setLogger( logger );
74 }
75
76 if ( localRepositoryDirectory != null )
77 {
78 cliBuilder.setLocalRepositoryDirectory( localRepositoryDirectory );
79 }
80
81 if ( mavenHome != null )
82 {
83 cliBuilder.setMavenHome( mavenHome );
84 }
85
86 if ( mavenExecutable != null )
87 {
88 cliBuilder.setMavenExecutable( mavenExecutable );
89 }
90
91 if ( workingDirectory != null )
92 {
93 cliBuilder.setBaseDirectory( workingDirectory );
94 }
95
96 Commandline cli;
97
98 try
99 {
100 cli = cliBuilder.build( request );
101 }
102 catch ( CommandLineConfigurationException e )
103 {
104 throw new MavenInvocationException( "Error configuring command-line. Reason: " + e.getMessage(), e );
105 }
106
107 DefaultInvocationResult result = new DefaultInvocationResult();
108
109 try
110 {
111 int exitCode = executeCommandLine( cli, request, request.getTimeoutInSeconds() );
112
113 result.setExitCode( exitCode );
114 }
115 catch ( CommandLineException e )
116 {
117 result.setExecutionException( e );
118 }
119
120 return result;
121 }
122
123 private int executeCommandLine( Commandline cli, InvocationRequest request, int timeoutInSeconds )
124 throws CommandLineException
125 {
126 int result;
127
128 InputStream inputStream = request.getInputStream( this.inputStream );
129 InvocationOutputHandler outputHandler = request.getOutputHandler( this.outputHandler );
130 InvocationOutputHandler errorHandler = request.getErrorHandler( this.errorHandler );
131
132 if ( getLogger().isDebugEnabled() )
133 {
134 getLogger().debug( "Executing: " + cli );
135 }
136
137 if ( request.isBatchMode() )
138 {
139 if ( inputStream != null )
140 {
141 getLogger().info( "Executing in batch mode. The configured input stream will be ignored." );
142 }
143
144 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler, timeoutInSeconds );
145 }
146 else
147 {
148 if ( inputStream == null )
149 {
150 getLogger().warn( "Maven will be executed in interactive mode"
151 + ", but no input stream has been configured for this MavenInvoker instance." );
152
153 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler, timeoutInSeconds );
154 }
155 else
156 {
157 result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler,
158 timeoutInSeconds );
159 }
160 }
161
162 return result;
163 }
164
165
166
167
168
169
170 public File getLocalRepositoryDirectory()
171 {
172 return localRepositoryDirectory;
173 }
174
175
176
177
178
179
180 public InvokerLogger getLogger()
181 {
182 return logger;
183 }
184
185
186 public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
187 {
188 this.localRepositoryDirectory = localRepositoryDirectory;
189 return this;
190 }
191
192
193 public Invoker setLogger( InvokerLogger logger )
194 {
195 this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
196 return this;
197 }
198
199
200
201
202
203
204 public File getWorkingDirectory()
205 {
206 return workingDirectory;
207 }
208
209
210 public Invoker setWorkingDirectory( File workingDirectory )
211 {
212 this.workingDirectory = workingDirectory;
213 return this;
214 }
215
216
217
218
219
220
221 public File getMavenHome()
222 {
223 return mavenHome;
224 }
225
226
227 public Invoker setMavenHome( File mavenHome )
228 {
229 this.mavenHome = mavenHome;
230
231 return this;
232 }
233
234
235
236
237
238
239 public File getMavenExecutable()
240 {
241 return mavenExecutable;
242 }
243
244
245 public Invoker setMavenExecutable( File mavenExecutable )
246 {
247 this.mavenExecutable = mavenExecutable;
248 return this;
249 }
250
251
252 public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
253 {
254 this.errorHandler = errorHandler;
255 return this;
256 }
257
258
259 public Invoker setInputStream( InputStream inputStream )
260 {
261 this.inputStream = inputStream;
262 return this;
263 }
264
265
266 public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
267 {
268 this.outputHandler = outputHandler;
269 return this;
270 }
271
272 }