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