[MNG-6863] Support --also-make flag in combination with --resume-from.

Author: Martin Kanters <Martin.Kanters@infosupport.com>
This commit is contained in:
rfscholte 2020-06-20 22:39:29 +02:00
parent 1e5bf55e12
commit 1e217703a8
2 changed files with 69 additions and 125 deletions

View File

@ -25,10 +25,12 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -59,6 +61,8 @@ import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.dag.CycleDetectedException; import org.codehaus.plexus.util.dag.CycleDetectedException;
import static java.util.Comparator.comparing;
/** /**
* Builds the {@link ProjectDependencyGraph inter-dependencies graph} between projects in the reactor. * Builds the {@link ProjectDependencyGraph inter-dependencies graph} between projects in the reactor.
*/ */
@ -149,43 +153,27 @@ public class DefaultGraphBuilder
if ( !request.getSelectedProjects().isEmpty() ) if ( !request.getSelectedProjects().isEmpty() )
{ {
result = new ArrayList<>( projects.size() ); File reactorDirectory = getReactorDirectory( request );
File reactorDirectory = null; Collection<MavenProject> selectedProjects = new LinkedHashSet<>( request.getSelectedProjects().size(), 1 );
if ( request.getBaseDirectory() != null )
{
reactorDirectory = new File( request.getBaseDirectory() );
}
Collection<MavenProject> selectedProjects = new LinkedHashSet<>( projects.size() );
for ( String selector : request.getSelectedProjects() ) for ( String selector : request.getSelectedProjects() )
{ {
MavenProject selectedProject = null; MavenProject selectedProject = projects.stream()
.filter( project -> isMatchingProject( project, selector, reactorDirectory ) )
for ( MavenProject project : projects ) .findFirst()
{ .orElseThrow( () -> new MavenExecutionException(
if ( isMatchingProject( project, selector, reactorDirectory ) ) "Could not find the selected project in the reactor: " + selector, request.getPom() ) );
{
selectedProject = project;
break;
}
}
if ( selectedProject != null )
{
selectedProjects.add( selectedProject ); selectedProjects.add( selectedProject );
} }
else
{
throw new MavenExecutionException( "Could not find the selected project in the reactor: "
+ selector, request.getPom() );
}
}
result.addAll( selectedProjects ); result = new ArrayList<>( selectedProjects );
result = includeAlsoMakeTransitively( result, request, graph ); result = includeAlsoMakeTransitively( result, request, graph );
// Order the new list in the original order
List<MavenProject> sortedProjects = graph.getSortedProjects();
result.sort( comparing( sortedProjects::indexOf ) );
} }
return result; return result;
@ -199,38 +187,20 @@ public class DefaultGraphBuilder
if ( StringUtils.isNotEmpty( request.getResumeFrom() ) ) if ( StringUtils.isNotEmpty( request.getResumeFrom() ) )
{ {
File reactorDirectory = null; File reactorDirectory = getReactorDirectory( request );
if ( request.getBaseDirectory() != null )
{
reactorDirectory = new File( request.getBaseDirectory() );
}
String selector = request.getResumeFrom(); String selector = request.getResumeFrom();
result = new ArrayList<>( projects.size() ); MavenProject resumingFromProject = projects.stream()
.filter( project -> isMatchingProject( project, selector, reactorDirectory ) )
.findFirst()
.orElseThrow( () -> new MavenExecutionException(
"Could not find project to resume reactor build from: " + selector + " vs "
+ formatProjects( projects ), request.getPom() ) );
int resumeFromProjectIndex = projects.indexOf( resumingFromProject );
List<MavenProject> retainingProjects = result.subList( resumeFromProjectIndex, projects.size() );
boolean resumed = false; result = includeAlsoMakeTransitively( retainingProjects, request, graph );
for ( MavenProject project : projects )
{
if ( !resumed && isMatchingProject( project, selector, reactorDirectory ) )
{
resumed = true;
}
if ( resumed )
{
result.add( project );
}
}
if ( !resumed )
{
throw new MavenExecutionException( "Could not find project to resume reactor build from: " + selector
+ " vs " + formatProjects( projects ), request.getPom() );
}
result = includeAlsoMakeTransitively( result, request, graph );
} }
return result; return result;
@ -243,46 +213,18 @@ public class DefaultGraphBuilder
if ( !request.getExcludedProjects().isEmpty() ) if ( !request.getExcludedProjects().isEmpty() )
{ {
File reactorDirectory = null; File reactorDirectory = getReactorDirectory( request );
if ( request.getBaseDirectory() != null ) result = new ArrayList<>( projects );
{
reactorDirectory = new File( request.getBaseDirectory() );
}
Collection<MavenProject> excludedProjects = new LinkedHashSet<>( projects.size() );
for ( String selector : request.getExcludedProjects() ) for ( String selector : request.getExcludedProjects() )
{ {
MavenProject excludedProject = null; MavenProject excludedProject = projects.stream()
.filter( project -> isMatchingProject( project, selector, reactorDirectory ) )
for ( MavenProject project : projects ) .findFirst()
{ .orElseThrow( () -> new MavenExecutionException( "Could not find the selected project in "
if ( isMatchingProject( project, selector, reactorDirectory ) ) + "the reactor: " + selector, request.getPom() ) );
{ result.remove( excludedProject );
excludedProject = project;
break;
}
}
if ( excludedProject != null )
{
excludedProjects.add( excludedProject );
}
else
{
throw new MavenExecutionException( "Could not find the selected project in the reactor: "
+ selector, request.getPom() );
}
}
result = new ArrayList<>( projects.size() );
for ( MavenProject project : projects )
{
if ( !excludedProjects.contains( project ) )
{
result.add( project );
}
} }
} }
@ -293,54 +235,41 @@ public class DefaultGraphBuilder
ProjectDependencyGraph graph ) ProjectDependencyGraph graph )
throws MavenExecutionException throws MavenExecutionException
{ {
List<MavenProject> result; List<MavenProject> result = projects;
boolean makeUpstream = false; String makeBehavior = request.getMakeBehavior();
boolean makeDownstream = false; boolean makeBoth = MavenExecutionRequest.REACTOR_MAKE_BOTH.equals( makeBehavior );
if ( MavenExecutionRequest.REACTOR_MAKE_UPSTREAM.equals( request.getMakeBehavior() ) ) boolean makeUpstream = makeBoth || MavenExecutionRequest.REACTOR_MAKE_UPSTREAM.equals( makeBehavior );
boolean makeDownstream = makeBoth || MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM.equals( makeBehavior );
if ( StringUtils.isNotEmpty( makeBehavior ) && !makeUpstream && !makeDownstream )
{ {
makeUpstream = true; throw new MavenExecutionException( "Invalid reactor make behavior: " + makeBehavior,
}
else if ( MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM.equals( request.getMakeBehavior() ) )
{
makeDownstream = true;
}
else if ( MavenExecutionRequest.REACTOR_MAKE_BOTH.equals( request.getMakeBehavior() ) )
{
makeUpstream = true;
makeDownstream = true;
}
else if ( StringUtils.isNotEmpty( request.getMakeBehavior() ) )
{
throw new MavenExecutionException( "Invalid reactor make behavior: " + request.getMakeBehavior(),
request.getPom() ); request.getPom() );
} }
if ( makeUpstream || makeDownstream ) if ( makeUpstream || makeDownstream )
{ {
Set<MavenProject> projectsSet = new HashSet<>( projects );
for ( MavenProject project : new ArrayList<>( projects ) ) for ( MavenProject project : projects )
{ {
if ( makeUpstream ) if ( makeUpstream )
{ {
projects.addAll( graph.getUpstreamProjects( project, true ) ); projectsSet.addAll( graph.getUpstreamProjects( project, true ) );
} }
if ( makeDownstream ) if ( makeDownstream )
{ {
projects.addAll( graph.getDownstreamProjects( project, true ) ); projectsSet.addAll( graph.getDownstreamProjects( project, true ) );
}
} }
} }
result = new ArrayList<>( projects.size() ); result = new ArrayList<>( projectsSet );
for ( MavenProject project : graph.getSortedProjects() ) // Order the new list in the original order
{ List<MavenProject> sortedProjects = graph.getSortedProjects();
if ( projects.contains( project ) ) result.sort( comparing( sortedProjects::indexOf ) );
{
result.add( project );
}
} }
return result; return result;
@ -412,6 +341,16 @@ public class DefaultGraphBuilder
return false; return false;
} }
private File getReactorDirectory( MavenExecutionRequest request )
{
if ( request.getBaseDirectory() != null )
{
return new File( request.getBaseDirectory() );
}
return null;
}
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Project collection // Project collection

View File

@ -140,9 +140,14 @@ public class DefaultGraphBuilderTest
scenario( "Exclude the project we are resuming from (as proposed in MNG-6676)" ) scenario( "Exclude the project we are resuming from (as proposed in MNG-6676)" )
.resumeFrom( MODULE_B ) .resumeFrom( MODULE_B )
.excludedProjects( singletonList( MODULE_B ) ) .excludedProjects( singletonList( MODULE_B ) )
.expectResult( singletonList( MODULE_C ) ) .expectResult( singletonList( MODULE_C ) ),
scenario( "Selected projects in wrong order are resumed correctly in order" )
.selectedProjects( asList( MODULE_C, MODULE_B, MODULE_A ) )
.resumeFrom( MODULE_B )
.expectResult( asList( MODULE_B, MODULE_C ) ),
scenario( "Duplicate projects are filtered out" )
.selectedProjects( asList( MODULE_A, MODULE_A ) )
.expectResult( singletonList( MODULE_A ) )
); );
} }