View Javadoc
1   package org.apache.maven.graph;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.commons.lang3.Validate;
29  import org.apache.maven.execution.ProjectDependencyGraph;
30  import org.apache.maven.project.DuplicateProjectException;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.project.ProjectSorter;
33  import org.codehaus.plexus.util.dag.CycleDetectedException;
34  
35  /**
36   * Describes the inter-dependencies between projects in the reactor.
37   *
38   * @author Benjamin Bentmann
39   */
40  public class DefaultProjectDependencyGraph
41      implements ProjectDependencyGraph
42  {
43  
44      private ProjectSorter sorter;
45  
46      /**
47       * Creates a new project dependency graph based on the specified projects.
48       *
49       * @param projects The projects to create the dependency graph with
50       * @throws DuplicateProjectException
51       * @throws CycleDetectedException
52       */
53      public DefaultProjectDependencyGraph( Collection<MavenProject> projects )
54          throws CycleDetectedException, DuplicateProjectException
55      {
56          this.sorter = new ProjectSorter( projects );
57      }
58  
59      public List<MavenProject> getSortedProjects()
60      {
61          return new ArrayList<>( sorter.getSortedProjects() );
62      }
63  
64      public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
65      {
66          Validate.notNull( project, "project cannot be null" );
67  
68          Set<String> projectIds = new HashSet<>();
69  
70          getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
71  
72          return getSortedProjects( projectIds );
73      }
74  
75      private void getDownstreamProjects( String projectId, Set<String> projectIds, boolean transitive )
76      {
77          for ( String id : sorter.getDependents( projectId ) )
78          {
79              if ( projectIds.add( id ) && transitive )
80              {
81                  getDownstreamProjects( id, projectIds, transitive );
82              }
83          }
84      }
85  
86      public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
87      {
88          Validate.notNull( project, "project cannot be null" );
89  
90          Set<String> projectIds = new HashSet<>();
91  
92          getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
93  
94          return getSortedProjects( projectIds );
95      }
96  
97      private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
98      {
99          for ( String id : sorter.getDependencies( projectId ) )
100         {
101             if ( projectIds.add( id ) && transitive )
102             {
103                 getUpstreamProjects( id, projectIds, transitive );
104             }
105         }
106     }
107 
108     private List<MavenProject> getSortedProjects( Set<String> projectIds )
109     {
110         List<MavenProject> result = new ArrayList<>( projectIds.size() );
111 
112         for ( MavenProject mavenProject : sorter.getSortedProjects() )
113         {
114             if ( projectIds.contains( ProjectSorter.getId( mavenProject ) ) )
115             {
116                 result.add( mavenProject );
117             }
118         }
119 
120         return result;
121     }
122 
123     @Override
124     public String toString()
125     {
126         return sorter.getSortedProjects().toString();
127     }
128 
129 }