[MNG-5230] Command line option to exclude modules from reactor

Contributed by Luuk van den Broek, slightly changed by Robert Scholte
Exclude/include marker char now match profiles, i.e +, ! and -
This commit is contained in:
Robert Scholte 2014-01-09 00:05:02 +01:00
parent 0f48aabf52
commit 91499839cf
4 changed files with 125 additions and 7 deletions

View File

@ -729,6 +729,7 @@ public class DefaultMaven
List<MavenProject> activeProjects = projectSorter.getSortedProjects(); List<MavenProject> activeProjects = projectSorter.getSortedProjects();
activeProjects = trimSelectedProjects( activeProjects, projectDependencyGraph, request ); activeProjects = trimSelectedProjects( activeProjects, projectDependencyGraph, request );
activeProjects = trimExcludedProjects( activeProjects, request );
activeProjects = trimResumedProjects( activeProjects, request ); activeProjects = trimResumedProjects( activeProjects, request );
if ( activeProjects.size() != projectSorter.getSortedProjects().size() ) if ( activeProjects.size() != projectSorter.getSortedProjects().size() )
@ -848,6 +849,59 @@ public class DefaultMaven
return result; return result;
} }
private List<MavenProject> trimExcludedProjects( List<MavenProject> projects, MavenExecutionRequest request )
throws MavenExecutionException
{
List<MavenProject> result = projects;
if ( !request.getExcludedProjects().isEmpty() )
{
File reactorDirectory = null;
if ( request.getBaseDirectory() != null )
{
reactorDirectory = new File( request.getBaseDirectory() );
}
Collection<MavenProject> excludedProjects = new LinkedHashSet<MavenProject>( projects.size() );
for ( String selector : request.getExcludedProjects() )
{
MavenProject excludedProject = null;
for ( MavenProject project : projects )
{
if ( isMatchingProject( project, selector, reactorDirectory ) )
{
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<MavenProject>( projects.size() );
for ( MavenProject project : projects )
{
if ( !excludedProjects.contains( project ) )
{
result.add( project );
}
}
}
return result;
}
private List<MavenProject> trimResumedProjects( List<MavenProject> projects, MavenExecutionRequest request ) private List<MavenProject> trimResumedProjects( List<MavenProject> projects, MavenExecutionRequest request )
throws MavenExecutionException throws MavenExecutionException

View File

@ -101,6 +101,8 @@ public class DefaultMavenExecutionRequest
private String reactorFailureBehavior = REACTOR_FAIL_FAST; private String reactorFailureBehavior = REACTOR_FAIL_FAST;
private List<String> selectedProjects; private List<String> selectedProjects;
private List<String> excludedProjects;
private String resumeFrom; private String resumeFrom;
@ -258,6 +260,16 @@ public class DefaultMavenExecutionRequest
return selectedProjects; return selectedProjects;
} }
public List<String> getExcludedProjects()
{
if ( excludedProjects == null )
{
excludedProjects = new ArrayList<String>();
}
return excludedProjects;
}
public String getResumeFrom() public String getResumeFrom()
{ {
@ -514,6 +526,20 @@ public class DefaultMavenExecutionRequest
return this; return this;
} }
public MavenExecutionRequest setExcludedProjects( List<String> excludedProjects )
{
if ( excludedProjects != null )
{
this.excludedProjects = new ArrayList<String>( excludedProjects );
}
else
{
this.excludedProjects = null;
}
return this;
}
public MavenExecutionRequest setResumeFrom( String project ) public MavenExecutionRequest setResumeFrom( String project )
{ {
this.resumeFrom = project; this.resumeFrom = project;

View File

@ -145,6 +145,20 @@ public interface MavenExecutionRequest
MavenExecutionRequest setSelectedProjects( List<String> projects ); MavenExecutionRequest setSelectedProjects( List<String> projects );
List<String> getSelectedProjects(); List<String> getSelectedProjects();
/**
*
* @param projects the projects to exclude
* @return this MavenExecutionRequest
* @since 3.2
*/
MavenExecutionRequest setExcludedProjects( List<String> projects );
/**
*
* @return the excluded projects, never {@code null}
* @since 3.2
*/
List<String> getExcludedProjects();
MavenExecutionRequest setResumeFrom( String project ); MavenExecutionRequest setResumeFrom( String project );
String getResumeFrom(); String getResumeFrom();

View File

@ -24,7 +24,6 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -976,14 +975,39 @@ public class MavenCli
if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) ) if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) )
{ {
String[] values = commandLine.getOptionValues( CLIManager.PROJECT_LIST ); String[] projectOptionValues = commandLine.getOptionValues( CLIManager.PROJECT_LIST );
List<String> projects = new ArrayList<String>();
for ( String value : values ) List<String> inclProjects = new ArrayList<String>();
List<String> exclProjects = new ArrayList<String>();
if ( projectOptionValues != null )
{ {
String[] tmp = StringUtils.split( value, "," ); for ( String projectOptionValue : projectOptionValues )
projects.addAll( Arrays.asList( tmp ) ); {
StringTokenizer projectTokens = new StringTokenizer( projectOptionValue, "," );
while ( projectTokens.hasMoreTokens() )
{
String projectAction = projectTokens.nextToken().trim();
if ( projectAction.startsWith( "-" ) || projectAction.startsWith( "!" ) )
{
exclProjects.add( projectAction.substring( 1 ) );
}
else if ( projectAction.startsWith( "+" ) )
{
inclProjects.add( projectAction.substring( 1 ) );
}
else
{
inclProjects.add( projectAction );
}
}
}
} }
request.setSelectedProjects( projects );
request.setSelectedProjects( inclProjects );
request.setExcludedProjects( exclProjects );
} }
if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) if ( commandLine.hasOption( CLIManager.ALSO_MAKE )