[MNG-6566] Plugins that require a certain phase should not fork goals that are already in the execution plan.

This closes #397
This commit is contained in:
Martin Kanters 2020-10-27 16:43:24 +01:00
parent be8ced6be3
commit d8c2cc7e4a
4 changed files with 64 additions and 31 deletions

View File

@ -27,6 +27,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
@ -153,37 +154,65 @@ public class DefaultLifecycleExecutionPlanCalculator
MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
{
Set<MojoDescriptor> alreadyPlannedExecutions = fillMojoDescriptors( session, project, mojoExecutions );
for ( MojoExecution mojoExecution : mojoExecutions )
{
setupMojoExecution( session, project, mojoExecution );
setupMojoExecution( session, project, mojoExecution, alreadyPlannedExecutions );
}
}
@Override
public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
private Set<MojoDescriptor> fillMojoDescriptors( MavenSession session, MavenProject project,
List<MojoExecution> mojoExecutions )
throws InvalidPluginDescriptorException, MojoNotFoundException, PluginResolutionException,
PluginDescriptorParsingException, PluginNotFoundException
{
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
Set<MojoDescriptor> descriptors = new HashSet<>( mojoExecutions.size() );
for ( MojoExecution execution : mojoExecutions )
{
MojoDescriptor mojoDescriptor = fillMojoDescriptor( session, project, execution );
descriptors.add( mojoDescriptor );
}
return descriptors;
}
private MojoDescriptor fillMojoDescriptor( MavenSession session, MavenProject project, MojoExecution execution )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, InvalidPluginDescriptorException
{
MojoDescriptor mojoDescriptor = execution.getMojoDescriptor();
if ( mojoDescriptor == null )
{
mojoDescriptor =
pluginManager.getMojoDescriptor( mojoExecution.getPlugin(), mojoExecution.getGoal(),
project.getRemotePluginRepositories(),
session.getRepositorySession() );
pluginManager.getMojoDescriptor( execution.getPlugin(), execution.getGoal(),
project.getRemotePluginRepositories(),
session.getRepositorySession() );
mojoExecution.setMojoDescriptor( mojoDescriptor );
execution.setMojoDescriptor( mojoDescriptor );
}
return mojoDescriptor;
}
@Override
public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution,
Set<MojoDescriptor> alreadyPlannedExecutions )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
{
fillMojoDescriptor( session, project, mojoExecution );
mojoExecutionConfigurator( mojoExecution ).configure( project,
mojoExecution,
MojoExecution.Source.CLI.equals( mojoExecution.getSource() ) );
finalizeMojoConfiguration( mojoExecution );
calculateForkedExecutions( mojoExecution, session, project, new HashSet<>() );
calculateForkedExecutions( mojoExecution, session, project, alreadyPlannedExecutions );
}
public List<MojoExecution> calculateMojoExecutions( MavenSession session, MavenProject project, List<Object> tasks )
@ -339,7 +368,7 @@ public class DefaultLifecycleExecutionPlanCalculator
}
private void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session, MavenProject project,
Collection<MojoDescriptor> alreadyForkedExecutions )
Collection<MojoDescriptor> alreadyPlannedExecutions )
throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
@ -351,10 +380,7 @@ public class DefaultLifecycleExecutionPlanCalculator
return;
}
if ( !alreadyForkedExecutions.add( mojoDescriptor ) )
{
return;
}
alreadyPlannedExecutions.add( mojoDescriptor );
List<MavenProject> forkedProjects =
LifecycleDependencyResolver.getProjects( project, session, mojoDescriptor.isAggregator() );
@ -371,23 +397,25 @@ public class DefaultLifecycleExecutionPlanCalculator
if ( StringUtils.isNotEmpty( mojoDescriptor.getExecutePhase() ) )
{
forkedExecutions =
calculateForkedLifecycle( mojoExecution, session, forkedProject, alreadyForkedExecutions );
calculateForkedLifecycle( mojoExecution, session, forkedProject, alreadyPlannedExecutions );
}
else
{
forkedExecutions = calculateForkedGoal( mojoExecution, session, forkedProject,
alreadyForkedExecutions );
alreadyPlannedExecutions );
}
mojoExecution.setForkedExecutions( BuilderCommon.getKey( forkedProject ), forkedExecutions );
// This List can be empty when the executions are already present in the plan
if ( !forkedExecutions.isEmpty() )
{
mojoExecution.setForkedExecutions( BuilderCommon.getKey( forkedProject ), forkedExecutions );
}
}
alreadyForkedExecutions.remove( mojoDescriptor );
}
private List<MojoExecution> calculateForkedLifecycle( MojoExecution mojoExecution, MavenSession session,
MavenProject project,
Collection<MojoDescriptor> alreadyForkedExecutions )
Collection<MojoDescriptor> alreadyPlannedExecutions )
throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
@ -425,11 +453,11 @@ public class DefaultLifecycleExecutionPlanCalculator
{
for ( MojoExecution forkedExecution : forkedExecutions )
{
if ( !alreadyForkedExecutions.contains( forkedExecution.getMojoDescriptor() ) )
if ( !alreadyPlannedExecutions.contains( forkedExecution.getMojoDescriptor() ) )
{
finalizeMojoConfiguration( forkedExecution );
calculateForkedExecutions( forkedExecution, session, project, alreadyForkedExecutions );
calculateForkedExecutions( forkedExecution, session, project, alreadyPlannedExecutions );
mojoExecutions.add( forkedExecution );
}
@ -534,7 +562,7 @@ public class DefaultLifecycleExecutionPlanCalculator
private List<MojoExecution> calculateForkedGoal( MojoExecution mojoExecution, MavenSession session,
MavenProject project,
Collection<MojoDescriptor> alreadyForkedExecutions )
Collection<MojoDescriptor> alreadyPlannedExecutions )
throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
@ -551,7 +579,7 @@ public class DefaultLifecycleExecutionPlanCalculator
throw new MojoNotFoundException( forkedGoal, pluginDescriptor );
}
if ( alreadyForkedExecutions.contains( forkedMojoDescriptor ) )
if ( alreadyPlannedExecutions.contains( forkedMojoDescriptor ) )
{
return Collections.emptyList();
}
@ -562,7 +590,7 @@ public class DefaultLifecycleExecutionPlanCalculator
finalizeMojoConfiguration( forkedExecution );
calculateForkedExecutions( forkedExecution, session, project, alreadyForkedExecutions );
calculateForkedExecutions( forkedExecution, session, project, alreadyPlannedExecutions );
return Collections.singletonList( forkedExecution );
}

View File

@ -29,11 +29,13 @@ import org.apache.maven.plugin.MojoNotFoundException;
import org.apache.maven.plugin.PluginDescriptorParsingException;
import org.apache.maven.plugin.PluginNotFoundException;
import org.apache.maven.plugin.PluginResolutionException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.project.MavenProject;
import java.util.List;
import java.util.Set;
/**
* @since 3.0
@ -58,7 +60,8 @@ public interface LifecycleExecutionPlanCalculator
PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException;
void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution,
Set<MojoDescriptor> alreadyPlannedExecutions )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException;

View File

@ -22,6 +22,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.apache.maven.AbstractCoreMavenComponentTestCase;
@ -386,7 +387,8 @@ public class LifecycleExecutorTest
assertEquals(execution.toString(), "maven-it-plugin", execution.getArtifactId());
assertNull(execution.getConfiguration());
lifeCycleExecutionPlanCalculator.setupMojoExecution( session, session.getCurrentProject(), execution );
lifeCycleExecutionPlanCalculator.setupMojoExecution( session, session.getCurrentProject(), execution,
new HashSet<>() );
assertNotNull(execution.getConfiguration());
assertEquals("1.0", execution.getConfiguration().getChild( "version" ).getAttribute( "default-value" ));
}

View File

@ -158,7 +158,7 @@ public class LifecycleExecutionPlanCalculatorStub
return calculateExecutionPlan( session, project, tasks, true );
}
public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution, Set<MojoDescriptor> alreadyForkedExecutions )
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException