factor out project sorter, and use whole ID

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163982 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-04-20 16:48:11 +00:00
parent 06ebdedc0c
commit c1cea8cbb8
4 changed files with 218 additions and 80 deletions

View File

@ -32,6 +32,7 @@ import org.apache.maven.plugin.PluginManager;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectSorter;
import org.apache.maven.reactor.ReactorException;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
@ -104,7 +105,7 @@ public class DefaultMaven
{
projects = collectProjects( request.getFiles(), request.getLocalRepository(), request.isRecursive() );
projects = MavenProject.getSortedProjects( projects );
projects = ProjectSorter.getSortedProjects( projects );
if ( projects.isEmpty() )
{

View File

@ -24,7 +24,6 @@ import org.apache.maven.artifact.repository.ArtifactRepository;
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;
@ -39,9 +38,6 @@ import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Reports;
import org.apache.maven.model.Scm;
import org.apache.maven.util.Xpp3DomUtils;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.codehaus.plexus.util.dag.DAG;
import org.codehaus.plexus.util.dag.TopologicalSorter;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.io.File;
@ -237,15 +233,18 @@ public class MavenProject
{
Artifact a = (Artifact) i.next();
// TODO: let the scope handler deal with this
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) )
if ( isAddedToClasspath( a ) )
{
File file = a.getFile();
if ( file == null )
// TODO: let the scope handler deal with this
if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) )
{
throw new DependencyResolutionRequiredException( a );
File file = a.getFile();
if ( file == null )
{
throw new DependencyResolutionRequiredException( a );
}
list.add( file.getPath() );
}
list.add( file.getPath() );
}
}
return list;
@ -628,75 +627,6 @@ 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;
}
public void addArtifacts( Collection newArtifacts )
{
// project.getArtifacts().addAll( result.getArtifacts().values() );

View File

@ -0,0 +1,122 @@
package org.apache.maven.project;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.model.Dependency;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.codehaus.plexus.util.dag.DAG;
import org.codehaus.plexus.util.dag.TopologicalSorter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Sort projects by dependencies.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class ProjectSorter
{
private ProjectSorter()
{
// no touchy...
}
/**
* 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 id = getProjectId( project );
dag.addVertex( id );
projectMap.put( id, project );
}
for ( Iterator i = projects.iterator(); i.hasNext(); )
{
MavenProject project = (MavenProject) i.next();
String id = getProjectId( project );
for ( Iterator j = project.getDependencies().iterator(); j.hasNext(); )
{
Dependency dependency = (Dependency) j.next();
String dependencyId = getDependencyId( dependency );
if ( dag.getVertex( dependencyId ) != null )
{
dag.addEdge( id, dependencyId );
}
}
MavenProject parent = project.getParent();
if ( parent != null )
{
String parentId = getProjectId( parent );
if ( dag.getVertex( parentId ) != null )
{
dag.addEdge( id, parentId );
}
}
}
List sortedProjects = new ArrayList();
for ( Iterator i = TopologicalSorter.sort( dag ).iterator(); i.hasNext(); )
{
String id = (String) i.next();
sortedProjects.add( projectMap.get( id ) );
}
return sortedProjects;
}
private static String getDependencyId( Dependency dependency )
{
return dependency.getGroupId() + ":" + dependency.getArtifactId();
}
private static String getProjectId( MavenProject project )
{
return project.getGroupId() + ":" + project.getArtifactId();
}
}

View File

@ -0,0 +1,85 @@
package org.apache.maven.project;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import junit.framework.TestCase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import java.util.ArrayList;
import java.util.List;
/**
* Test sorting projects by dependencies.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class ProjectSorterTest
extends TestCase
{
public void testMatchingArtifactIdsDifferentGroupIds()
throws CycleDetectedException
{
List projects = new ArrayList();
MavenProject project1 = createProject( "groupId1", "artifactId", "1.0" );
projects.add( project1 );
MavenProject project2 = createProject( "groupId2", "artifactId", "1.0" );
projects.add( project2 );
project1.getDependencies().add( createDependency( project2 ) );
projects = ProjectSorter.getSortedProjects( projects );
assertEquals( project2, projects.get( 0 ) );
assertEquals( project1, projects.get( 1 ) );
}
public void testMatchingGroupIdsDifferentArtifactIds()
throws CycleDetectedException
{
List projects = new ArrayList();
MavenProject project1 = createProject( "groupId", "artifactId1", "1.0" );
projects.add( project1 );
MavenProject project2 = createProject( "groupId", "artifactId2", "1.0" );
projects.add( project2 );
project1.getDependencies().add( createDependency( project2 ) );
projects = ProjectSorter.getSortedProjects( projects );
assertEquals( project2, projects.get( 0 ) );
assertEquals( project1, projects.get( 1 ) );
}
private Dependency createDependency( MavenProject project )
{
Dependency depdendency = new Dependency();
depdendency.setArtifactId( project.getArtifactId() );
depdendency.setGroupId( project.getGroupId() );
depdendency.setVersion( project.getVersion() );
return depdendency;
}
private static MavenProject createProject( String groupId, String artifactId, String version )
{
Model model = new Model();
model.setGroupId( groupId );
model.setArtifactId( artifactId );
model.setVersion( version );
return new MavenProject( model );
}
}