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