Coverage Report - org.apache.maven.shared.invoker.DefaultInvoker
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultInvoker
68%
47/69
45%
9/20
1,867
 
 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  6
 public class DefaultInvoker
 37  
     implements Invoker
 38  
 {
 39  
 
 40  
     public static final String ROLE_HINT = "default";
 41  
 
 42  1
     private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();
 43  
 
 44  1
     private static final InvocationOutputHandler DEFAULT_OUTPUT_HANDLER = new SystemOutHandler();
 45  
 
 46  
     private File localRepositoryDirectory;
 47  
 
 48  6
     private InvokerLogger logger = DEFAULT_LOGGER;
 49  
 
 50  
     private File workingDirectory;
 51  
 
 52  
     private File mavenHome;
 53  
 
 54  
     private File mavenExecutable;
 55  
 
 56  6
     private InvocationOutputHandler outputHandler = DEFAULT_OUTPUT_HANDLER;
 57  
 
 58  
     private InputStream inputStream;
 59  
 
 60  6
     private InvocationOutputHandler errorHandler = DEFAULT_OUTPUT_HANDLER;
 61  
 
 62  
     public InvocationResult execute( InvocationRequest request )
 63  
         throws MavenInvocationException
 64  
     {
 65  6
         MavenCommandLineBuilder cliBuilder = new MavenCommandLineBuilder();
 66  
 
 67  6
         InvokerLogger logger = getLogger();
 68  6
         if ( logger != null )
 69  
         {
 70  6
             cliBuilder.setLogger( getLogger() );
 71  
         }
 72  
 
 73  6
         File localRepo = getLocalRepositoryDirectory();
 74  6
         if ( localRepo != null )
 75  
         {
 76  0
             cliBuilder.setLocalRepositoryDirectory( getLocalRepositoryDirectory() );
 77  
         }
 78  
 
 79  6
         File mavenHome = getMavenHome();
 80  6
         if ( mavenHome != null )
 81  
         {
 82  6
             cliBuilder.setMavenHome( getMavenHome() );
 83  
         }
 84  
         
 85  6
         File mavenExecutable = getMavenExecutable();
 86  6
         if ( mavenExecutable != null )
 87  
         {
 88  0
             cliBuilder.setMavenExecutable( mavenExecutable );
 89  
         }
 90  
         
 91  
 
 92  6
         File workingDirectory = getWorkingDirectory();
 93  6
         if ( workingDirectory != null )
 94  
         {
 95  0
             cliBuilder.setWorkingDirectory( getWorkingDirectory() );
 96  
         }
 97  
 
 98  
         Commandline cli;
 99  
         try
 100  
         {
 101  6
             cli = cliBuilder.build( request );
 102  
         }
 103  0
         catch ( CommandLineConfigurationException e )
 104  
         {
 105  0
             throw new MavenInvocationException( "Error configuring command-line. Reason: " + e.getMessage(), e );
 106  6
         }
 107  
 
 108  6
         DefaultInvocationResult result = new DefaultInvocationResult();
 109  
 
 110  
         try
 111  
         {
 112  6
             int exitCode = executeCommandLine( cli, request );
 113  
 
 114  6
             result.setExitCode( exitCode );
 115  
         }
 116  0
         catch ( CommandLineException e )
 117  
         {
 118  0
             result.setExecutionException( e );
 119  6
         }
 120  
 
 121  6
         return result;
 122  
     }
 123  
 
 124  
     private int executeCommandLine( Commandline cli, InvocationRequest request )
 125  
         throws CommandLineException
 126  
     {
 127  6
         int result = Integer.MIN_VALUE;
 128  
 
 129  6
         InputStream inputStream = request.getInputStream( this.inputStream );
 130  6
         InvocationOutputHandler outputHandler = request.getOutputHandler( this.outputHandler );
 131  6
         InvocationOutputHandler errorHandler = request.getErrorHandler( this.errorHandler );
 132  
 
 133  6
         if ( getLogger().isDebugEnabled() )
 134  
         {
 135  6
             getLogger().debug( "Executing: " + cli );
 136  
         }
 137  6
         if ( request.isInteractive() )
 138  
         {
 139  0
             if ( inputStream == null )
 140  
             {
 141  0
                 getLogger().warn(
 142  
                                   "Maven will be executed in interactive mode"
 143  
                                       + ", but no input stream has been configured for this MavenInvoker instance." );
 144  
 
 145  0
                 result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
 146  
             }
 147  
             else
 148  
             {
 149  0
                 result = CommandLineUtils.executeCommandLine( cli, inputStream, outputHandler, errorHandler );
 150  
             }
 151  
         }
 152  
         else
 153  
         {
 154  6
             if ( inputStream != null )
 155  
             {
 156  0
                 getLogger().info( "Executing in batch mode. The configured input stream will be ignored." );
 157  
             }
 158  
 
 159  6
             result = CommandLineUtils.executeCommandLine( cli, outputHandler, errorHandler );
 160  
         }
 161  
 
 162  6
         return result;
 163  
     }
 164  
 
 165  
     public File getLocalRepositoryDirectory()
 166  
     {
 167  6
         return localRepositoryDirectory;
 168  
     }
 169  
 
 170  
     public InvokerLogger getLogger()
 171  
     {
 172  24
         return logger;
 173  
     }
 174  
 
 175  
     public Invoker setLocalRepositoryDirectory( File localRepositoryDirectory )
 176  
     {
 177  6
         this.localRepositoryDirectory = localRepositoryDirectory;
 178  6
         return this;
 179  
     }
 180  
 
 181  
     public Invoker setLogger( InvokerLogger logger )
 182  
     {
 183  6
         this.logger = ( logger != null ) ? logger : DEFAULT_LOGGER;
 184  6
         return this;
 185  
     }
 186  
 
 187  
     public File getWorkingDirectory()
 188  
     {
 189  6
         return workingDirectory;
 190  
     }
 191  
 
 192  
     public Invoker setWorkingDirectory( File workingDirectory )
 193  
     {
 194  0
         this.workingDirectory = workingDirectory;
 195  0
         return this;
 196  
     }
 197  
 
 198  
     public File getMavenHome()
 199  
     {
 200  12
         return mavenHome;
 201  
     }
 202  
 
 203  
     public Invoker setMavenHome( File mavenHome )
 204  
     {
 205  6
         this.mavenHome = mavenHome;
 206  
 
 207  6
         return this;
 208  
     }
 209  
 
 210  
     public File getMavenExecutable()
 211  
     {
 212  6
         return mavenExecutable;
 213  
     }
 214  
 
 215  
     public Invoker setMavenExecutable( File mavenExecutable )
 216  
     {
 217  0
         this.mavenExecutable = mavenExecutable;
 218  0
         return this;
 219  
     }
 220  
 
 221  
     public Invoker setErrorHandler( InvocationOutputHandler errorHandler )
 222  
     {
 223  0
         this.errorHandler = errorHandler;
 224  0
         return this;
 225  
     }
 226  
 
 227  
     public Invoker setInputStream( InputStream inputStream )
 228  
     {
 229  0
         this.inputStream = inputStream;
 230  0
         return this;
 231  
     }
 232  
 
 233  
     public Invoker setOutputHandler( InvocationOutputHandler outputHandler )
 234  
     {
 235  0
         this.outputHandler = outputHandler;
 236  0
         return this;
 237  
     }
 238  
 
 239  
 }