diff --git a/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java b/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java index de4ea731d1..1f422937b1 100644 --- a/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java +++ b/maven-core/src/main/java/org/apache/maven/graph/DefaultProjectDependencyGraph.java @@ -43,7 +43,7 @@ import org.codehaus.plexus.util.dag.CycleDetectedException; * @author Benjamin Bentmann */ public class DefaultProjectDependencyGraph - implements ProjectDependencyGraph + implements ProjectDependencyGraph { private final ProjectSorter sorter; @@ -62,38 +62,24 @@ public class DefaultProjectDependencyGraph * @throws CycleDetectedException */ public DefaultProjectDependencyGraph( Collection projects ) - throws CycleDetectedException, DuplicateProjectException + throws CycleDetectedException, DuplicateProjectException { - super(); - this.allProjects = Collections.unmodifiableList( new ArrayList<>( projects ) ); - this.sorter = new ProjectSorter( projects ); - this.order = new HashMap<>(); - this.projects = new HashMap<>(); - List sorted = this.sorter.getSortedProjects(); - for ( int index = 0; index < sorted.size(); index++ ) - { - MavenProject project = sorted.get( index ); - String id = ProjectSorter.getId( project ); - this.projects.put( id, project ); - this.order.put( project, index ); - } + this( projects, projects ); } /** * Creates a new project dependency graph based on the specified projects. * * @param allProjects All collected projects. - * @param projects The projects to create the dependency graph with. - * + * @param projects The projects to create the dependency graph with. * @throws DuplicateProjectException * @throws CycleDetectedException * @since 3.5.0 */ - public DefaultProjectDependencyGraph( final List allProjects, - final Collection projects ) - throws CycleDetectedException, DuplicateProjectException + public DefaultProjectDependencyGraph( Collection allProjects, + Collection projects ) + throws CycleDetectedException, DuplicateProjectException { - super(); this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) ); this.sorter = new ProjectSorter( projects ); this.order = new HashMap<>(); diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java b/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java index 40cdf1b3f6..a736410347 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java @@ -19,13 +19,12 @@ package org.apache.maven.project; * under the License. */ -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Dependency; @@ -49,8 +48,6 @@ public class ProjectSorter private Map projectMap; - private MavenProject topLevelProject; - /** * Sort a list of projects. *
    @@ -126,43 +123,29 @@ public class ProjectSorter parent.getVersion(), true, false ); } - List buildPlugins = project.getBuildPlugins(); - if ( buildPlugins != null ) + for ( Plugin plugin : project.getBuildPlugins() ) { - for ( Plugin plugin : buildPlugins ) - { - addEdge( projectMap, vertexMap, project, projectVertex, plugin.getGroupId(), - plugin.getArtifactId(), plugin.getVersion(), false, true ); + addEdge( projectMap, vertexMap, project, projectVertex, plugin.getGroupId(), + plugin.getArtifactId(), plugin.getVersion(), false, true ); - for ( Dependency dependency : plugin.getDependencies() ) - { - addEdge( projectMap, vertexMap, project, projectVertex, dependency.getGroupId(), - dependency.getArtifactId(), dependency.getVersion(), false, true ); - } + for ( Dependency dependency : plugin.getDependencies() ) + { + addEdge( projectMap, vertexMap, project, projectVertex, dependency.getGroupId(), + dependency.getArtifactId(), dependency.getVersion(), false, true ); } } - List buildExtensions = project.getBuildExtensions(); - if ( buildExtensions != null ) + for ( Extension extension : project.getBuildExtensions() ) { - for ( Extension extension : buildExtensions ) - { - addEdge( projectMap, vertexMap, project, projectVertex, extension.getGroupId(), - extension.getArtifactId(), extension.getVersion(), false, true ); - } + addEdge( projectMap, vertexMap, project, projectVertex, extension.getGroupId(), + extension.getArtifactId(), extension.getVersion(), false, true ); } } - List sortedProjects = new ArrayList<>( projects.size() ); - List sortedProjectLabels = TopologicalSorter.sort( dag ); - for ( String id : sortedProjectLabels ) - { - sortedProjects.add( projectMap.get( id ) ); - } - - this.sortedProjects = Collections.unmodifiableList( sortedProjects ); + this.sortedProjects = sortedProjectLabels.stream().map( id -> projectMap.get( id ) ) + .collect( Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList ) ); } @SuppressWarnings( "checkstyle:parameternumber" ) private void addEdge( Map projectMap, Map> vertexMap, @@ -235,19 +218,8 @@ public class ProjectSorter // TODO !![jc; 28-jul-2005] check this; if we're using '-r' and there are aggregator tasks, this will result in weirdness. public MavenProject getTopLevelProject() { - if ( topLevelProject == null ) - { - for ( Iterator i = sortedProjects.iterator(); i.hasNext() && ( topLevelProject == null ); ) - { - MavenProject project = i.next(); - if ( project.isExecutionRoot() ) - { - topLevelProject = project; - } - } - } - - return topLevelProject; + return sortedProjects.stream().filter( MavenProject::isExecutionRoot ).findFirst() + .orElse( null ); } public List getSortedProjects()