001package org.apache.maven.graph; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.HashSet; 025import java.util.List; 026import java.util.Set; 027 028import org.apache.maven.execution.ProjectDependencyGraph; 029import org.apache.maven.project.DuplicateProjectException; 030import org.apache.maven.project.MavenProject; 031import org.apache.maven.project.ProjectSorter; 032import org.codehaus.plexus.util.dag.CycleDetectedException; 033 034/** 035 * Describes the inter-dependencies between projects in the reactor. 036 * 037 * @author Benjamin Bentmann 038 */ 039public class DefaultProjectDependencyGraph 040 implements ProjectDependencyGraph 041{ 042 043 private ProjectSorter sorter; 044 045 /** 046 * Creates a new project dependency graph based on the specified projects. 047 * 048 * @param projects The projects to create the dependency graph with 049 * @throws DuplicateProjectException 050 * @throws CycleDetectedException 051 */ 052 public DefaultProjectDependencyGraph( Collection<MavenProject> projects ) 053 throws CycleDetectedException, DuplicateProjectException 054 { 055 this.sorter = new ProjectSorter( projects ); 056 } 057 058 public List<MavenProject> getSortedProjects() 059 { 060 return new ArrayList<MavenProject>( sorter.getSortedProjects() ); 061 } 062 063 public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive ) 064 { 065 if ( project == null ) 066 { 067 throw new IllegalArgumentException( "project missing" ); 068 } 069 070 Set<String> projectIds = new HashSet<String>(); 071 072 getDownstreamProjects( ProjectSorter.getId( project ), projectIds, transitive ); 073 074 return getSortedProjects( projectIds ); 075 } 076 077 private void getDownstreamProjects( String projectId, Set<String> projectIds, boolean transitive ) 078 { 079 for ( String id : sorter.getDependents( projectId ) ) 080 { 081 if ( projectIds.add( id ) && transitive ) 082 { 083 getDownstreamProjects( id, projectIds, transitive ); 084 } 085 } 086 } 087 088 public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive ) 089 { 090 if ( project == null ) 091 { 092 throw new IllegalArgumentException( "project missing" ); 093 } 094 095 Set<String> projectIds = new HashSet<String>(); 096 097 getUpstreamProjects( ProjectSorter.getId( project ), projectIds, transitive ); 098 099 return getSortedProjects( projectIds ); 100 } 101 102 private void getUpstreamProjects( String projectId, Collection<String> projectIds, boolean transitive ) 103 { 104 for ( String id : sorter.getDependencies( projectId ) ) 105 { 106 if ( projectIds.add( id ) && transitive ) 107 { 108 getUpstreamProjects( id, projectIds, transitive ); 109 } 110 } 111 } 112 113 private List<MavenProject> getSortedProjects( Set<String> projectIds ) 114 { 115 List<MavenProject> result = new ArrayList<MavenProject>( projectIds.size() ); 116 117 for ( MavenProject mavenProject : sorter.getSortedProjects() ) 118 { 119 if ( projectIds.contains( ProjectSorter.getId( mavenProject ) ) ) 120 { 121 result.add( mavenProject ); 122 } 123 } 124 125 return result; 126 } 127 128 @Override 129 public String toString() 130 { 131 return sorter.getSortedProjects().toString(); 132 } 133 134}