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 3fd3ef4fcd..fc77721f96 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -729,6 +729,7 @@ public class DefaultMaven List activeProjects = projectSorter.getSortedProjects(); activeProjects = trimSelectedProjects( activeProjects, projectDependencyGraph, request ); + activeProjects = trimExcludedProjects( activeProjects, request ); activeProjects = trimResumedProjects( activeProjects, request ); if ( activeProjects.size() != projectSorter.getSortedProjects().size() ) @@ -848,6 +849,59 @@ public class DefaultMaven return result; } + + private List trimExcludedProjects( List projects, MavenExecutionRequest request ) + throws MavenExecutionException + { + List result = projects; + + if ( !request.getExcludedProjects().isEmpty() ) + { + File reactorDirectory = null; + + if ( request.getBaseDirectory() != null ) + { + reactorDirectory = new File( request.getBaseDirectory() ); + } + + Collection excludedProjects = new LinkedHashSet( 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( projects.size() ); + for ( MavenProject project : projects ) + { + if ( !excludedProjects.contains( project ) ) + { + result.add( project ); + } + } + } + + return result; + } private List trimResumedProjects( List projects, MavenExecutionRequest request ) throws MavenExecutionException diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java index ef769d9f46..9919a1e2be 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java @@ -101,6 +101,8 @@ public class DefaultMavenExecutionRequest private String reactorFailureBehavior = REACTOR_FAIL_FAST; private List selectedProjects; + + private List excludedProjects; private String resumeFrom; @@ -258,6 +260,16 @@ public class DefaultMavenExecutionRequest return selectedProjects; } + + public List getExcludedProjects() + { + if ( excludedProjects == null ) + { + excludedProjects = new ArrayList(); + } + + return excludedProjects; + } public String getResumeFrom() { @@ -514,6 +526,20 @@ public class DefaultMavenExecutionRequest return this; } + public MavenExecutionRequest setExcludedProjects( List excludedProjects ) + { + if ( excludedProjects != null ) + { + this.excludedProjects = new ArrayList( excludedProjects ); + } + else + { + this.excludedProjects = null; + } + + return this; + } + public MavenExecutionRequest setResumeFrom( String project ) { this.resumeFrom = project; diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java index a3160fe097..70b77e1481 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java @@ -145,6 +145,20 @@ public interface MavenExecutionRequest MavenExecutionRequest setSelectedProjects( List projects ); List getSelectedProjects(); + + /** + * + * @param projects the projects to exclude + * @return this MavenExecutionRequest + * @since 3.2 + */ + MavenExecutionRequest setExcludedProjects( List projects ); + /** + * + * @return the excluded projects, never {@code null} + * @since 3.2 + */ + List getExcludedProjects(); MavenExecutionRequest setResumeFrom( String project ); String getResumeFrom(); diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 37a5884c40..c6d705c330 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -24,7 +24,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.ArrayList; -import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -976,14 +975,39 @@ public class MavenCli if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) ) { - String[] values = commandLine.getOptionValues( CLIManager.PROJECT_LIST ); - List projects = new ArrayList(); - for ( String value : values ) + String[] projectOptionValues = commandLine.getOptionValues( CLIManager.PROJECT_LIST ); + + List inclProjects = new ArrayList(); + List exclProjects = new ArrayList(); + + if ( projectOptionValues != null ) { - String[] tmp = StringUtils.split( value, "," ); - projects.addAll( Arrays.asList( tmp ) ); + for ( String projectOptionValue : projectOptionValues ) + { + 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 )