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  
92          File workingDirectory = getWorkingDirectory();
93          if ( workingDirectory != null )
94          {
95              cliBuilder.setWorkingDirectory( getWorkingDirectory() );
96          }
97  
98          Commandline cli;
99          try
100         {
101             cli = cliBuilder.build( request );
102         }
103         catch ( CommandLineConfigurationException e )
104         {
105             throw new MavenInvocationException( "Error configuring command-line. Reason: " + e.getMessage(), e );
106         }
107 
108         DefaultInvocationResult result = new DefaultInvocationResult();
109 
110         try
111         {
112             int exitCode = executeCommandLine( cli, request );
113 
114             result.setExitCode( exitCode );
115         }
116         catch ( CommandLineException e )
117         {
118             result.setExecutionException( e );
119         }
120 
121         return result;
122     }
123 
124     private int executeCommandLine( Commandline cli, InvocationRequest request )
125         throws CommandLineException
126     {
127         int result = Integer.MIN_VALUE;
128 
129         InputStream inputStream = request.getInputStream( this.inputStream );
130         InvocationOutputHandler outputHandler = request.getOutputHandler( this.outputHandler );
131         InvocationOutputHandler errorHandler = request.getErrorHandler( this.errorHandler );
132 
133         if ( getLogger().isDebugEnabled() )
134         {
135             getLogger().debug( "Executing: " + cli );
136         }
137         if ( request.isInteractive() )
138         {
139             if ( inputStream == null )
140             {
141                 getLogger().warn(
142                                   "Maven will be executed in interactive mode"
143                                       + ", but no input stream has been configured for this MavenInvoker instance." );
144 
145                 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
146             }
147             else
148             {
149                 result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler );
150             }
151         }
152         else
153         {
154             if ( inputStream != null )
155             {
156                 getLogger().info( "Executing in batch mode. The configured input stream will be ignored." );
157             }
158 
159             result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
160         }
161 
162         return result;
163     }
164 
165     public File getLocalRepositoryDirectory()
166     {
167         return localRepositoryDirectory;
168     }
169 
170     public InvokerLogger getLogger()
171     {
172         return logger;
173     }
174 
175     public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
176     {
177         this.localRepositoryDirectory = localRepositoryDirectory;
178         return this;
179     }
180 
181     public Invoker setLogger( InvokerLogger logger )
182     {
183         this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
184         return this;
185     }
186 
187     public File getWorkingDirectory()
188     {
189         return workingDirectory;
190     }
191 
192     public Invoker setWorkingDirectory( File workingDirectory )
193     {
194         this.workingDirectory = workingDirectory;
195         return this;
196     }
197 
198     public File getMavenHome()
199     {
200         return mavenHome;
201     }
202 
203     public Invoker setMavenHome( File mavenHome )
204     {
205         this.mavenHome = mavenHome;
206 
207         return this;
208     }
209 
210     public File getMavenExecutable()
211     {
212         return mavenExecutable;
213     }
214 
215     public Invoker setMavenExecutable( File mavenExecutable )
216     {
217         this.mavenExecutable = mavenExecutable;
218         return this;
219     }
220 
221     public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
222     {
223         this.errorHandler = errorHandler;
224         return this;
225     }
226 
227     public Invoker setInputStream( InputStream inputStream )
228     {
229         this.inputStream = inputStream;
230         return this;
231     }
232 
233     public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
234     {
235         this.outputHandler = outputHandler;
236         return this;
237     }
238 
239 }