From 6dc7f104e29dc3add7303fc909743daaf431c835 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Wed, 29 Jul 2009 21:19:03 +0000 Subject: [PATCH] o Restored forking to other mojo git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@799083 13f79535-47bb-0310-9956-ffa450edef68 --- .../lifecycle/DefaultLifecycleExecutor.java | 107 ++++++++++++++++-- .../apache/maven/plugin/MojoExecution.java | 23 +++- .../apache/maven/project/MavenProject.java | 15 ++- 3 files changed, 131 insertions(+), 14 deletions(-) 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 38a9d5e789..d413c3d94f 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 @@ -44,7 +44,10 @@ import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.CycleDetectedInPluginGraphException; import org.apache.maven.plugin.InvalidPluginDescriptorException; import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoNotFoundException; +import org.apache.maven.plugin.PluginConfigurationException; import org.apache.maven.plugin.PluginDescriptorParsingException; import org.apache.maven.plugin.PluginManager; import org.apache.maven.plugin.PluginManagerException; @@ -192,9 +195,8 @@ public class DefaultLifecycleExecutor for ( MojoExecution mojoExecution : executionPlan.getExecutions() ) { - logger.info( executionDescription( mojoExecution, currentProject ) ); - pluginManager.executeMojo( session, mojoExecution ); - } + execute( currentProject, session, mojoExecution ); + } } catch ( Exception e ) @@ -229,7 +231,39 @@ public class DefaultLifecycleExecutor } } } - + + private void execute( MavenProject project, MavenSession session, MojoExecution mojoExecution ) + throws MojoFailureException, MojoExecutionException, PluginConfigurationException, PluginManagerException + { + MavenProject executionProject = null; + + List forkedExecutions = mojoExecution.getForkedExecutions(); + + if ( !forkedExecutions.isEmpty() ) + { + executionProject = project.clone(); + + session.setCurrentProject( executionProject ); + try + { + for ( MojoExecution forkedExecution : forkedExecutions ) + { + execute( executionProject, session, forkedExecution ); + } + } + finally + { + session.setCurrentProject( project ); + } + } + + project.setExecutionProject( executionProject ); + + logger.info( executionDescription( mojoExecution, project ) ); + + pluginManager.executeMojo( session, mojoExecution ); + } + public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks ) throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, CycleDetectedInPluginGraphException, MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException, PluginManagerException { @@ -268,19 +302,34 @@ public class DefaultLifecycleExecutor pluginDescriptor.setClassRealm( pluginManager.getPluginRealm( session, pluginDescriptor ) ); } - if ( StringUtils.isNotEmpty( mojoDescriptor.isDependencyResolutionRequired() ) ) - { - requiredDependencyResolutionScopes.add( mojoDescriptor.isDependencyResolutionRequired() ); - } - mojoExecution.setMojoDescriptor( mojoDescriptor ); populateMojoExecutionConfiguration( project, mojoExecution, false ); + + calculateForkedExecutions( mojoExecution, project, new HashSet() ); + + collectDependencyResolutionScopes( requiredDependencyResolutionScopes, mojoExecution ); } return new MavenExecutionPlan( lifecyclePlan, requiredDependencyResolutionScopes ); } - + + private void collectDependencyResolutionScopes( Collection requiredDependencyResolutionScopes, + MojoExecution mojoExecution ) + { + String requiredDependencyResolutionScope = mojoExecution.getMojoDescriptor().isDependencyResolutionRequired(); + + if ( StringUtils.isNotEmpty( requiredDependencyResolutionScope ) ) + { + requiredDependencyResolutionScopes.add( requiredDependencyResolutionScope ); + } + + for ( MojoExecution forkedExecution : mojoExecution.getForkedExecutions() ) + { + collectDependencyResolutionScopes( requiredDependencyResolutionScopes, forkedExecution ); + } + } + private void calculateExecutionForIndividualGoal( MavenSession session, List lifecyclePlan, String goal ) throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException, CycleDetectedInPluginGraphException, MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException { @@ -452,6 +501,44 @@ public class DefaultLifecycleExecutor } } + private void calculateForkedExecutions( MojoExecution mojoExecution, MavenProject project, + Collection alreadyForkedExecutions ) + throws MojoNotFoundException + { + MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); + + if ( !alreadyForkedExecutions.add( mojoDescriptor ) ) + { + return; + } + + PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); + + if ( StringUtils.isNotEmpty( mojoDescriptor.getExecutePhase() ) ) + { + // TODO + + } + else if ( StringUtils.isNotEmpty( mojoDescriptor.getExecuteGoal() ) ) + { + String forkedGoal = mojoDescriptor.getExecuteGoal(); + + MojoDescriptor forkedMojoDescriptor = pluginDescriptor.getMojo( forkedGoal ); + if ( forkedMojoDescriptor == null ) + { + throw new MojoNotFoundException( forkedGoal, pluginDescriptor ); + } + + MojoExecution forkedExecution = new MojoExecution( forkedMojoDescriptor, forkedGoal ); + + populateMojoExecutionConfiguration( project, forkedExecution, true ); + + calculateForkedExecutions( forkedExecution, project, alreadyForkedExecutions ); + + mojoExecution.addForkedExecution( forkedExecution ); + } + } + private String executionDescription( MojoExecution me, MavenProject project ) { PluginDescriptor pd = me.getMojoDescriptor().getPluginDescriptor(); diff --git a/maven-core/src/main/java/org/apache/maven/plugin/MojoExecution.java b/maven-core/src/main/java/org/apache/maven/plugin/MojoExecution.java index 0bbe17f46a..d202f3d87e 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/MojoExecution.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/MojoExecution.java @@ -19,6 +19,9 @@ package org.apache.maven.plugin; * under the License. */ +import java.util.ArrayList; +import java.util.List; + import org.apache.maven.model.Plugin; import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.codehaus.plexus.util.xml.Xpp3Dom; @@ -41,7 +44,9 @@ public class MojoExecution * this mojo execution is going to run in. */ private String lifecyclePhase; - + + private List forkedExecutions = new ArrayList(); + public MojoExecution( Plugin plugin, String goal, String executionId ) { this.plugin = plugin; @@ -176,4 +181,20 @@ public class MojoExecution { this.mojoDescriptor = mojoDescriptor; } + + public List getForkedExecutions() + { + return forkedExecutions; + } + + public void addForkedExecution( MojoExecution forkedExecution ) + { + if ( forkedExecution == null ) + { + throw new IllegalArgumentException( "forked execution missing" ); + } + + forkedExecutions.add( forkedExecution ); + } + } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 3fe6648d06..df86ad90aa 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -1713,11 +1713,20 @@ public class MavenProject * @since 2.0.9 */ @Override - public Object clone() - throws CloneNotSupportedException + public MavenProject clone() { - MavenProject clone = (MavenProject) super.clone(); + MavenProject clone; + try + { + clone = (MavenProject) super.clone(); + } + catch ( CloneNotSupportedException e ) + { + throw new UnsupportedOperationException( e ); + } + clone.deepCopy( this ); + return clone; }