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.Collections;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Objects;
28  import java.util.Set;
29  
30  import org.apache.maven.execution.ProjectDependencyGraph;
31  import org.apache.maven.project.DuplicateProjectException;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.project.ProjectSorter;
34  import org.codehaus.plexus.util.dag.CycleDetectedException;
35  
36  /**
37   * Describes the inter-dependencies between projects in the reactor.
38   *
39   * @author Benjamin Bentmann
40   */
41  public class DefaultProjectDependencyGraph
42      implements ProjectDependencyGraph
43  {
44  
45      private ProjectSorter sorter;
46  
47      private List<MavenProject> allProjects;
48  
49      /**
50       * Creates a new project dependency graph based on the specified projects.
51       *
52       * @param projects The projects to create the dependency graph with
53       * @throws DuplicateProjectException
54       * @throws CycleDetectedException
55       */
56      public DefaultProjectDependencyGraph( Collection<MavenProject> projects )
57          throws CycleDetectedException, DuplicateProjectException
58      {
59          super();
60          this.allProjects = Collections.unmodifiableList( new ArrayList<>( projects ) );
61          this.sorter = new ProjectSorter( projects );
62      }
63  
64      /**
65       * Creates a new project dependency graph based on the specified projects.
66       *
67       * @param allProjects All collected projects.
68       * @param projects The projects to create the dependency graph with.
69       *
70       * @throws DuplicateProjectException
71       * @throws CycleDetectedException
72       * @since 3.5.0
73       */
74      public DefaultProjectDependencyGraph( final List<MavenProject> allProjects,
75                                            final Collection<MavenProject> projects )
76          throws CycleDetectedException, DuplicateProjectException
77      {
78          super();
79          this.allProjects = Collections.unmodifiableList( new ArrayList<>( allProjects ) );
80          this.sorter = new ProjectSorter( projects );
81      }
82  
83      /**
84       * @since 3.5.0
85       */
86      public List<MavenProject> getAllProjects()
87      {
88          return this.allProjects;
89      }
90  
91      public List<MavenProject> getSortedProjects()
92      {
93          return new ArrayList<>( sorter.getSortedProjects() );
94      }
95  
96      public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
97      {
98          Objects.requireNonNull( project, "project cannot be null" );
99  
100         Set<String> projectIds = new HashSet<>();
101 
102         getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
103 
104         return getSortedProjects( projectIds );
105     }
106 
107     private void getDownstreamProjects( String projectId, Set<String> projectIds, boolean transitive )
108     {
109         for ( String id : sorter.getDependents( projectId ) )
110         {
111             if ( projectIds.add( id ) && transitive )
112             {
113                 getDownstreamProjects( id, projectIds, transitive );
114             }
115         }
116     }
117 
118     public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
119     {
120         Objects.requireNonNull( project, "project cannot be null" );
121 
122         Set<String> projectIds = new HashSet<>();
123 
124         getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive );
125 
126         return getSortedProjects( projectIds );
127     }
128 
129     private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive )
130     {
131         for ( String id : sorter.getDependencies( projectId ) )
132         {
133             if ( projectIds.add( id ) && transitive )
134             {
135                 getUpstreamProjects( id, projectIds, transitive );
136             }
137         }
138     }
139 
140     private List<MavenProject> getSortedProjects( Set<String> projectIds )
141     {
142         List<MavenProject> result = new ArrayList<>( projectIds.size() );
143 
144         for ( MavenProject mavenProject : sorter.getSortedProjects() )
145         {
146             if ( projectIds.contains( ProjectSorter.getId( mavenProject ) ) )
147             {
148                 result.add( mavenProject );
149             }
150         }
151 
152         return result;
153     }
154 
155     @Override
156     public String toString()
157     {
158         return sorter.getSortedProjects().toString();
159     }
160 
161 }