[MNG-7272] - Code Improvement - II

This commit is contained in:
Karl Heinz Marbaise 2021-09-30 00:46:08 +02:00
parent 35e5a4d71d
commit 71a0a49904
No known key found for this signature in database
GPG Key ID: BF1518E0160788A2
2 changed files with 22 additions and 64 deletions

View File

@ -64,19 +64,7 @@ public class DefaultProjectDependencyGraph
public DefaultProjectDependencyGraph( Collection<MavenProject> projects )
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<MavenProject> 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 );
}
/**
@ -84,16 +72,14 @@ public class DefaultProjectDependencyGraph
*
* @param allProjects All collected projects.
* @param projects The projects to create the dependency graph with.
*
* @throws DuplicateProjectException
* @throws CycleDetectedException
* @since 3.5.0
*/
public DefaultProjectDependencyGraph( final List<MavenProject> allProjects,
final Collection<MavenProject> projects )
public DefaultProjectDependencyGraph( Collection<MavenProject> allProjects,
Collection<MavenProject> projects )
throws CycleDetectedException, DuplicateProjectException
{
super();
this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) );
this.sorter = new ProjectSorter( projects );
this.order = new HashMap<>();

View File

@ -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<String, MavenProject> projectMap;
private MavenProject topLevelProject;
/**
* Sort a list of projects.
* <ul>
@ -126,10 +123,7 @@ public class ProjectSorter
parent.getVersion(), true, false );
}
List<Plugin> buildPlugins = project.getBuildPlugins();
if ( buildPlugins != null )
{
for ( Plugin plugin : buildPlugins )
for ( Plugin plugin : project.getBuildPlugins() )
{
addEdge( projectMap, vertexMap, project, projectVertex, plugin.getGroupId(),
plugin.getArtifactId(), plugin.getVersion(), false, true );
@ -140,29 +134,18 @@ public class ProjectSorter
dependency.getArtifactId(), dependency.getVersion(), false, true );
}
}
}
List<Extension> buildExtensions = project.getBuildExtensions();
if ( buildExtensions != null )
{
for ( Extension extension : buildExtensions )
for ( Extension extension : project.getBuildExtensions() )
{
addEdge( projectMap, vertexMap, project, projectVertex, extension.getGroupId(),
extension.getArtifactId(), extension.getVersion(), false, true );
}
}
}
List<MavenProject> sortedProjects = new ArrayList<>( projects.size() );
List<String> 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<String, MavenProject> projectMap, Map<String, Map<String, Vertex>> 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<MavenProject> 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<MavenProject> getSortedProjects()