From c1cea8cbb8ed672275bbba59afcaeabb6b4ec331 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 20 Apr 2005 16:48:11 +0000 Subject: [PATCH] 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 --- .../java/org/apache/maven/DefaultMaven.java | 3 +- .../apache/maven/project/MavenProject.java | 88 ++----------- .../apache/maven/project/ProjectSorter.java | 122 ++++++++++++++++++ .../maven/project/ProjectSorterTest.java | 85 ++++++++++++ 4 files changed, 218 insertions(+), 80 deletions(-) create mode 100644 maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java create mode 100644 maven-core/src/test/java/org/apache/maven/project/ProjectSorterTest.java diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 9455b65866..878fe6cb7f 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -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() ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index b55b1d4c05..71aaa26e93 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -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. - * - */ - 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() ); diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java b/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java new file mode 100644 index 0000000000..954a26a681 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java @@ -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 Brett Porter + * @version $Id$ + */ +public class ProjectSorter +{ + private ProjectSorter() + { + // no touchy... + } + + /** + * Sort a list of projects. + * + */ + 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(); + } +} diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectSorterTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectSorterTest.java new file mode 100644 index 0000000000..51516573d4 --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectSorterTest.java @@ -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 Brett Porter + * @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 ); + } +}