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.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.util.cli.CommandLineException;
27  import org.codehaus.plexus.util.cli.CommandLineUtils;
28  import org.codehaus.plexus.util.cli.Commandline;
29  
30  /**
31   * Class intended to be used by clients who wish to invoke a forked Maven process from their applications
32   * 
33   * @author jdcasey
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 );
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 )
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 );
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 );
154             }
155             else
156             {
157                 result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler );
158             }
159         }
160 
161         return result;
162     }
163 
164     public File getLocalRepositoryDirectory()
165     {
166         return localRepositoryDirectory;
167     }
168 
169     public InvokerLogger getLogger()
170     {
171         return logger;
172     }
173 
174     public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
175     {
176         this.localRepositoryDirectory = localRepositoryDirectory;
177         return this;
178     }
179 
180     public Invoker setLogger( InvokerLogger logger )
181     {
182         this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
183         return this;
184     }
185 
186     public File getWorkingDirectory()
187     {
188         return workingDirectory;
189     }
190 
191     public Invoker setWorkingDirectory( File workingDirectory )
192     {
193         this.workingDirectory = workingDirectory;
194         return this;
195     }
196 
197     public File getMavenHome()
198     {
199         return mavenHome;
200     }
201 
202     public Invoker setMavenHome( File mavenHome )
203     {
204         this.mavenHome = mavenHome;
205 
206         return this;
207     }
208 
209     public File getMavenExecutable()
210     {
211         return mavenExecutable;
212     }
213 
214     public Invoker setMavenExecutable( File mavenExecutable )
215     {
216         this.mavenExecutable = mavenExecutable;
217         return this;
218     }
219 
220     public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
221     {
222         this.errorHandler = errorHandler;
223         return this;
224     }
225 
226     public Invoker setInputStream( InputStream inputStream )
227     {
228         this.inputStream = inputStream;
229         return this;
230     }
231 
232     public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
233     {
234         this.outputHandler = outputHandler;
235         return this;
236     }
237 
238 }