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          if ( logger != null )
72          {
73              cliBuilder.setLogger( logger );
74          }
75  
76          if ( localRepositoryDirectory != null )
77          {
78              cliBuilder.setLocalRepositoryDirectory( localRepositoryDirectory );
79          }
80  
81          if ( mavenHome != null )
82          {
83              cliBuilder.setMavenHome( mavenHome );
84          }
85  
86          if ( mavenExecutable != null )
87          {
88              cliBuilder.setMavenExecutable( mavenExecutable );
89          }
90  
91          if ( workingDirectory != null )
92          {
93              cliBuilder.setBaseDirectory( workingDirectory );
94          }
95  
96          Commandline cli;
97  
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;
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     /**
166      * <p>Getter for the field <code>localRepositoryDirectory</code>.</p>
167      *
168      * @return a {@link java.io.File} object.
169      */
170     public File getLocalRepositoryDirectory()
171     {
172         return localRepositoryDirectory;
173     }
174 
175     /**
176      * <p>Getter for the field <code>logger</code>.</p>
177      *
178      * @return a {@link org.apache.maven.shared.invoker.InvokerLogger} object.
179      */
180     public InvokerLogger getLogger()
181     {
182         return logger;
183     }
184 
185     /** {@inheritDoc} */
186     public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
187     {
188         this.localRepositoryDirectory = localRepositoryDirectory;
189         return this;
190     }
191 
192     /** {@inheritDoc} */
193     public Invoker setLogger( InvokerLogger logger )
194     {
195         this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
196         return this;
197     }
198 
199     /**
200      * <p>Getter for the field <code>workingDirectory</code>.</p>
201      *
202      * @return a {@link java.io.File} object.
203      */
204     public File getWorkingDirectory()
205     {
206         return workingDirectory;
207     }
208 
209     /** {@inheritDoc} */
210     public Invoker setWorkingDirectory( File workingDirectory )
211     {
212         this.workingDirectory = workingDirectory;
213         return this;
214     }
215 
216     /**
217      * <p>Getter for the field <code>mavenHome</code>.</p>
218      *
219      * @return a {@link java.io.File} object.
220      */
221     public File getMavenHome()
222     {
223         return mavenHome;
224     }
225 
226     /** {@inheritDoc} */
227     public Invoker setMavenHome( File mavenHome )
228     {
229         this.mavenHome = mavenHome;
230 
231         return this;
232     }
233 
234     /**
235      * <p>Getter for the field <code>mavenExecutable</code>.</p>
236      *
237      * @return a {@link java.io.File} object.
238      */
239     public File getMavenExecutable()
240     {
241         return mavenExecutable;
242     }
243 
244     /** {@inheritDoc} */
245     public Invoker setMavenExecutable( File mavenExecutable )
246     {
247         this.mavenExecutable = mavenExecutable;
248         return this;
249     }
250 
251     /** {@inheritDoc} */
252     public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
253     {
254         this.errorHandler = errorHandler;
255         return this;
256     }
257 
258     /** {@inheritDoc} */
259     public Invoker setInputStream( InputStream inputStream )
260     {
261         this.inputStream = inputStream;
262         return this;
263     }
264 
265     /** {@inheritDoc} */
266     public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
267     {
268         this.outputHandler = outputHandler;
269         return this;
270     }
271 
272 }