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