View Javadoc
1   package org.apache.maven.shared.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Class intended to be used by clients who wish to invoke a forked Maven process from their applications
34   *
35   * @author jdcasey
36   */
37  @Named
38  @Singleton
39  public class DefaultInvoker
40      implements Invoker
41  {
42      /** Constant <code>ROLE_HINT="default"</code> */
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      /** {@inheritDoc} */
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      * <p>Getter for the field <code>localRepositoryDirectory</code>.</p>
171      *
172      * @return a {@link java.io.File} object.
173      */
174     public File getLocalRepositoryDirectory()
175     {
176         return localRepositoryDirectory;
177     }
178 
179     /**
180      * <p>Getter for the field <code>logger</code>.</p>
181      *
182      * @return a {@link org.apache.maven.shared.invoker.InvokerLogger} object.
183      */
184     public InvokerLogger getLogger()
185     {
186         return logger;
187     }
188 
189     /** {@inheritDoc} */
190     public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
191     {
192         this.localRepositoryDirectory = localRepositoryDirectory;
193         return this;
194     }
195 
196     /** {@inheritDoc} */
197     public Invoker setLogger( InvokerLogger logger )
198     {
199         this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
200         return this;
201     }
202 
203     /**
204      * <p>Getter for the field <code>workingDirectory</code>.</p>
205      *
206      * @return a {@link java.io.File} object.
207      */
208     public File getWorkingDirectory()
209     {
210         return workingDirectory;
211     }
212 
213     /** {@inheritDoc} */
214     public Invoker setWorkingDirectory( File workingDirectory )
215     {
216         this.workingDirectory = workingDirectory;
217         return this;
218     }
219 
220     /**
221      * <p>Getter for the field <code>mavenHome</code>.</p>
222      *
223      * @return a {@link java.io.File} object.
224      */
225     public File getMavenHome()
226     {
227         return mavenHome;
228     }
229 
230     /** {@inheritDoc} */
231     public Invoker setMavenHome( File mavenHome )
232     {
233         this.mavenHome = mavenHome;
234 
235         return this;
236     }
237 
238     /**
239      * <p>Getter for the field <code>mavenExecutable</code>.</p>
240      *
241      * @return a {@link java.io.File} object.
242      */
243     public File getMavenExecutable()
244     {
245         return mavenExecutable;
246     }
247 
248     /** {@inheritDoc} */
249     public Invoker setMavenExecutable( File mavenExecutable )
250     {
251         this.mavenExecutable = mavenExecutable;
252         return this;
253     }
254 
255     /** {@inheritDoc} */
256     public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
257     {
258         this.errorHandler = errorHandler;
259         return this;
260     }
261 
262     /** {@inheritDoc} */
263     public Invoker setInputStream( InputStream inputStream )
264     {
265         this.inputStream = inputStream;
266         return this;
267     }
268 
269     /** {@inheritDoc} */
270     public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
271     {
272         this.outputHandler = outputHandler;
273         return this;
274     }
275 
276 }