1 package org.apache.maven.project; 2 3 18 19 import org.apache.maven.artifact.ArtifactUtils; 20 import org.apache.maven.model.Dependency; 21 import org.apache.maven.model.Extension; 22 import org.apache.maven.model.Plugin; 23 import org.apache.maven.model.ReportPlugin; 24 import org.codehaus.plexus.util.dag.CycleDetectedException; 25 import org.codehaus.plexus.util.dag.DAG; 26 import org.codehaus.plexus.util.dag.TopologicalSorter; 27 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 41 public class ProjectSorter 42 { 43 private final DAG dag; 44 45 private final List sortedProjects; 46 47 private MavenProject topLevelProject; 48 49 61 public ProjectSorter( List projects ) 62 throws CycleDetectedException, DuplicateProjectException 63 { 64 dag = new DAG(); 65 66 Map projectMap = new HashMap (); 67 68 for ( Iterator i = projects.iterator(); i.hasNext(); ) 69 { 70 MavenProject project = (MavenProject) i.next(); 71 72 String id = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ); 73 74 if ( dag.getVertex( id ) != null ) 75 { 76 throw new DuplicateProjectException( "Project '" + id + "' is duplicated in the reactor" ); 77 } 78 79 dag.addVertex( id ); 80 81 projectMap.put( id, project ); 82 } 83 84 for ( Iterator i = projects.iterator(); i.hasNext(); ) 85 { 86 MavenProject project = (MavenProject) i.next(); 87 88 String id = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ); 89 90 for ( Iterator j = project.getDependencies().iterator(); j.hasNext(); ) 91 { 92 Dependency dependency = (Dependency) j.next(); 93 94 String dependencyId = ArtifactUtils.versionlessKey( dependency.getGroupId(), 95 dependency.getArtifactId() ); 96 97 if ( dag.getVertex( dependencyId ) != null ) 98 { 99 project.addProjectReference( (MavenProject) projectMap.get( dependencyId ) ); 100 101 dag.addEdge( id, dependencyId ); 102 } 103 } 104 105 MavenProject parent = project.getParent(); 106 if ( parent != null ) 107 { 108 String parentId = ArtifactUtils.versionlessKey( parent.getGroupId(), parent.getArtifactId() ); 109 if ( dag.getVertex( parentId ) != null ) 110 { 111 if ( dag.hasEdge( parentId, id ) ) 113 { 114 dag.removeEdge( parentId, id ); 115 } 116 dag.addEdge( id, parentId ); 117 } 118 } 119 120 List buildPlugins = project.getBuildPlugins(); 121 if ( buildPlugins != null ) 122 { 123 for ( Iterator j = buildPlugins.iterator(); j.hasNext(); ) 124 { 125 Plugin plugin = (Plugin) j.next(); 126 String pluginId = ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ); 127 if ( dag.getVertex( pluginId ) != null && !pluginId.equals( id ) ) 128 { 129 addEdgeWithParentCheck( projectMap, pluginId, project, id ); 130 } 131 } 132 } 133 134 List reportPlugins = project.getReportPlugins(); 135 if ( reportPlugins != null ) 136 { 137 for ( Iterator j = reportPlugins.iterator(); j.hasNext(); ) 138 { 139 ReportPlugin plugin = (ReportPlugin) j.next(); 140 String pluginId = ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() ); 141 if ( dag.getVertex( pluginId ) != null && !pluginId.equals( id ) ) 142 { 143 addEdgeWithParentCheck( projectMap, pluginId, project, id ); 144 } 145 } 146 } 147 148 for ( Iterator j = project.getBuildExtensions().iterator(); j.hasNext(); ) 149 { 150 Extension extension = (Extension) j.next(); 151 String extensionId = ArtifactUtils.versionlessKey( extension.getGroupId(), extension.getArtifactId() ); 152 if ( dag.getVertex( extensionId ) != null ) 153 { 154 addEdgeWithParentCheck( projectMap, extensionId, project, id ); 155 } 156 } 157 } 158 159 List sortedProjects = new ArrayList (); 160 161 for ( Iterator i = TopologicalSorter.sort( dag ).iterator(); i.hasNext(); ) 162 { 163 String id = (String ) i.next(); 164 165 sortedProjects.add( projectMap.get( id ) ); 166 } 167 168 this.sortedProjects = Collections.unmodifiableList( sortedProjects ); 169 } 170 171 private void addEdgeWithParentCheck( Map projectMap, String extensionId, MavenProject project, String id ) 172 throws CycleDetectedException 173 { 174 MavenProject extProject = (MavenProject) projectMap.get( extensionId ); 175 project.addProjectReference( extProject ); 176 177 MavenProject extParent = extProject.getParent(); 178 if ( extParent != null ) 179 { 180 String parentId = ArtifactUtils.versionlessKey( extParent.getGroupId(), extParent.getArtifactId() ); 181 if ( !dag.hasEdge( extensionId, id ) || !parentId.equals( id ) ) 183 { 184 dag.addEdge( id, extensionId ); 185 } 186 } 187 } 188 189 public MavenProject getTopLevelProject() 191 { 192 if ( topLevelProject == null ) 193 { 194 for ( Iterator i = sortedProjects.iterator(); i.hasNext() && topLevelProject == null; ) 195 { 196 MavenProject project = (MavenProject) i.next(); 197 if ( project.isExecutionRoot() ) 198 { 199 topLevelProject = project; 200 } 201 } 202 } 203 204 return topLevelProject; 205 } 206 207 public List getSortedProjects() 208 { 209 return sortedProjects; 210 } 211 212 public boolean hasMultipleProjects() 213 { 214 return sortedProjects.size() > 1; 215 } 216 217 public List getDependents( String id ) 218 { 219 return dag.getParentLabels( id ); 220 } 221 } 222 | Popular Tags |