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.codehaus.plexus.component.annotations.Component; |
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 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
@Component( role = Invoker.class, hint = "default" ) |
36 | 6 | public class DefaultInvoker |
37 | |
implements Invoker |
38 | |
{ |
39 | |
|
40 | |
public static final String ROLE_HINT = "default"; |
41 | |
|
42 | 1 | private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger(); |
43 | |
|
44 | 1 | private static final InvocationOutputHandler DEFAULT_OUTPUT_HANDLER = new SystemOutHandler(); |
45 | |
|
46 | |
private File localRepositoryDirectory; |
47 | |
|
48 | 6 | private InvokerLogger logger = DEFAULT_LOGGER; |
49 | |
|
50 | |
private File workingDirectory; |
51 | |
|
52 | |
private File mavenHome; |
53 | |
|
54 | |
private File mavenExecutable; |
55 | |
|
56 | 6 | private InvocationOutputHandler outputHandler = DEFAULT_OUTPUT_HANDLER; |
57 | |
|
58 | |
private InputStream inputStream; |
59 | |
|
60 | 6 | private InvocationOutputHandler errorHandler = DEFAULT_OUTPUT_HANDLER; |
61 | |
|
62 | |
public InvocationResult execute( InvocationRequest request ) |
63 | |
throws MavenInvocationException |
64 | |
{ |
65 | 6 | MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder(); |
66 | |
|
67 | 6 | InvokerLogger logger = getLogger(); |
68 | 6 | if ( logger != null ) |
69 | |
{ |
70 | 6 | cliBuilder.setLogger( getLogger() ); |
71 | |
} |
72 | |
|
73 | 6 | File localRepo = getLocalRepositoryDirectory(); |
74 | 6 | if ( localRepo != null ) |
75 | |
{ |
76 | 0 | cliBuilder.setLocalRepositoryDirectory( getLocalRepositoryDirectory() ); |
77 | |
} |
78 | |
|
79 | 6 | File mavenHome = getMavenHome(); |
80 | 6 | if ( mavenHome != null ) |
81 | |
{ |
82 | 6 | cliBuilder.setMavenHome( getMavenHome() ); |
83 | |
} |
84 | |
|
85 | 6 | File mavenExecutable = getMavenExecutable(); |
86 | 6 | if ( mavenExecutable != null ) |
87 | |
{ |
88 | 0 | cliBuilder.setMavenExecutable( mavenExecutable ); |
89 | |
} |
90 | |
|
91 | |
|
92 | 6 | File workingDirectory = getWorkingDirectory(); |
93 | 6 | if ( workingDirectory != null ) |
94 | |
{ |
95 | 0 | cliBuilder.setWorkingDirectory( getWorkingDirectory() ); |
96 | |
} |
97 | |
|
98 | |
Commandline cli; |
99 | |
try |
100 | |
{ |
101 | 6 | cli = cliBuilder.build( request ); |
102 | |
} |
103 | 0 | catch ( CommandLineConfigurationException e ) |
104 | |
{ |
105 | 0 | throw new MavenInvocationException( "Error configuring command-line. Reason: " + e.getMessage(), e ); |
106 | 6 | } |
107 | |
|
108 | 6 | DefaultInvocationResult result = new DefaultInvocationResult(); |
109 | |
|
110 | |
try |
111 | |
{ |
112 | 6 | int exitCode = executeCommandLine( cli, request ); |
113 | |
|
114 | 6 | result.setExitCode( exitCode ); |
115 | |
} |
116 | 0 | catch ( CommandLineException e ) |
117 | |
{ |
118 | 0 | result.setExecutionException( e ); |
119 | 6 | } |
120 | |
|
121 | 6 | return result; |
122 | |
} |
123 | |
|
124 | |
private int executeCommandLine( Commandline cli, InvocationRequest request ) |
125 | |
throws CommandLineException |
126 | |
{ |
127 | 6 | int result = Integer.MIN_VALUE; |
128 | |
|
129 | 6 | InputStream inputStream = request.getInputStream( this.inputStream ); |
130 | 6 | InvocationOutputHandler outputHandler = request.getOutputHandler( this.outputHandler ); |
131 | 6 | InvocationOutputHandler errorHandler = request.getErrorHandler( this.errorHandler ); |
132 | |
|
133 | 6 | if ( getLogger().isDebugEnabled() ) |
134 | |
{ |
135 | 6 | getLogger().debug( "Executing: " + cli ); |
136 | |
} |
137 | 6 | if ( request.isInteractive() ) |
138 | |
{ |
139 | 0 | if ( inputStream == null ) |
140 | |
{ |
141 | 0 | getLogger().warn( |
142 | |
"Maven will be executed in interactive mode" |
143 | |
+ ", but no input stream has been configured for this MavenInvoker instance." ); |
144 | |
|
145 | 0 | result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler ); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler ); |
150 | |
} |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 6 | if ( inputStream != null ) |
155 | |
{ |
156 | 0 | getLogger().info( "Executing in batch mode. The configured input stream will be ignored." ); |
157 | |
} |
158 | |
|
159 | 6 | result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler ); |
160 | |
} |
161 | |
|
162 | 6 | return result; |
163 | |
} |
164 | |
|
165 | |
public File getLocalRepositoryDirectory() |
166 | |
{ |
167 | 6 | return localRepositoryDirectory; |
168 | |
} |
169 | |
|
170 | |
public InvokerLogger getLogger() |
171 | |
{ |
172 | 24 | return logger; |
173 | |
} |
174 | |
|
175 | |
public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory ) |
176 | |
{ |
177 | 6 | this.localRepositoryDirectory = localRepositoryDirectory; |
178 | 6 | return this; |
179 | |
} |
180 | |
|
181 | |
public Invoker setLogger( InvokerLogger logger ) |
182 | |
{ |
183 | 6 | this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER; |
184 | 6 | return this; |
185 | |
} |
186 | |
|
187 | |
public File getWorkingDirectory() |
188 | |
{ |
189 | 6 | return workingDirectory; |
190 | |
} |
191 | |
|
192 | |
public Invoker setWorkingDirectory( File workingDirectory ) |
193 | |
{ |
194 | 0 | this.workingDirectory = workingDirectory; |
195 | 0 | return this; |
196 | |
} |
197 | |
|
198 | |
public File getMavenHome() |
199 | |
{ |
200 | 12 | return mavenHome; |
201 | |
} |
202 | |
|
203 | |
public Invoker setMavenHome( File mavenHome ) |
204 | |
{ |
205 | 6 | this.mavenHome = mavenHome; |
206 | |
|
207 | 6 | return this; |
208 | |
} |
209 | |
|
210 | |
public File getMavenExecutable() |
211 | |
{ |
212 | 6 | return mavenExecutable; |
213 | |
} |
214 | |
|
215 | |
public Invoker setMavenExecutable( File mavenExecutable ) |
216 | |
{ |
217 | 0 | this.mavenExecutable = mavenExecutable; |
218 | 0 | return this; |
219 | |
} |
220 | |
|
221 | |
public Invoker setErrorHandler( InvocationOutputHandler errorHandler ) |
222 | |
{ |
223 | 0 | this.errorHandler = errorHandler; |
224 | 0 | return this; |
225 | |
} |
226 | |
|
227 | |
public Invoker setInputStream( InputStream inputStream ) |
228 | |
{ |
229 | 0 | this.inputStream = inputStream; |
230 | 0 | return this; |
231 | |
} |
232 | |
|
233 | |
public Invoker setOutputHandler( InvocationOutputHandler outputHandler ) |
234 | |
{ |
235 | 0 | this.outputHandler = outputHandler; |
236 | 0 | return this; |
237 | |
} |
238 | |
|
239 | |
} |