Coverage Report - org.apache.maven.it.ForkedLauncher
 
Classes in this File Line Coverage Branch Coverage Complexity
ForkedLauncher
0%
0/32
0%
0/16
3.25
 
 1  
 package org.apache.maven.it;
 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.FileWriter;
 24  
 import java.io.IOException;
 25  
 import java.io.Writer;
 26  
 import java.util.Collections;
 27  
 import java.util.Iterator;
 28  
 import java.util.Map;
 29  
 
 30  
 import org.apache.maven.it.util.cli.CommandLineException;
 31  
 import org.apache.maven.it.util.cli.CommandLineUtils;
 32  
 import org.apache.maven.it.util.cli.Commandline;
 33  
 import org.apache.maven.it.util.cli.StreamConsumer;
 34  
 import org.apache.maven.it.util.cli.WriterStreamConsumer;
 35  
 
 36  
 /**
 37  
  * @author Benjamin Bentmann
 38  
  */
 39  
 class ForkedLauncher
 40  
     implements MavenLauncher
 41  
 {
 42  
 
 43  
     private final String mavenHome;
 44  
 
 45  
     private final String executable;
 46  
 
 47  
     public ForkedLauncher( String mavenHome )
 48  
     {
 49  0
         this( mavenHome, false );
 50  0
     }
 51  
 
 52  
     public ForkedLauncher( String mavenHome, boolean debugJvm )
 53  0
     {
 54  0
         this.mavenHome = mavenHome;
 55  
 
 56  0
         String script = debugJvm ? "mvnDebug" : "mvn";
 57  
 
 58  0
         if ( mavenHome != null )
 59  
         {
 60  0
             executable = new File( mavenHome, "bin/" + script ).getPath();
 61  
         }
 62  
         else
 63  
         {
 64  0
             executable = script;
 65  
         }
 66  0
     }
 67  
 
 68  
     public int run( String[] cliArgs, Map envVars, String workingDirectory, File logFile )
 69  
         throws IOException, LauncherException
 70  
     {
 71  0
         Commandline cmd = new Commandline();
 72  
 
 73  0
         cmd.setExecutable( executable );
 74  
 
 75  0
         if ( mavenHome != null )
 76  
         {
 77  0
             cmd.addEnvironment( "M2_HOME", mavenHome );
 78  
         }
 79  
 
 80  0
         if ( envVars != null )
 81  
         {
 82  0
             for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); )
 83  
             {
 84  0
                 String key = (String) i.next();
 85  
 
 86  0
                 cmd.addEnvironment( key, (String) envVars.get( key ) );
 87  0
             }
 88  
         }
 89  
 
 90  0
         if ( envVars == null || envVars.get( "JAVA_HOME" ) == null )
 91  
         {
 92  0
             cmd.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
 93  
         }
 94  
 
 95  0
         cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
 96  
 
 97  0
         cmd.setWorkingDirectory( workingDirectory );
 98  
 
 99  0
         for ( int i = 0; i < cliArgs.length; i++ )
 100  
         {
 101  0
             cmd.createArgument().setValue( cliArgs[i] );
 102  
         }
 103  
 
 104  0
         Writer logWriter = new FileWriter( logFile );
 105  
 
 106  0
         StreamConsumer out = new WriterStreamConsumer( logWriter );
 107  
 
 108  0
         StreamConsumer err = new WriterStreamConsumer( logWriter );
 109  
 
 110  
         try
 111  
         {
 112  0
             return CommandLineUtils.executeCommandLine( cmd, out, err );
 113  
         }
 114  0
         catch ( CommandLineException e )
 115  
         {
 116  0
             throw new LauncherException( "Failed to run Maven: " + e.getMessage() + "\n" + cmd, e );
 117  
         }
 118  
         finally
 119  
         {
 120  0
             logWriter.close();
 121  
         }
 122  
     }
 123  
 
 124  
     public int run( String[] cliArgs, String workingDirectory, File logFile )
 125  
         throws IOException, LauncherException
 126  
     {
 127  0
         return run( cliArgs, Collections.EMPTY_MAP, workingDirectory, logFile );
 128  
     }
 129  
 
 130  
 }