View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Class intended to be used by clients who wish to invoke a forked Maven process from their applications
33   *
34   * @author jdcasey
35   */
36  @Named
37  @Singleton
38  public class DefaultInvoker implements Invoker {
39      /** Constant <code>ROLE_HINT="default"</code> */
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      /** {@inheritDoc} */
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      * <p>Getter for the field <code>localRepositoryDirectory</code>.</p>
143      *
144      * @return a {@link java.io.File} object.
145      */
146     public File getLocalRepositoryDirectory() {
147         return localRepositoryDirectory;
148     }
149 
150     /**
151      * <p>Getter for the field <code>logger</code>.</p>
152      *
153      * @return a {@link org.apache.maven.shared.invoker.InvokerLogger} object.
154      */
155     public InvokerLogger getLogger() {
156         return logger;
157     }
158 
159     /** {@inheritDoc} */
160     public Invoker setLocalRepositoryDirectory(File localRepositoryDirectory) {
161         this.localRepositoryDirectory = localRepositoryDirectory;
162         return this;
163     }
164 
165     /** {@inheritDoc} */
166     public Invoker setLogger(InvokerLogger logger) {
167         this.logger = (logger != null) ? logger : DEFAULT_LOGGER;
168         return this;
169     }
170 
171     /**
172      * <p>Getter for the field <code>workingDirectory</code>.</p>
173      *
174      * @return a {@link java.io.File} object.
175      */
176     public File getWorkingDirectory() {
177         return workingDirectory;
178     }
179 
180     /** {@inheritDoc} */
181     public Invoker setWorkingDirectory(File workingDirectory) {
182         this.workingDirectory = workingDirectory;
183         return this;
184     }
185 
186     /**
187      * <p>Getter for the field <code>mavenHome</code>.</p>
188      *
189      * @return a {@link java.io.File} object.
190      */
191     public File getMavenHome() {
192         return mavenHome;
193     }
194 
195     /** {@inheritDoc} */
196     public Invoker setMavenHome(File mavenHome) {
197         this.mavenHome = mavenHome;
198 
199         return this;
200     }
201 
202     /**
203      * <p>Getter for the field <code>mavenExecutable</code>.</p>
204      *
205      * @return a {@link java.io.File} object.
206      */
207     public File getMavenExecutable() {
208         return mavenExecutable;
209     }
210 
211     /** {@inheritDoc} */
212     public Invoker setMavenExecutable(File mavenExecutable) {
213         this.mavenExecutable = mavenExecutable;
214         return this;
215     }
216 
217     /** {@inheritDoc} */
218     public Invoker setErrorHandler(InvocationOutputHandler errorHandler) {
219         this.errorHandler = errorHandler;
220         return this;
221     }
222 
223     /** {@inheritDoc} */
224     public Invoker setInputStream(InputStream inputStream) {
225         this.inputStream = inputStream;
226         return this;
227     }
228 
229     /** {@inheritDoc} */
230     public Invoker setOutputHandler(InvocationOutputHandler outputHandler) {
231         this.outputHandler = outputHandler;
232         return this;
233     }
234 }