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 InvokerLogger logger = getLogger();
72 if ( logger != null )
73 {
74 cliBuilder.setLogger( getLogger() );
75 }
76
77 File localRepo = getLocalRepositoryDirectory();
78 if ( localRepo != null )
79 {
80 cliBuilder.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
81 }
82
83 File mavenHome = getMavenHome();
84 if ( mavenHome != null )
85 {
86 cliBuilder.setMavenHome( getMavenHome() );
87 }
88
89 File mavenExecutable = getMavenExecutable();
90 if ( mavenExecutable != null )
91 {
92 cliBuilder.setMavenExecutable( mavenExecutable );
93 }
94
95 File workingDirectory = getWorkingDirectory();
96 if ( workingDirectory != null )
97 {
98 cliBuilder.setWorkingDirectory( getWorkingDirectory() );
99 }
100
101 Commandline cli;
102 try
103 {
104 cli = cliBuilder.build( request );
105 }
106 catch ( CommandLineConfigurationException e )
107 {
108 throw new MavenInvocationException( "Error configuring command-line. Reason: " + e.getMessage(), e );
109 }
110
111 DefaultInvocationResult result = new DefaultInvocationResult();
112
113 try
114 {
115 int exitCode = executeCommandLine( cli, request, request.getTimeoutInSeconds() );
116
117 result.setExitCode( exitCode );
118 }
119 catch ( CommandLineException e )
120 {
121 result.setExecutionException( e );
122 }
123
124 return result;
125 }
126
127 private int executeCommandLine( Commandline cli, InvocationRequest request, int timeoutInSeconds )
128 throws CommandLineException
129 {
130 int result;
131
132 InputStream inputStream = request.getInputStream( this.inputStream );
133 InvocationOutputHandler outputHandler = request.getOutputHandler( this.outputHandler );
134 InvocationOutputHandler errorHandler = request.getErrorHandler( this.errorHandler );
135
136 if ( getLogger().isDebugEnabled() )
137 {
138 getLogger().debug( "Executing: " + cli );
139 }
140
141 if ( request.isBatchMode() )
142 {
143 if ( inputStream != null )
144 {
145 getLogger().info( "Executing in batch mode. The configured input stream will be ignored." );
146 }
147
148 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler, timeoutInSeconds );
149 }
150 else
151 {
152 if ( inputStream == null )
153 {
154 getLogger().warn( "Maven will be executed in interactive mode"
155 + ", but no input stream has been configured for this MavenInvoker instance." );
156
157 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler, timeoutInSeconds );
158 }
159 else
160 {
161 result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler,
162 timeoutInSeconds );
163 }
164 }
165
166 return result;
167 }
168
169
170
171
172
173
174 public File getLocalRepositoryDirectory()
175 {
176 return localRepositoryDirectory;
177 }
178
179
180
181
182
183
184 public InvokerLogger getLogger()
185 {
186 return logger;
187 }
188
189
190 public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
191 {
192 this.localRepositoryDirectory = localRepositoryDirectory;
193 return this;
194 }
195
196
197 public Invoker setLogger( InvokerLogger logger )
198 {
199 this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
200 return this;
201 }
202
203
204
205
206
207
208 public File getWorkingDirectory()
209 {
210 return workingDirectory;
211 }
212
213
214 public Invoker setWorkingDirectory( File workingDirectory )
215 {
216 this.workingDirectory = workingDirectory;
217 return this;
218 }
219
220
221
222
223
224
225 public File getMavenHome()
226 {
227 return mavenHome;
228 }
229
230
231 public Invoker setMavenHome( File mavenHome )
232 {
233 this.mavenHome = mavenHome;
234
235 return this;
236 }
237
238
239
240
241
242
243 public File getMavenExecutable()
244 {
245 return mavenExecutable;
246 }
247
248
249 public Invoker setMavenExecutable( File mavenExecutable )
250 {
251 this.mavenExecutable = mavenExecutable;
252 return this;
253 }
254
255
256 public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
257 {
258 this.errorHandler = errorHandler;
259 return this;
260 }
261
262
263 public Invoker setInputStream( InputStream inputStream )
264 {
265 this.inputStream = inputStream;
266 return this;
267 }
268
269
270 public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
271 {
272 this.outputHandler = outputHandler;
273 return this;
274 }
275
276 }