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 import org.codehaus.plexus.component.annotations.Component;
29
30
31
32
33
34
35 @Component( role = Invoker.class, hint = "default" )
36 public class DefaultInvoker
37 implements Invoker
38 {
39
40 public static final String ROLE_HINT = "default";
41
42 private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();
43
44 private static final InvocationOutputHandler DEFAULT_OUTPUT_HANDLER = new SystemOutHandler();
45
46 private File localRepositoryDirectory;
47
48 private InvokerLogger logger = DEFAULT_LOGGER;
49
50 private File workingDirectory;
51
52 private File mavenHome;
53
54 private File mavenExecutable;
55
56 private InvocationOutputHandler outputHandler = DEFAULT_OUTPUT_HANDLER;
57
58 private InputStream inputStream;
59
60 private InvocationOutputHandler errorHandler = DEFAULT_OUTPUT_HANDLER;
61
62 public InvocationResult execute( InvocationRequest request )
63 throws MavenInvocationException
64 {
65 MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder();
66
67 InvokerLogger logger = getLogger();
68 if ( logger != null )
69 {
70 cliBuilder.setLogger( getLogger() );
71 }
72
73 File localRepo = getLocalRepositoryDirectory();
74 if ( localRepo != null )
75 {
76 cliBuilder.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
77 }
78
79 File mavenHome = getMavenHome();
80 if ( mavenHome != null )
81 {
82 cliBuilder.setMavenHome( getMavenHome() );
83 }
84
85 File mavenExecutable = getMavenExecutable();
86 if ( mavenExecutable != null )
87 {
88 cliBuilder.setMavenExecutable( mavenExecutable );
89 }
90
91 File workingDirectory = getWorkingDirectory();
92 if ( workingDirectory != null )
93 {
94 cliBuilder.setWorkingDirectory( getWorkingDirectory() );
95 }
96
97 Commandline cli;
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 = Integer.MIN_VALUE;
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 public File getLocalRepositoryDirectory()
166 {
167 return localRepositoryDirectory;
168 }
169
170 public InvokerLogger getLogger()
171 {
172 return logger;
173 }
174
175 public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
176 {
177 this.localRepositoryDirectory = localRepositoryDirectory;
178 return this;
179 }
180
181 public Invoker setLogger( InvokerLogger logger )
182 {
183 this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
184 return this;
185 }
186
187 public File getWorkingDirectory()
188 {
189 return workingDirectory;
190 }
191
192 public Invoker setWorkingDirectory( File workingDirectory )
193 {
194 this.workingDirectory = workingDirectory;
195 return this;
196 }
197
198 public File getMavenHome()
199 {
200 return mavenHome;
201 }
202
203 public Invoker setMavenHome( File mavenHome )
204 {
205 this.mavenHome = mavenHome;
206
207 return this;
208 }
209
210 public File getMavenExecutable()
211 {
212 return mavenExecutable;
213 }
214
215 public Invoker setMavenExecutable( File mavenExecutable )
216 {
217 this.mavenExecutable = mavenExecutable;
218 return this;
219 }
220
221 public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
222 {
223 this.errorHandler = errorHandler;
224 return this;
225 }
226
227 public Invoker setInputStream( InputStream inputStream )
228 {
229 this.inputStream = inputStream;
230 return this;
231 }
232
233 public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
234 {
235 this.outputHandler = outputHandler;
236 return this;
237 }
238
239 }