You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2009/10/10 01:20:58 UTC
svn commit: r823739 - in
/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it:
Embedded3xLauncher.java ForkedLauncher.java LauncherException.java
Verifier.java
Author: bentmann
Date: Fri Oct 9 23:20:57 2009
New Revision: 823739
URL: http://svn.apache.org/viewvc?rev=823739&view=rev
Log:
o Added support to launch ITs using embedded Maven 3.x
Added:
maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java (with props)
maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java (with props)
maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.java (with props)
Modified:
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/Embedded3xLauncher.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java?rev=823739&view=auto
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java (added)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java Fri Oct 9 23:20:57 2009
@@ -0,0 +1,196 @@
+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.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * @author Benjamin Bentmann
+ */
+class Embedded3xLauncher
+{
+
+ private final Object mavenCli;
+
+ private final Method doMain;
+
+ public Embedded3xLauncher( String mavenHome )
+ throws LauncherException
+ {
+ System.setProperty( "maven.home", mavenHome );
+
+ File config = new File( mavenHome, "bin/m2.conf" );
+
+ ClassLoader bootLoader = getBootLoader( mavenHome );
+
+ ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader( bootLoader );
+ try
+ {
+ Class launcherClass = bootLoader.loadClass( "org.codehaus.plexus.classworlds.launcher.Launcher" );
+
+ Object launcher = launcherClass.newInstance();
+
+ Method configure = launcherClass.getMethod( "configure", new Class[] { InputStream.class } );
+
+ configure.invoke( launcher, new Object[] { new FileInputStream( config ) } );
+
+ Method getWorld = launcherClass.getMethod( "getWorld", null );
+ Object classWorld = getWorld.invoke( launcher, null );
+
+ Method getMainClass = launcherClass.getMethod( "getMainClass", null );
+ Class cliClass = (Class) getMainClass.invoke( launcher, null );
+
+ Constructor newMavenCli = cliClass.getConstructor( new Class[] { classWorld.getClass() } );
+ mavenCli = newMavenCli.newInstance( new Object[] { classWorld } );
+
+ Class[] parameterTypes = { String[].class, String.class, PrintStream.class, PrintStream.class };
+ doMain = cliClass.getMethod( "doMain", parameterTypes );
+ }
+ catch ( ClassNotFoundException e )
+ {
+ throw new LauncherException( "Invalid Maven home directory " + mavenHome, e );
+ }
+ catch ( InstantiationException e )
+ {
+ throw new LauncherException( "Invalid Maven home directory " + mavenHome, e );
+ }
+ catch ( IllegalAccessException e )
+ {
+ throw new LauncherException( "Invalid Maven home directory " + mavenHome, e );
+ }
+ catch ( NoSuchMethodException e )
+ {
+ throw new LauncherException( "Invalid Maven home directory " + mavenHome, e );
+ }
+ catch ( InvocationTargetException e )
+ {
+ throw new LauncherException( "Invalid Maven home directory " + mavenHome, e );
+ }
+ catch ( IOException e )
+ {
+ throw new LauncherException( "Invalid Maven home directory " + mavenHome, e );
+ }
+ finally
+ {
+ Thread.currentThread().setContextClassLoader( oldClassLoader );
+ }
+ }
+
+ private static ClassLoader getBootLoader( String mavenHome )
+ {
+ File bootDir = new File( mavenHome, "boot" );
+
+ List urls = new ArrayList();
+
+ addUrls( urls, bootDir );
+
+ if ( urls.isEmpty() )
+ {
+ throw new IllegalArgumentException( "Invalid Maven home directory " + mavenHome );
+ }
+
+ URL[] ucp = (URL[]) urls.toArray( new URL[urls.size()] );
+
+ return new URLClassLoader( ucp, ClassLoader.getSystemClassLoader().getParent() );
+ }
+
+ private static void addUrls( List urls, File directory )
+ {
+ File[] jars = directory.listFiles();
+
+ if ( jars != null )
+ {
+ for ( int i = 0; i < jars.length; i++ )
+ {
+ File jar = jars[i];
+
+ if ( jar.getName().endsWith( ".jar" ) )
+ {
+ try
+ {
+ urls.add( jar.toURI().toURL() );
+ }
+ catch ( MalformedURLException e )
+ {
+ throw (RuntimeException) new IllegalStateException().initCause( 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/Embedded3xLauncher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/Embedded3xLauncher.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: 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=823739&view=auto
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java (added)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java Fri Oct 9 23:20:57 2009
@@ -0,0 +1,115 @@
+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.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.maven.it.util.cli.CommandLineException;
+import org.apache.maven.it.util.cli.CommandLineUtils;
+import org.apache.maven.it.util.cli.Commandline;
+import org.apache.maven.it.util.cli.StreamConsumer;
+import org.apache.maven.it.util.cli.WriterStreamConsumer;
+
+/**
+ * @author Benjamin Bentmann
+ */
+class ForkedLauncher
+{
+
+ private final String mavenHome;
+
+ private final String executable;
+
+ public ForkedLauncher( String mavenHome )
+ {
+ this.mavenHome = mavenHome;
+
+ if ( mavenHome != null )
+ {
+ executable = new File( mavenHome, "bin/mvn" ).getPath();
+ }
+ else
+ {
+ executable = "mvn";
+ }
+ }
+
+ public int run( String[] cliArgs, Map envVars, String workingDirectory, File logFile )
+ throws IOException, LauncherException
+ {
+ Commandline cmd = new Commandline();
+
+ cmd.setExecutable( executable );
+
+ if ( mavenHome != null )
+ {
+ cmd.addEnvironment( "M2_HOME", mavenHome );
+ }
+
+ if ( envVars != null )
+ {
+ for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); )
+ {
+ String key = (String) i.next();
+
+ cmd.addEnvironment( key, (String) envVars.get( key ) );
+ }
+ }
+
+ if ( envVars == null || envVars.get( "JAVA_HOME" ) == null )
+ {
+ cmd.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
+ }
+
+ cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
+
+ cmd.setWorkingDirectory( workingDirectory );
+
+ for ( int i = 0; i < cliArgs.length; i++ )
+ {
+ cmd.createArgument().setValue( cliArgs[i] );
+ }
+
+ Writer logWriter = new FileWriter( logFile );
+
+ StreamConsumer out = new WriterStreamConsumer( logWriter );
+
+ StreamConsumer err = new WriterStreamConsumer( logWriter );
+
+ try
+ {
+ return CommandLineUtils.executeCommandLine( cmd, out, err );
+ }
+ catch ( CommandLineException e )
+ {
+ throw new LauncherException( "Failed to run Maven: " + e.getMessage() + "\n" + cmd, e );
+ }
+ finally
+ {
+ logWriter.close();
+ }
+ }
+
+}
Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/ForkedLauncher.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.java?rev=823739&view=auto
==============================================================================
--- maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.java (added)
+++ maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.java Fri Oct 9 23:20:57 2009
@@ -0,0 +1,39 @@
+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.
+ */
+
+/**
+ * @author Benjamin Bentmann
+ */
+class LauncherException
+ extends Exception
+{
+
+ public LauncherException( String message )
+ {
+ super( message );
+ }
+
+ public LauncherException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+}
Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: maven/shared/trunk/maven-verifier/src/main/java/org/apache/maven/it/LauncherException.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=823739&r1=823738&r2=823739&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 Fri Oct 9 23:20:57 2009
@@ -37,7 +37,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
@@ -46,13 +45,8 @@
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Writer;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
-import java.net.URLClassLoader;
-import java.security.AccessControlException;
-import java.security.Permission;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
@@ -106,7 +100,7 @@
private boolean debug;
- private boolean forkJvm = true;
+ private Boolean forkJvm;
private String logFileName = LOG_FILENAME;
@@ -115,6 +109,22 @@
// will launch mvn with --debug
private boolean mavenDebug = false;
+ private String forkMode;
+
+ private static Embedded3xLauncher embeddedLauncher;
+
+ public Verifier( String basedir )
+ throws VerificationException
+ {
+ this( basedir, null );
+ }
+
+ public Verifier( String basedir, boolean debug )
+ throws VerificationException
+ {
+ this( basedir, null, debug );
+ }
+
public Verifier( String basedir, String settingsFile )
throws VerificationException
{
@@ -124,17 +134,24 @@
public Verifier( String basedir, String settingsFile, boolean debug )
throws VerificationException
{
- this( basedir, settingsFile, debug, true );
+ this( basedir, settingsFile, debug, null );
}
public Verifier( String basedir, String settingsFile, boolean debug, boolean forkJvm )
throws VerificationException
{
+ this( basedir, settingsFile, debug, Boolean.valueOf( forkJvm ) );
+ }
+
+ private Verifier( String basedir, String settingsFile, boolean debug, Boolean forkJvm )
+ throws VerificationException
+ {
this.basedir = basedir;
this.debug = debug;
this.forkJvm = forkJvm;
+ this.forkMode = System.getProperty( "verifier.forkMode" );
if ( !debug )
{
@@ -168,18 +185,15 @@
throw new VerificationException( "Cannot read system environment variables.", e );
}
}
- }
-
- public Verifier( String basedir )
- throws VerificationException
- {
- this( basedir, null );
- }
- public Verifier( String basedir, boolean debug )
- throws VerificationException
- {
- this( basedir, null, debug );
+ if ( defaultMavenHome == null )
+ {
+ File f = new File( System.getProperty( "user.home" ), "m2" );
+ if ( new File( f, "bin/mvn" ).isFile() )
+ {
+ defaultMavenHome = f.getAbsolutePath();
+ }
+ }
}
public void setLocalRepo( String localRepo )
@@ -1197,93 +1211,109 @@
allGoals.addAll( goals );
- Commandline cli = null;
+ List args = new ArrayList();
+
int ret;
File logFile = new File( getBasedir(), getLogFileName() );
- try
- {
- cli = createCommandLine();
- for ( Iterator i = envVars.keySet().iterator(); i.hasNext(); )
- {
- String key = (String) i.next();
-
- cli.addEnvironment( key, (String) envVars.get( key ) );
+ for ( Iterator it = cliOptions.iterator(); it.hasNext(); )
+ {
+ String key = String.valueOf( it.next() );
- /* What was the point of this? It doesn't work on windows.
- * try
- {
- FileUtils.fileWrite( "/tmp/foo.txt", "setting envar[ " + key + " = " + envVars.get( key ) );
- }
- catch ( IOException e )
- {
- e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates.
- }*/
+ String resolvedArg = resolveCommandLineArg( key );
- // System.out.println();
+ try
+ {
+ args.addAll( Arrays.asList( Commandline.translateCommandline( resolvedArg ) ) );
}
-
- if ( envVars.get( "JAVA_HOME" ) == null )
+ catch ( Exception e )
{
- cli.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
+ e.printStackTrace();
}
+ }
- cli.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
+ args.add( "-e" );
- cli.setWorkingDirectory( getBasedir() );
+ args.add( "--batch-mode" );
- for ( Iterator it = cliOptions.iterator(); it.hasNext(); )
- {
- String key = String.valueOf( it.next() );
+ if ( this.mavenDebug )
+ {
+ args.add( "--debug" );
+ }
- String resolvedArg = resolveCommandLineArg( key );
+ for ( Iterator i = systemProperties.keySet().iterator(); i.hasNext(); )
+ {
+ String key = (String) i.next();
+ String value = systemProperties.getProperty( key );
+ args.add( "-D" + key + "=" + value );
+ }
- cli.createArgument().setLine( resolvedArg );
- }
+ /*
+ * NOTE: Unless explicitly requested by the caller, the forked builds should use the current local
+ * repository. Otherwise, the forked builds would in principle leave the sandbox environment which has been
+ * setup for the current build. In particular, using "maven.repo.local" will make sure the forked builds use
+ * the same local repo as the parent build even if a custom user settings is provided.
+ */
+ boolean useMavenRepoLocal =
+ Boolean.valueOf( verifierProperties.getProperty( "use.mavenRepoLocal", "true" ) ).booleanValue();
- cli.createArgument().setValue( "-e" );
+ if ( useMavenRepoLocal )
+ {
+ args.add( "-Dmaven.repo.local=" + localRepo );
+ }
- cli.createArgument().setValue( "--batch-mode" );
-
- if ( this.mavenDebug )
- {
- cli.createArgument().setValue( "--debug" );
- }
+ args.addAll( allGoals );
+
+ try
+ {
+ String[] cliArgs = (String[]) args.toArray( new String[args.size()] );
- for ( Iterator i = systemProperties.keySet().iterator(); i.hasNext(); )
+ boolean fork;
+ if ( forkJvm != null )
{
- String key = (String) i.next();
- String value = systemProperties.getProperty( key );
- cli.createArgument().setValue( "-D" + key + "=" + value );
+ fork = forkJvm.booleanValue();
}
+ else if ( envVars.isEmpty() && "auto".equalsIgnoreCase( forkMode ) )
+ {
+ fork = false;
- /*
- * NOTE: Unless explicitly requested by the caller, the forked builds should use the current local
- * repository. Otherwise, the forked builds would in principle leave the sandbox environment which has been
- * setup for the current build. In particular, using "maven.repo.local" will make sure the forked builds use
- * the same local repo as the parent build even if a custom user settings is provided.
- */
- boolean useMavenRepoLocal =
- Boolean.valueOf( verifierProperties.getProperty( "use.mavenRepoLocal", "true" ) ).booleanValue();
-
- if ( useMavenRepoLocal )
+ if ( embeddedLauncher == null )
+ {
+ try
+ {
+ embeddedLauncher = new Embedded3xLauncher( defaultMavenHome );
+ }
+ catch ( Exception e )
+ {
+ fork = true;
+ }
+ }
+ }
+ else
{
- cli.createArgument().setValue( "-Dmaven.repo.local=" + localRepo );
+ fork = true;
}
- for ( Iterator i = allGoals.iterator(); i.hasNext(); )
+ if ( !fork )
{
- cli.createArgument().setValue( (String) i.next() );
- }
+ if ( embeddedLauncher == null )
+ {
+ embeddedLauncher = new Embedded3xLauncher( defaultMavenHome );
+ }
- // System.out.println( "Command: " + Commandline.toString( cli.getCommandline() ) );
+ ret = embeddedLauncher.run( cliArgs, getBasedir(), logFile );
+ }
+ else
+ {
+ ForkedLauncher launcher = new ForkedLauncher( defaultMavenHome );
- ret = runCommandLine( cli, logFile );
+ ret = launcher.run( cliArgs, envVars, getBasedir(), logFile );
+ }
}
- catch ( CommandLineException e )
+ catch ( LauncherException e )
{
- throw new VerificationException( "Failed to execute Maven: " + cli, e );
+ throw new VerificationException( "Failed to execute Maven: " + e.getMessage(), e );
}
catch ( IOException e )
{
@@ -1294,21 +1324,21 @@
{
System.err.println( "Exit code: " + ret );
- throw new VerificationException( "Exit code was non-zero: " + ret + "; command line and log = \n" + cli
- + "\n" + getLogContents( logFile ) );
+ throw new VerificationException( "Exit code was non-zero: " + ret + "; command line and log = \n"
+ + new File( defaultMavenHome, "bin/mvn" ) + " " + StringUtils.join( args.iterator(), " " ) + "\n"
+ + getLogContents( logFile ) );
}
}
public String getMavenVersion()
throws VerificationException
{
- Commandline cmd = createCommandLine();
- cmd.addArguments( new String[] { "--version" } );
+ ForkedLauncher launcher = new ForkedLauncher( defaultMavenHome );
- File log;
+ File logFile;
try
{
- log = File.createTempFile( "maven", "log" );
+ logFile = File.createTempFile( "maven", "log" );
}
catch ( IOException e )
{
@@ -1317,21 +1347,21 @@
try
{
- runCommandLine( cmd, log );
+ launcher.run( new String[] { "--version" }, null, null, logFile );
}
- catch ( CommandLineException e )
+ catch ( LauncherException e )
{
- throw new VerificationException( "Error running commandline " + cmd.toString(), e );
+ throw new VerificationException( "Error running commandline " + e.toString(), e );
}
catch ( IOException e )
{
- throw new VerificationException( "IO Error communicating with commandline " + cmd.toString(), e );
+ throw new VerificationException( "IO Error communicating with commandline " + e.toString(), e );
}
String version = null;
- List logLines = loadFile( log, false );
- log.delete();
+ List logLines = loadFile( logFile, false );
+ logFile.delete();
for ( Iterator it = logLines.iterator(); version == null && it.hasNext(); )
{
@@ -1373,197 +1403,6 @@
}
}
- private Commandline createCommandLine()
- {
- Commandline cmd = new Commandline();
- String executable = getExecutable();
- if ( executable.endsWith( "/bin/mvn" ) )
- {
- cmd.addEnvironment( "M2_HOME", executable.substring( 0, executable.length() - 8 ) );
- }
- cmd.setExecutable( executable );
- return cmd;
- }
-
- private int runCommandLine( Commandline cli, File logFile )
- throws CommandLineException, IOException
- {
- if ( forkJvm )
- {
- Writer logWriter = new FileWriter( logFile );
-
- StreamConsumer out = new WriterStreamConsumer( logWriter );
-
- StreamConsumer err = new WriterStreamConsumer( logWriter );
-
- try
- {
- return CommandLineUtils.executeCommandLine( cli, out, err );
- }
- finally
- {
- logWriter.close();
- }
- }
-
- String mavenHome = defaultMavenHome;
-
- if ( mavenHome == null )
- {
- mavenHome = System.getProperty( "user.home" ) + "/local/apache-maven-2.1-SNAPSHOT";
- }
-
- File coreDir = new File( mavenHome, "core/boot" );
- File[] files = coreDir.listFiles();
- File classWorldFile = null;
- for ( int i = 0; files != null && i < files.length; i++ )
- {
- if ( files[i].getName().indexOf( "plexus-classworlds" ) >= 0 )
- {
- classWorldFile = files[i];
- break;
- }
- }
-
- if ( classWorldFile == null )
- {
- throw new CommandLineException( "Cannot find plexus-classworlds in " + coreDir );
- }
-
- URLClassLoader cl;
-
- try
- {
- cl = new URLClassLoader( new URL[] { classWorldFile.toURI().toURL() }, null );
- }
- catch ( MalformedURLException e )
- {
- throw new CommandLineException( "Cannot conver to url: " + classWorldFile, e );
- }
-
- class ExitSecurityException
- extends SecurityException
- {
-
- private int status;
-
- public ExitSecurityException( int status )
- {
- this.status = status;
- }
-
- public int getStatus()
- {
- return status;
- }
- }
-
- try
- {
- Class c = cl.loadClass( "org.codehaus.plexus.classworlds.launcher.Launcher" );
-
- Method m = c.getMethod( "mainWithExitCode", new Class[] { String[].class } );
-
- SecurityManager oldSm = System.getSecurityManager();
-
- try
- {
- System.setSecurityManager( new SecurityManager()
- {
- public void checkPermission( Permission perm )
- {
- // ok
- }
-
- public void checkExit( int status )
- {
- throw new ExitSecurityException( status );
- }
- } );
- }
- catch ( AccessControlException e )
- {
- throw new CommandLineException( "Error isntalling securitymanager", e );
- }
-
- cli.createArgument().setValue( "-f" );
- cli.createArgument().setValue( cli.getWorkingDirectory().getAbsolutePath() + "/pom.xml" );
-
- PrintStream oldOut = System.out;
- PrintStream oldErr = System.err;
-
- String oldCwConf = System.getProperty( "classworlds.conf" );
- String oldMavenHome = System.getProperty( "maven.home" );
-
- ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
-
- try
- {
- Thread.currentThread().setContextClassLoader( cl );// ClassLoader.getSystemClassLoader() );
- FileOutputStream logWriter = new FileOutputStream( logFile );
- System.setOut( new PrintStream( logWriter ) );
- System.setErr( new PrintStream( logWriter ) );
-
- System.setProperty( "classworlds.conf", new File( mavenHome, "bin/m2.conf" ).getAbsolutePath() );
- System.setProperty( "maven.home", mavenHome );
-
- return ( (Integer) m.invoke( null, new Object[] { cli.getArguments() } ) ).intValue();
- }
- catch ( ExitSecurityException e )
- {
- oldOut.println( "exit security exception caught: status=" + e.getStatus() );
- return e.getStatus();
- }
- finally
- {
- System.setOut( oldOut );
- System.setErr( oldErr );
- if ( oldCwConf == null )
- {
- System.getProperties().remove( "classworlds.conf" );
- }
- else
- {
- System.setProperty( "classworlds.conf", oldCwConf );
- }
- if ( oldMavenHome == null )
- {
- System.getProperties().remove( "maven.home" );
- }
- else
- {
- System.setProperty( "maven.home", oldMavenHome );
- }
- Thread.currentThread().setContextClassLoader( oldCl );
- System.setSecurityManager( oldSm );
- }
- }
- catch ( ClassNotFoundException e )
- {
- throw new CommandLineException( "Cannot load classworlds launcher", e );
- }
- catch ( NoSuchMethodException e )
- {
- throw new CommandLineException( "Cannot find classworlds launcher's main method", e );
- }
- catch ( IllegalArgumentException e )
- {
- throw new CommandLineException( "Error executing classworlds launcher's main method", e );
- }
- catch ( InvocationTargetException e )
- {
- if ( e.getCause() instanceof ExitSecurityException )
- {
- return ( (ExitSecurityException) e.getCause() ).getStatus();
- }
- throw new CommandLineException( "Error executing classworlds launcher's main method", e );
- }
- catch ( IllegalAccessException e )
- {
- throw new CommandLineException( "Error executing classworlds launcher's main method", e );
- }
- }
-
private static String getLogContents( File logFile )
{
try
@@ -2001,6 +1840,18 @@
this.systemProperties = systemProperties;
}
+ public void setSystemProperty( String key, String value )
+ {
+ if ( value != null )
+ {
+ systemProperties.setProperty( key, value );
+ }
+ else
+ {
+ systemProperties.remove( key );
+ }
+ }
+
public Properties getVerifierProperties()
{
return verifierProperties;