From 08df9d163d416caa8975d7edf22db0c5e0b0dd10 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 16 Mar 2005 23:19:00 +0000 Subject: [PATCH] PR: MNG-209 re-initialise lifecycle on each execution. Make it threadsafe. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163586 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/DefaultMaven.java | 10 +-- .../lifecycle/DefaultLifecycleExecutor.java | 82 +++++++++---------- .../maven/lifecycle/LifecycleExecutor.java | 2 - .../org/apache/maven/lifecycle/Phase.java | 35 ++++++++ 4 files changed, 78 insertions(+), 51 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index bbc7840adf..64b3335ef6 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -189,12 +189,12 @@ public class DefaultMaven private MavenExecutionResponse processProject( MavenExecutionRequest request, MavenProject project, EventDispatcher dispatcher, List goals ) - throws ComponentLookupException + throws Exception { MavenSession session = createSession( request ); session.setProject( project ); - + session.setRemoteRepositories( getArtifactRepositories( project, request.getSettings() ) ); resolveParameters( request ); @@ -216,6 +216,7 @@ public class DefaultMaven catch ( Exception e ) { dispatcher.dispatchError( event, project.getId(), e ); + throw e; } // End event monitoring. @@ -298,9 +299,8 @@ public class DefaultMaven if ( proxy != null ) { - wagonManager.setProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), - proxy.getUsername(), proxy.getPassword(), - proxy.getNonProxyHosts() ); + wagonManager.setProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), + proxy.getPassword(), proxy.getNonProxyHosts() ); } } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java index b89121b9be..15bc4e6a76 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java @@ -34,7 +34,6 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.Logger; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import java.util.Date; import java.util.HashMap; @@ -49,7 +48,7 @@ import java.util.Map; */ public class DefaultLifecycleExecutor extends AbstractLogEnabled - implements LifecycleExecutor, Initializable + implements LifecycleExecutor { // ---------------------------------------------------------------------- // Components @@ -65,8 +64,6 @@ public class DefaultLifecycleExecutor private List phases; - private Map phaseMap; - // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- @@ -84,6 +81,16 @@ public class DefaultLifecycleExecutor response.setStart( new Date() ); + Map phaseMap = new HashMap(); + + for ( Iterator i = phases.iterator(); i.hasNext(); ) + { + Phase p = (Phase) i.next(); + + // Make a copy of the phase as we will modify it + phaseMap.put( p.getId(), new Phase( p ) ); + } + try { MavenProject project = session.getProject(); @@ -94,7 +101,7 @@ public class DefaultLifecycleExecutor { if ( artifactHandler.packageGoal() != null ) { - verifyMojoPhase( artifactHandler.packageGoal(), session ); + verifyMojoPhase( artifactHandler.packageGoal(), session, phaseMap ); } if ( artifactHandler.additionalPlugin() != null ) @@ -111,21 +118,21 @@ public class DefaultLifecycleExecutor plugin.setArtifactId( additionalPluginArtifactId ); - processPluginPhases( plugin, session ); + processPluginPhases( plugin, session, phaseMap ); } } - processPluginConfiguration( session.getProject(), session ); + processPluginConfiguration( session.getProject(), session, phaseMap ); for ( Iterator i = tasks.iterator(); i.hasNext(); ) { String task = (String) i.next(); - processGoalChain( task, session ); + processGoalChain( task, session, phaseMap ); if ( phaseMap.containsKey( task ) ) { - executePhase( task, session, response ); + executePhase( task, session, response, phaseMap ); } else { @@ -184,14 +191,14 @@ public class DefaultLifecycleExecutor } // TODO: don't throw Exception - private void processPluginConfiguration( MavenProject project, MavenSession mavenSession ) + private void processPluginConfiguration( MavenProject project, MavenSession mavenSession, Map phaseMap ) throws Exception { for ( Iterator i = project.getPlugins().iterator(); i.hasNext(); ) { Plugin plugin = (Plugin) i.next(); - processPluginPhases( plugin, mavenSession ); + processPluginPhases( plugin, mavenSession, phaseMap ); } } @@ -204,7 +211,7 @@ public class DefaultLifecycleExecutor * @throws Exception */ // TODO: don't throw Exception - private void processPluginPhases( Plugin plugin, MavenSession mavenSession ) + private void processPluginPhases( Plugin plugin, MavenSession mavenSession, Map phaseMap ) throws Exception { String groupId = plugin.getGroupId(); @@ -225,7 +232,7 @@ public class DefaultLifecycleExecutor { String pluginId = pluginDescriptor.getArtifactId(); - // Right now this maven-foo-plugin so this is a hack right now. + // TODO: Right now this maven-foo-plugin so this is a hack right now. pluginId = pluginId.substring( 6 ); @@ -245,7 +252,7 @@ public class DefaultLifecycleExecutor "A goal '" + mojoId + "' was declared in pom.xml, but does not exist" ); } - configureMojo( mojoDescriptor ); + configureMojo( mojoDescriptor, phaseMap ); } } else @@ -254,7 +261,7 @@ public class DefaultLifecycleExecutor { MojoDescriptor mojoDescriptor = (MojoDescriptor) j.next(); - configureMojo( mojoDescriptor ); + configureMojo( mojoDescriptor, phaseMap ); } } } @@ -267,7 +274,7 @@ public class DefaultLifecycleExecutor * @param mojoDescriptor * @throws Exception */ - private void configureMojo( MojoDescriptor mojoDescriptor ) + private void configureMojo( MojoDescriptor mojoDescriptor, Map phaseMap ) throws Exception { if ( mojoDescriptor.getPhase() != null ) @@ -278,7 +285,7 @@ public class DefaultLifecycleExecutor } } - private void processGoalChain( String task, MavenSession session ) + private void processGoalChain( String task, MavenSession session, Map phaseMap ) throws Exception { if ( phaseMap.containsKey( task ) ) @@ -288,26 +295,29 @@ public class DefaultLifecycleExecutor for ( int j = 0; j <= index; j++ ) { + // TODO: phases should just be strings... Phase p = (Phase) phases.get( j ); + p = (Phase) phaseMap.get( p.getId() ); + if ( p.getGoals() != null ) { for ( Iterator k = p.getGoals().iterator(); k.hasNext(); ) { String goal = (String) k.next(); - verifyMojoPhase( goal, session ); + verifyMojoPhase( goal, session, phaseMap ); } } } } else { - verifyMojoPhase( task, session ); + verifyMojoPhase( task, session, phaseMap ); } } - private void verifyMojoPhase( String task, MavenSession session ) + private void verifyMojoPhase( String task, MavenSession session, Map phaseMap ) throws Exception { MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor( task ); @@ -332,7 +342,7 @@ public class DefaultLifecycleExecutor } } - private void executePhase( String phase, MavenSession session, MavenExecutionResponse response ) + private void executePhase( String phase, MavenSession session, MavenExecutionResponse response, Map phaseMap ) throws LifecycleExecutionException { // only execute up to the given phase @@ -344,6 +354,8 @@ public class DefaultLifecycleExecutor { Phase p = (Phase) phases.get( j ); + p = (Phase) phaseMap.get( p.getId() ); + String event = MavenEvents.PHASE_EXECUTION; // !! This is ripe for refactoring to an aspect. @@ -395,7 +407,11 @@ public class DefaultLifecycleExecutor try { Logger logger = getLogger(); - logger.debug( "Resolving artifacts from:\n" + "\t{localRepository: " + session.getLocalRepository() + "}\n" + "\t{remoteRepositories: " + session.getRemoteRepositories() + "}" ); + logger.debug( + "Resolving artifacts from:\n" + "\t{localRepository: " + session.getLocalRepository() + "}\n" + + "\t{remoteRepositories: " + + session.getRemoteRepositories() + + "}" ); return pluginManager.executeMojo( session, id ); } @@ -413,26 +429,4 @@ public class DefaultLifecycleExecutor { return phases; } - - public Phase getPhase( String id ) - { - return (Phase) phaseMap.get( id ); - } - - // ---------------------------------------------------------------------- - // Lifecylce Management - // ---------------------------------------------------------------------- - - public void initialize() - throws Exception - { - phaseMap = new HashMap(); - - for ( Iterator i = phases.iterator(); i.hasNext(); ) - { - Phase p = (Phase) i.next(); - - phaseMap.put( p.getId(), p ); - } - } } \ No newline at end of file diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java index 9815c0d76d..d600a694de 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/LifecycleExecutor.java @@ -34,6 +34,4 @@ public interface LifecycleExecutor throws LifecycleExecutionException; List getPhases(); - - Phase getPhase( String id ); } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java index 2aa6db4bc0..c937ee4565 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/Phase.java @@ -30,6 +30,19 @@ public class Phase private Set goals; + public Phase() + { + } + + Phase( Phase phase ) + { + this.id = phase.id; + if ( phase.goals != null ) + { + this.goals = new HashSet( phase.goals ); + } + } + public String getId() { return id; @@ -43,4 +56,26 @@ public class Phase } return goals; } + + public boolean equals( Object obj ) + { + if ( obj == this ) + { + return true; + } + + if ( !( obj instanceof Phase ) ) + { + return false; + } + + Phase p = (Phase) obj; + + return id.equals( p.id ); + } + + public int hashCode() + { + return id.hashCode(); + } }