From 9149ba09043b8542dbb406c9d84f4269b56ddf78 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Thu, 15 Sep 2005 04:26:09 +0000 Subject: [PATCH] another pass at integration, not ready. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@289142 13f79535-47bb-0310-9956-ffa450edef68 --- maven-1.x-integration/pom.xml | 16 ++- .../legacy/DefaultMaven1xIntegration.java | 101 +++++++++++++----- .../maven/legacy/Maven1xIntegration.java | 10 +- .../legacy/Maven1xIntegrationException.java | 37 +++++++ 4 files changed, 124 insertions(+), 40 deletions(-) create mode 100644 maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegrationException.java diff --git a/maven-1.x-integration/pom.xml b/maven-1.x-integration/pom.xml index 3d3fd8590b..af25264a7e 100644 --- a/maven-1.x-integration/pom.xml +++ b/maven-1.x-integration/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> maven org.apache.maven @@ -12,12 +12,18 @@ plexus - plexus-container-default + plexus-utils - forehead - forehead - 1.0-beta-5 + plexus + plexus-container-default + diff --git a/maven-1.x-integration/src/main/java/org/apache/maven/legacy/DefaultMaven1xIntegration.java b/maven-1.x-integration/src/main/java/org/apache/maven/legacy/DefaultMaven1xIntegration.java index 9dea5c071f..0d2225b289 100644 --- a/maven-1.x-integration/src/main/java/org/apache/maven/legacy/DefaultMaven1xIntegration.java +++ b/maven-1.x-integration/src/main/java/org/apache/maven/legacy/DefaultMaven1xIntegration.java @@ -16,21 +16,17 @@ * limitations under the License. */ -import com.werken.forehead.Forehead; -import com.werken.forehead.ForeheadClassLoader; +import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.util.cli.CommandLineException; +import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.codehaus.plexus.util.cli.Commandline; +import org.codehaus.plexus.util.cli.DefaultConsumer; +import org.codehaus.plexus.util.cli.StreamConsumer; import java.io.File; -import java.io.FileInputStream; -import java.util.Arrays; -import java.util.Date; import java.util.Iterator; import java.util.List; -import java.lang.reflect.*; -import java.net.*; - -import org.codehaus.plexus.logging.AbstractLogEnabled; - /** * @author Brett Porter * @version $Id$ @@ -40,8 +36,8 @@ public class DefaultMaven1xIntegration implements Maven1xIntegration { private String mavenHome; - private String mavenHomeLocal; + private String mavenHomeLocal; // ---------------------------------------------------------------------- // Execution @@ -49,47 +45,98 @@ public class DefaultMaven1xIntegration // TODO: may want an executionresponse returned? If so, that may need to be part of another component public void execute( File project, List goals ) + throws Maven1xIntegrationException { - // TODO: need real integration + Commandline cl = new Commandline(); + + String exec = "maven"; + if ( mavenHome != null ) + { + exec = mavenHome + "/bin/" + exec; + cl.createArgument().setValue( "-Dmaven.home=" + mavenHome ); + } + if ( mavenHomeLocal != null ) + { + cl.createArgument().setValue( "-Dmaven.home.local=" + mavenHomeLocal ); + } + + cl.setExecutable( exec ); + + cl.setWorkingDirectory( project.getParentFile().getAbsolutePath() ); + + for ( Iterator i = goals.iterator(); i.hasNext(); ) + { + cl.createArgument().setValue( (String) i.next() ); + } + + StreamConsumer consumer = new DefaultConsumer(); + try { - ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); - URL foreheadUrl = new File( mavenHome, "lib/forehead-1.0-beta-5.jar" ).toURL(); + int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, consumer ); + if ( exitCode != 0 ) + { + throw new Maven1xIntegrationException( "Received exit code " + exitCode + " from Maven" ); + } + } + catch ( CommandLineException e ) + { + throw new Maven1xIntegrationException( "Can't run goals " + goals, e ); + } + + // TODO: need better integration, requires changes in Maven 1.1 - also want to avoid a dep on forehead in m2/lib + try + { +/* File foreheadConf = new File( mavenHome, "bin/forehead.conf" ); + System.setProperty( "tools.jar", "file:" + System.getProperty( "java.home" ) + "/lib/tools.jar" ); + System.setProperty( "maven.home", mavenHome ); + System.setProperty( "maven.home.local", mavenHome ); + + Forehead.getInstance().config( new FileReader( foreheadConf ) ); + // TODO: this currently System.exit()s + Forehead.getInstance().run( (String[]) goals.toArray( EMPTY_STRING_ARRAY ) ); +*/ + +/* + URL foreheadUrl = new File( mavenHome, "lib/forehead-1.0-beta-5.jar" ).toURL(); + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader cl = URLClassLoader.newInstance( new URL[] { foreheadUrl } ); Class c = Class.forName( "com.werken.forehead.Forehead", true, cl ); Method m = c.getMethod( "getInstance", new Class[] {} ); Object forehead = m.invoke( null, new Object[] {} ); - System.setProperty( "maven.home", mavenHome ); - System.setProperty( "tools.jar", "file:" + System.getProperty( "java.home" ) + "/lib/tools.jar" ); m = c.getMethod( "config", new Class[] { java.io.InputStream.class } ); m.invoke( forehead, new Object[] { new FileInputStream( foreheadConf ) } ); - System.setProperty( "user.dir", project.getParentFile().getAbsolutePath() ); - /*m = c.getMethod( "getClassLoader", new Class[] { String.class } ); - cl = ( ClassLoader ) m.invoke( forehead, new Object[] { "root.maven" } ); +*/ + +/* + m = c.getMethod( "getClassLoader", new Class[]{String.class} ); + cl = (ClassLoader) m.invoke( forehead, new Object[]{"root.maven"} ); Thread.currentThread().setContextClassLoader( cl ); c = Class.forName( "org.apache.maven.cli.App", true, cl ); Object app = c.newInstance(); - m = c.getMethod( "initialize", new Class[] { String[].class } ); - m.invoke( app, new Object[] { (String[]) goals.toArray( new String[0] ) } ); + m = c.getMethod( "initialize", new Class[]{String[].class} ); + m.invoke( app, new Object[]{(String[]) goals.toArray( new String[0] )} ); Object session = c.getDeclaredField( "mavenSession" ).get( app ); m = session.getClass().getMethod( "initialize", new Class[0] ); m.invoke( session, new Object[0] ); m = session.getClass().getMethod( "getRootProject", new Class[0] ); Object p = m.invoke( session, new Object[0] ); - m = session.getClass().getMethod( "attainGoals", new Class[] { p.getClass(), List.class } ); - m.invoke( session, new Object[] { p, goals } ); */ + m = session.getClass().getMethod( "attainGoals", new Class[]{p.getClass(), List.class} ); + m.invoke( session, new Object[]{p, goals} ); +*/ +/* // TODO: this currently system.exit's - m = c.getMethod( "run", new Class[] { String[].class } ); - m.invoke( forehead, new Object[] { (String[]) goals.toArray( new String[0] ) } ); + m = c.getMethod( "run", new Class[]{String[].class} ); + m.invoke( forehead, new Object[]{(String[]) goals.toArray( new String[0] )} ); Thread.currentThread().setContextClassLoader( oldClassLoader ); +*/ } catch ( Exception e ) { - // TODO: handle this - e.printStackTrace(); + throw new Maven1xIntegrationException( "Error executing Maven 1.x", e ); } } diff --git a/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegration.java b/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegration.java index 93bc9e5eaa..39a70fa20f 100644 --- a/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegration.java +++ b/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegration.java @@ -31,14 +31,8 @@ public interface Maven1xIntegration // Execution // ---------------------------------------------------------------------- - void execute( File project, List goals ); - - // ---------------------------------------------------------------------- - // Reactor execution - // ---------------------------------------------------------------------- - - // TODO: perhaps? - // ExecutionResponse executeReactor( String goal, String includes, String excludes ); + void execute( File project, List args, List goals ) + throws Maven1xIntegrationException; // ---------------------------------------------------------------------- // Maven home diff --git a/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegrationException.java b/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegrationException.java new file mode 100644 index 0000000000..b3e603b2eb --- /dev/null +++ b/maven-1.x-integration/src/main/java/org/apache/maven/legacy/Maven1xIntegrationException.java @@ -0,0 +1,37 @@ +package org.apache.maven.legacy; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +/** + * Exception in the Maven 1.x integration. + * + * @author Brett Porter + * @version $Id$ + */ +public class Maven1xIntegrationException + extends Exception +{ + public Maven1xIntegrationException( String message ) + { + super( message ); + } + + public Maven1xIntegrationException( String msg, Exception e ) + { + super( msg, e ); + } +}