* <li>do a topo sort on the graph that remains.</li>
* </ul>
*/
public static List<Project> getSortedProjects( Collection<Project> projects, Logger logger )
{
DAG dag = new DAG();
Map<String, Project> projectMap = new HashMap<String, Project>();
for ( Project project : projects )
{
String id = getProjectId( project );
if ( dag.getVertex( id ) != null )
{
logger.warn( "Project '" + id + "' is duplicated in the reactor." );
}
dag.addVertex( id );
projectMap.put( id, project );
}
for ( Project project : projects )
{
String id = getProjectId( project );
String projectGroupId = "[" + project.getProjectGroup().getId() + "]";
// Dependencies
for ( Object o : project.getDependencies() )
{
ProjectDependency dependency = (ProjectDependency) o;
String dependencyId = projectGroupId + ":" + getDependencyId( dependency );
if ( dag.getVertex( dependencyId ) != null )
{
try
{
dag.addEdge( id, dependencyId );
}
catch ( CycleDetectedException e )
{
logger.warn( "Ignore cycle detected in project dependencies: " + e.getMessage() );
}
}
}
// Parent
ProjectDependency parent = project.getParent();
if ( parent != null )
{
String parentId = projectGroupId + ":" + getDependencyId( parent );
if ( dag.getVertex( parentId ) != null )
{
// Parent is added as an edge, but must not cause a cycle - so we remove any other edges it has in conflict
if ( dag.hasEdge( parentId, id ) )
{
dag.removeEdge( parentId, id );
}
try
{
dag.addEdge( id, parentId );
}
catch ( CycleDetectedException e )
{
logger.warn( "Ignore cycle detected in project parent: " + e.getMessage() );
}