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