maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bentm...@apache.org
Subject svn commit: r824335 - in /maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it: Classpath3xLauncher.java Embedded3xLauncher.java ForkedLauncher.java MavenLauncher.java Verifier.java
Date Mon, 12 Oct 2009 13:47:17 GMT
Author: bentmann
Date: Mon Oct 12 13:47:17 2009
New Revision: 824335

URL: http://svn.apache.org/viewvc?rev=824335&view=rev
Log:
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

Added:
    maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
  (with props)
    maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java
  (with props)
Modified:
    maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
    maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java
    maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java

Added: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java?rev=824335&view=auto
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
(added)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
Mon Oct 12 13:47:17 2009
@@ -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();
+            }
+        }
+    }
+
+}

Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Classpath3xLauncher.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java?rev=824335&r1=824334&r2=824335&view=diff
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
(original)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
Mon Oct 12 13:47:17 2009
@@ -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" );

Modified: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java?rev=824335&r1=824334&r2=824335&view=diff
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java
(original)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java
Mon Oct 12 13:47:17 2009
@@ -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 );
+    }
+
 }

Added: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java?rev=824335&view=auto
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java
(added)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java
Mon Oct 12 13:47:17 2009
@@ -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;
+
+}

Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/MavenLauncher.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java?rev=824335&r1=824334&r2=824335&view=diff
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java (original)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Verifier.java Mon
Oct 12 13:47:17 2009
@@ -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 )
         {



Mime
View raw message