[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

@ -43,7 +43,7 @@ import org.codehaus.plexus.util.dag.CycleDetectedException;
* @author Benjamin Bentmann * @author Benjamin Bentmann
*/ */
public class DefaultProjectDependencyGraph public class DefaultProjectDependencyGraph
implements ProjectDependencyGraph implements ProjectDependencyGraph
{ {
private final ProjectSorter sorter; private final ProjectSorter sorter;
@ -62,38 +62,24 @@ public class DefaultProjectDependencyGraph
* @throws CycleDetectedException * @throws CycleDetectedException
*/ */
public DefaultProjectDependencyGraph( Collection<MavenProject> projects ) public DefaultProjectDependencyGraph( Collection<MavenProject> projects )
throws CycleDetectedException, DuplicateProjectException throws CycleDetectedException, DuplicateProjectException
{ {
super(); this( projects, projects );
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 );
}
} }
/** /**
* Creates a new project dependency graph based on the specified projects. * Creates a new project dependency graph based on the specified projects.
* *
* @param allProjects All collected 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 DuplicateProjectException
* @throws CycleDetectedException * @throws CycleDetectedException
* @since 3.5.0 * @since 3.5.0
*/ */
public DefaultProjectDependencyGraph( final List<MavenProject> allProjects, public DefaultProjectDependencyGraph( Collection<MavenProject> allProjects,
final Collection<MavenProject> projects ) Collection<MavenProject> projects )
throws CycleDetectedException, DuplicateProjectException throws CycleDetectedException, DuplicateProjectException
{ {
super();
this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) ); this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) );
this.sorter = new ProjectSorter( projects ); this.sorter = new ProjectSorter( projects );
this.order = new HashMap<>(); this.order = new HashMap<>();

View File

@ -19,13 +19,12 @@ package org.apache.maven.project;
* under the License. * under the License.
*/ */
import java.util.ArrayList;
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.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.Dependency; import org.apache.maven.model.Dependency;
@ -49,8 +48,6 @@ public class ProjectSorter
private Map<String, MavenProject> projectMap; private Map<String, MavenProject> projectMap;
private MavenProject topLevelProject;
/** /**
* Sort a list of projects. * Sort a list of projects.
* <ul> * <ul>
@ -126,43 +123,29 @@ public class ProjectSorter
parent.getVersion(), true, false ); parent.getVersion(), true, false );
} }
List<Plugin> buildPlugins = project.getBuildPlugins(); for ( Plugin plugin : project.getBuildPlugins() )
if ( buildPlugins != null )
{ {
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() ) for ( Dependency dependency : plugin.getDependencies() )
{ {
addEdge( projectMap, vertexMap, project, projectVertex, dependency.getGroupId(), addEdge( projectMap, vertexMap, project, projectVertex, dependency.getGroupId(),
dependency.getArtifactId(), dependency.getVersion(), false, true ); dependency.getArtifactId(), dependency.getVersion(), false, true );
}
} }
} }
List<Extension> buildExtensions = project.getBuildExtensions(); for ( Extension extension : project.getBuildExtensions() )
if ( buildExtensions != null )
{ {
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<MavenProject> sortedProjects = new ArrayList<>( projects.size() );
List<String> sortedProjectLabels = TopologicalSorter.sort( dag ); List<String> sortedProjectLabels = TopologicalSorter.sort( dag );
for ( String id : sortedProjectLabels ) this.sortedProjects = sortedProjectLabels.stream().map( id -> projectMap.get( id ) )
{ .collect( Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList ) );
sortedProjects.add( projectMap.get( id ) );
}
this.sortedProjects = Collections.unmodifiableList( sortedProjects );
} }
@SuppressWarnings( "checkstyle:parameternumber" ) @SuppressWarnings( "checkstyle:parameternumber" )
private void addEdge( Map<String, MavenProject> projectMap, Map<String, Map<String, Vertex>> vertexMap, 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. // 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() public MavenProject getTopLevelProject()
{ {
if ( topLevelProject == null ) return sortedProjects.stream().filter( MavenProject::isExecutionRoot ).findFirst()
{ .orElse( null );
for ( Iterator<MavenProject> i = sortedProjects.iterator(); i.hasNext() && ( topLevelProject == null ); )
{
MavenProject project = i.next();
if ( project.isExecutionRoot() )
{
topLevelProject = project;
}
}
}
return topLevelProject;
} }
public List<MavenProject> getSortedProjects() public List<MavenProject> getSortedProjects()