project sorting can be a static method

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163575 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-03-16 04:21:03 +00:00
parent 83c5474b1f
commit 64eb3c2ddd
3 changed files with 75 additions and 80 deletions

View File

@ -25,7 +25,6 @@ import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Repository;
@ -42,9 +41,6 @@ import org.apache.maven.project.validation.ModelValidator;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.codehaus.plexus.util.dag.DAG;
import org.codehaus.plexus.util.dag.TopologicalSorter;
import java.io.File;
import java.io.FileNotFoundException;
@ -336,75 +332,6 @@ public class DefaultMavenProjectBuilder
return artifact.getFile();
}
/**
* Sort a list of projects.
* <ul>
* <li>collect all the vertices for the projects that we want to build.</li>
* <li>iterate through the deps of each project and if that dep is within
* the set of projects we want to build then add an edge, otherwise throw
* the edge away because that dependency is not within the set of projects
* we are trying to build. we assume a closed set.</li>
* <li>do a topo sort on the graph that remains.</li>
* </ul>
*/
public List getSortedProjects( List projects )
throws CycleDetectedException
{
DAG dag = new DAG();
Map projectMap = new HashMap();
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
String artifactId = project.getArtifactId();
dag.addVertex( artifactId );
projectMap.put( artifactId, project );
}
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
String artifactId = project.getArtifactId();
for ( Iterator j = project.getDependencies().iterator(); j.hasNext(); )
{
Dependency dependency = (Dependency) j.next();
String dependencyArtifactId = dependency.getArtifactId();
if ( dag.getVertex( dependencyArtifactId ) != null )
{
dag.addEdge( artifactId, dependencyArtifactId );
}
}
MavenProject parent = project.getParent();
if ( parent != null )
{
if ( dag.getVertex( parent.getArtifactId() ) != null )
{
dag.addEdge( artifactId, parent.getArtifactId() );
}
}
}
List sortedProjects = new ArrayList();
for ( Iterator i = TopologicalSorter.sort( dag ).iterator(); i.hasNext(); )
{
String artifactId = (String) i.next();
sortedProjects.add( projectMap.get( artifactId ) );
}
return sortedProjects;
}
public MavenProject getCachedProject( String groupId, String artifactId, String version )
{
return (MavenProject) projectCache.get( createCacheKey( groupId, artifactId, version ) );

View File

@ -21,6 +21,7 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Build;
import org.apache.maven.model.CiManagement;
import org.apache.maven.model.Contributor;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Developer;
import org.apache.maven.model.DistributionManagement;
@ -33,12 +34,17 @@ import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Reports;
import org.apache.maven.model.Scm;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.codehaus.plexus.util.dag.DAG;
import org.codehaus.plexus.util.dag.TopologicalSorter;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@ -569,5 +575,74 @@ public class MavenProject
{
this.collectedProjects = collectedProjects;
}
/**
* Sort a list of projects.
* <ul>
* <li>collect all the vertices for the projects that we want to build.</li>
* <li>iterate through the deps of each project and if that dep is within
* the set of projects we want to build then add an edge, otherwise throw
* the edge away because that dependency is not within the set of projects
* we are trying to build. we assume a closed set.</li>
* <li>do a topo sort on the graph that remains.</li>
* </ul>
*/
public static List getSortedProjects( List projects )
throws CycleDetectedException
{
DAG dag = new DAG();
Map projectMap = new HashMap();
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
String artifactId = project.getArtifactId();
dag.addVertex( artifactId );
projectMap.put( artifactId, project );
}
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
String artifactId = project.getArtifactId();
for ( Iterator j = project.getDependencies().iterator(); j.hasNext(); )
{
Dependency dependency = (Dependency) j.next();
String dependencyArtifactId = dependency.getArtifactId();
if ( dag.getVertex( dependencyArtifactId ) != null )
{
dag.addEdge( artifactId, dependencyArtifactId );
}
}
MavenProject parent = project.getParent();
if ( parent != null )
{
if ( dag.getVertex( parent.getArtifactId() ) != null )
{
dag.addEdge( artifactId, parent.getArtifactId() );
}
}
}
List sortedProjects = new ArrayList();
for ( Iterator i = TopologicalSorter.sort( dag ).iterator(); i.hasNext(); )
{
String artifactId = (String) i.next();
sortedProjects.add( projectMap.get( artifactId ) );
}
return sortedProjects;
}
}

View File

@ -18,10 +18,8 @@ package org.apache.maven.project;
*/
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import java.io.File;
import java.util.List;
public interface MavenProjectBuilder
{
@ -36,10 +34,5 @@ public interface MavenProjectBuilder
MavenProject buildSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException;
// take this out
List getSortedProjects( List projects )
throws CycleDetectedException;
MavenProject getCachedProject( String groupId, String artifactId, String version );
}