o Added another embedded launcher that does not load Maven from a home directory but from the class path, this allows us to run the core ITs with the Maven from our IDE workspace

git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@824335 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/it/Classpath3xLauncher.java b/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
new file mode 100644
index 0000000..7da46bf
--- /dev/null
+++ b/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
@@ -0,0 +1,118 @@
+package org.apache.maven.it;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Properties;
+
+/**
+ * Launches an embedded Maven 3.x instance from the current class path, i.e. the Maven 3.x dependencies are assumed to
+ * be present on the class path.
+ * 
+ * @author Benjamin Bentmann
+ */
+class Classpath3xLauncher
+    implements MavenLauncher
+{
+
+    private final Object mavenCli;
+
+    private final Method doMain;
+
+    public Classpath3xLauncher()
+        throws LauncherException
+    {
+        ClassLoader coreLoader = Thread.currentThread().getContextClassLoader();
+
+        try
+        {
+            Class cliClass = coreLoader.loadClass( "org.apache.maven.cli.MavenCli" );
+
+            mavenCli = cliClass.newInstance();
+
+            Class[] parameterTypes = { String[].class, String.class, PrintStream.class, PrintStream.class };
+            doMain = cliClass.getMethod( "doMain", parameterTypes );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            throw new LauncherException( e.getMessage(), e );
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new LauncherException( e.getMessage(), e );
+        }
+        catch ( InstantiationException e )
+        {
+            throw new LauncherException( e.getMessage(), e );
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new LauncherException( e.getMessage(), e );
+        }
+    }
+
+    public int run( String[] cliArgs, String workingDirectory, File logFile )
+        throws IOException, LauncherException
+    {
+        PrintStream out = ( logFile != null ) ? new PrintStream( new FileOutputStream( logFile ) ) : System.out;
+        try
+        {
+            Properties originalProperties = System.getProperties();
+            System.setProperties( null );
+            System.setProperty( "maven.home", originalProperties.getProperty( "maven.home", "" ) );
+
+            ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+            Thread.currentThread().setContextClassLoader( mavenCli.getClass().getClassLoader() );
+            try
+            {
+                Object result = doMain.invoke( mavenCli, new Object[] { cliArgs, workingDirectory, out, out } );
+
+                return ( (Number) result ).intValue();
+            }
+            finally
+            {
+                Thread.currentThread().setContextClassLoader( originalClassLoader );
+
+                System.setProperties( originalProperties );
+            }
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new LauncherException( "Failed to run Maven: " + e.getMessage(), e );
+        }
+        catch ( InvocationTargetException e )
+        {
+            throw new LauncherException( "Failed to run Maven: " + e.getMessage(), e );
+        }
+        finally
+        {
+            if ( logFile != null )
+            {
+                out.close();
+            }
+        }
+    }
+
+}
diff --git a/src/main/java/org/apache/maven/it/Embedded3xLauncher.java b/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
index 65bbccd..9e218d8 100644
--- a/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
+++ b/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
@@ -36,9 +36,12 @@
 import java.util.Properties;
 
 /**
+ * Launches an embedded Maven 3.x instance from some Maven installation directory.
+ * 
  * @author Benjamin Bentmann
  */
 class Embedded3xLauncher
+    implements MavenLauncher
 {
 
     private final Object mavenCli;
@@ -48,6 +51,11 @@
     public Embedded3xLauncher( String mavenHome )
         throws LauncherException
     {
+        if ( mavenHome == null || mavenHome.length() <= 0 )
+        {
+            throw new LauncherException( "Invalid Maven home directory " + mavenHome );
+        }
+
         System.setProperty( "maven.home", mavenHome );
 
         File config = new File( mavenHome, "bin/m2.conf" );
diff --git a/src/main/java/org/apache/maven/it/ForkedLauncher.java b/src/main/java/org/apache/maven/it/ForkedLauncher.java
index 2d5557b..2559e40 100644
--- a/src/main/java/org/apache/maven/it/ForkedLauncher.java
+++ b/src/main/java/org/apache/maven/it/ForkedLauncher.java
@@ -23,6 +23,7 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -36,6 +37,7 @@
  * @author Benjamin Bentmann
  */
 class ForkedLauncher
+    implements MavenLauncher
 {
 
     private final String mavenHome;
@@ -112,4 +114,10 @@
         }
     }
 
+    public int run( String[] cliArgs, String workingDirectory, File logFile )
+        throws IOException, LauncherException
+    {
+        return run( cliArgs, Collections.EMPTY_MAP, workingDirectory, logFile );
+    }
+
 }
diff --git a/src/main/java/org/apache/maven/it/MavenLauncher.java b/src/main/java/org/apache/maven/it/MavenLauncher.java
new file mode 100644
index 0000000..8084cdf
--- /dev/null
+++ b/src/main/java/org/apache/maven/it/MavenLauncher.java
@@ -0,0 +1,34 @@
+package org.apache.maven.it;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author Benjamin Bentmann
+ */
+interface MavenLauncher
+{
+
+    int run( String[] cliArgs, String workingDirectory, File logFile )
+        throws IOException, LauncherException;
+
+}
diff --git a/src/main/java/org/apache/maven/it/Verifier.java b/src/main/java/org/apache/maven/it/Verifier.java
index fb8941e..2961a94 100644
--- a/src/main/java/org/apache/maven/it/Verifier.java
+++ b/src/main/java/org/apache/maven/it/Verifier.java
@@ -111,7 +111,7 @@
 
     private String forkMode;
 
-    private static Embedded3xLauncher embeddedLauncher;
+    private static MavenLauncher embeddedLauncher;
 
     public Verifier( String basedir )
         throws VerificationException
@@ -166,6 +166,11 @@
 
         findLocalRepo( settingsFile );
         findDefaultMavenHome();
+
+        if ( StringUtils.isEmpty( defaultMavenHome ) && StringUtils.isEmpty( forkMode ) )
+        {
+            forkMode = "auto";
+        }
     }
 
     private void findDefaultMavenHome()
@@ -1278,16 +1283,13 @@
             {
                 fork = false;
 
-                if ( embeddedLauncher == null )
+                try
                 {
-                    try
-                    {
-                        embeddedLauncher = new Embedded3xLauncher( defaultMavenHome );
-                    }
-                    catch ( Exception e )
-                    {
-                        fork = true;
-                    }
+                    initEmbeddedLauncher();
+                }
+                catch ( Exception e )
+                {
+                    fork = true;
                 }
             }
             else
@@ -1297,10 +1299,7 @@
 
             if ( !fork )
             {
-                if ( embeddedLauncher == null )
-                {
-                    embeddedLauncher = new Embedded3xLauncher( defaultMavenHome );
-                }
+                initEmbeddedLauncher();
 
                 ret = embeddedLauncher.run( cliArgs, getBasedir(), logFile );
             }
@@ -1330,10 +1329,26 @@
         }
     }
 
+    private void initEmbeddedLauncher()
+        throws LauncherException
+    {
+        if ( embeddedLauncher == null )
+        {
+            if ( StringUtils.isEmpty( defaultMavenHome ) )
+            {
+                embeddedLauncher = new Classpath3xLauncher();
+            }
+            else
+            {
+                embeddedLauncher = new Embedded3xLauncher( defaultMavenHome );
+            }
+        }
+    }
+
     public String getMavenVersion()
         throws VerificationException
     {
-        ForkedLauncher launcher = new ForkedLauncher( defaultMavenHome );
+        MavenLauncher launcher = new ForkedLauncher( defaultMavenHome );
 
         File logFile;
         try
@@ -1347,7 +1362,7 @@
 
         try
         {
-            launcher.run( new String[] { "--version" }, null, null, logFile );
+            launcher.run( new String[] { "--version" }, null, logFile );
         }
         catch ( LauncherException e )
         {