maven-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject cvs commit: maven-components/maven-core/src/test/java/org/apache/maven/project ProjectSorterTest.java
Date Wed, 20 Apr 2005 16:48:11 GMT
brett       2005/04/20 09:48:11

  Modified:    maven-core/src/main/java/org/apache/maven/project
                        MavenProject.java
               maven-core/src/main/java/org/apache/maven DefaultMaven.java
  Added:       maven-core/src/main/java/org/apache/maven/project
                        ProjectSorter.java
               maven-core/src/test/java/org/apache/maven/project
                        ProjectSorterTest.java
  Log:
  factor out project sorter, and use whole ID
  
  Revision  Changes    Path
  1.43      +9 -79     maven-components/maven-core/src/main/java/org/apache/maven/project/MavenProject.java
  
  Index: MavenProject.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/project/MavenProject.java,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- MavenProject.java	18 Apr 2005 04:27:02 -0000	1.42
  +++ MavenProject.java	20 Apr 2005 16:48:11 -0000	1.43
  @@ -24,7 +24,6 @@
   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.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 @@
           {
               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 @@
           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() );
  
  
  
  1.1                  maven-components/maven-core/src/main/java/org/apache/maven/project/ProjectSorter.java
  
  Index: ProjectSorter.java
  ===================================================================
  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: ProjectSorter.java,v 1.1 2005/04/20 16:48:11 brett Exp $
   */
  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();
      }
  }
  
  
  
  1.49      +3 -2      maven-components/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
  
  Index: DefaultMaven.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/DefaultMaven.java,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- DefaultMaven.java	20 Apr 2005 16:41:36 -0000	1.48
  +++ DefaultMaven.java	20 Apr 2005 16:48:11 -0000	1.49
  @@ -32,6 +32,7 @@
   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 @@
           {
               projects = collectProjects( request.getFiles(), request.getLocalRepository(),
request.isRecursive() );
   
  -            projects = MavenProject.getSortedProjects( projects );
  +            projects = ProjectSorter.getSortedProjects( projects );
   
               if ( projects.isEmpty() )
               {
  
  
  
  1.1                  maven-components/maven-core/src/test/java/org/apache/maven/project/ProjectSorterTest.java
  
  Index: ProjectSorterTest.java
  ===================================================================
  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: ProjectSorterTest.java,v 1.1 2005/04/20 16:48:11 brett Exp $
   */
  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 );
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Mime
View raw message