Coverage Report - org.apache.maven.it.Classpath3xLauncher
 
Classes in this File Line Coverage Branch Coverage Complexity
Classpath3xLauncher
0%
0/33
0%
0/4
8.5
 
 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.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.PrintStream;
 26  
 import java.lang.reflect.InvocationTargetException;
 27  
 import java.lang.reflect.Method;
 28  
 import java.util.Properties;
 29  
 
 30  
 /**
 31  
  * Launches an embedded Maven 3.x instance from the current class path, i.e. the Maven 3.x dependencies are assumed to
 32  
  * be present on the class path.
 33  
  *
 34  
  * @author Benjamin Bentmann
 35  
  */
 36  
 class Classpath3xLauncher
 37  
     implements MavenLauncher
 38  
 {
 39  
 
 40  
     private final Object mavenCli;
 41  
 
 42  
     private final Method doMain;
 43  
 
 44  
     public Classpath3xLauncher()
 45  
         throws LauncherException
 46  0
     {
 47  0
         ClassLoader coreLoader = Thread.currentThread().getContextClassLoader();
 48  
 
 49  
         try
 50  
         {
 51  0
             Class cliClass = coreLoader.loadClass( "org.apache.maven.cli.MavenCli" );
 52  
 
 53  0
             mavenCli = cliClass.newInstance();
 54  
 
 55  0
             Class[] parameterTypes = { String[].class, String.class, PrintStream.class, PrintStream.class };
 56  0
             doMain = cliClass.getMethod( "doMain", parameterTypes );
 57  
         }
 58  0
         catch ( ClassNotFoundException e )
 59  
         {
 60  0
             throw new LauncherException( e.getMessage(), e );
 61  
         }
 62  0
         catch ( NoSuchMethodException e )
 63  
         {
 64  0
             throw new LauncherException( e.getMessage(), e );
 65  
         }
 66  0
         catch ( InstantiationException e )
 67  
         {
 68  0
             throw new LauncherException( e.getMessage(), e );
 69  
         }
 70  0
         catch ( IllegalAccessException e )
 71  
         {
 72  0
             throw new LauncherException( e.getMessage(), e );
 73  0
         }
 74  0
     }
 75  
 
 76  
     public int run( String[] cliArgs, String workingDirectory, File logFile )
 77  
         throws IOException, LauncherException
 78  
     {
 79  0
         PrintStream out = ( logFile != null ) ? new PrintStream( new FileOutputStream( logFile ) ) : System.out;
 80  
         try
 81  
         {
 82  0
             Properties originalProperties = System.getProperties();
 83  0
             System.setProperties( null );
 84  0
             System.setProperty( "maven.home", originalProperties.getProperty( "maven.home", "" ) );
 85  0
             System.setProperty( "user.dir", new File( workingDirectory ).getAbsolutePath() );
 86  
 
 87  0
             ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
 88  0
             Thread.currentThread().setContextClassLoader( mavenCli.getClass().getClassLoader() );
 89  
             try
 90  
             {
 91  0
                 Object result = doMain.invoke( mavenCli, new Object[]{ cliArgs, workingDirectory, out, out } );
 92  
 
 93  0
                 return ( (Number) result ).intValue();
 94  
             }
 95  
             finally
 96  
             {
 97  0
                 Thread.currentThread().setContextClassLoader( originalClassLoader );
 98  
 
 99  0
                 System.setProperties( originalProperties );
 100  
             }
 101  
         }
 102  0
         catch ( IllegalAccessException e )
 103  
         {
 104  0
             throw new LauncherException( "Failed to run Maven: " + e.getMessage(), e );
 105  
         }
 106  0
         catch ( InvocationTargetException e )
 107  
         {
 108  0
             throw new LauncherException( "Failed to run Maven: " + e.getMessage(), e );
 109  
         }
 110  
         finally
 111  
         {
 112  0
             if ( logFile != null )
 113  
             {
 114  0
                 out.close();
 115  
             }
 116  
         }
 117  
     }
 118  
 
 119  
 }