1 | |
package org.apache.maven.plugin.dependency; |
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.HashMap; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
import java.util.Set; |
29 | |
|
30 | |
import org.apache.maven.artifact.Artifact; |
31 | |
import org.apache.maven.model.Dependency; |
32 | |
import org.apache.maven.model.DependencyManagement; |
33 | |
import org.apache.maven.model.Exclusion; |
34 | |
import org.apache.maven.plugin.AbstractMojo; |
35 | |
import org.apache.maven.plugin.MojoExecutionException; |
36 | |
import org.apache.maven.plugin.MojoFailureException; |
37 | |
import org.apache.maven.project.MavenProject; |
38 | |
import org.codehaus.plexus.util.StringUtils; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 5 | public class AnalyzeDepMgt |
57 | |
extends AbstractMojo |
58 | |
{ |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private MavenProject project; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 5 | private boolean failBuild = false; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 5 | private boolean ignoreDirect = true; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void execute() |
90 | |
throws MojoExecutionException, MojoFailureException |
91 | |
{ |
92 | 4 | boolean result = checkDependencyManagement(); |
93 | 4 | if ( result ) |
94 | |
{ |
95 | 2 | if ( this.failBuild ) |
96 | |
|
97 | |
{ |
98 | 1 | throw new MojoExecutionException( "Found Dependency errors." ); |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 1 | getLog().warn( "Potential problems found in Dependency Management " ); |
103 | |
} |
104 | |
} |
105 | 3 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private boolean checkDependencyManagement() |
113 | |
throws MojoExecutionException |
114 | |
{ |
115 | 4 | boolean foundError = false; |
116 | |
|
117 | 4 | getLog().info( "Found Resolved Dependency / DependencyManagement mismatches:" ); |
118 | |
|
119 | 4 | List depMgtDependencies = null; |
120 | |
|
121 | 4 | DependencyManagement depMgt = project.getDependencyManagement(); |
122 | 4 | if ( depMgt != null ) |
123 | |
{ |
124 | 4 | depMgtDependencies = depMgt.getDependencies(); |
125 | |
} |
126 | |
|
127 | 4 | if ( depMgtDependencies != null && !depMgtDependencies.isEmpty() ) |
128 | |
{ |
129 | |
|
130 | 3 | Map depMgtMap = new HashMap(); |
131 | 3 | Map exclusions = new HashMap(); |
132 | 3 | Iterator iter = depMgtDependencies.iterator(); |
133 | 6 | while ( iter.hasNext() ) |
134 | |
{ |
135 | 3 | Dependency depMgtDependency = (Dependency) iter.next(); |
136 | 3 | depMgtMap.put( depMgtDependency.getManagementKey(), depMgtDependency ); |
137 | |
|
138 | |
|
139 | 3 | exclusions.putAll( addExclusions( depMgtDependency.getExclusions() ) ); |
140 | 3 | } |
141 | |
|
142 | |
|
143 | 3 | Set allDependencyArtifacts = new HashSet( project.getArtifacts() ); |
144 | |
|
145 | |
|
146 | |
|
147 | 3 | if ( this.ignoreDirect ) |
148 | |
{ |
149 | 1 | getLog().info( "\tIgnoring Direct Dependencies." ); |
150 | 1 | Set directDependencies = project.getDependencyArtifacts(); |
151 | 1 | allDependencyArtifacts.removeAll( directDependencies ); |
152 | |
} |
153 | |
|
154 | |
|
155 | 3 | List exclusionErrors = getExclusionErrors( exclusions, allDependencyArtifacts ); |
156 | 3 | Iterator exclusionIter = exclusionErrors.iterator(); |
157 | 5 | while ( exclusionIter.hasNext() ) |
158 | |
{ |
159 | 2 | Artifact exclusion = (Artifact) exclusionIter.next(); |
160 | 2 | getLog().info( |
161 | |
StringUtils.stripEnd( getArtifactManagementKey( exclusion ),":") + " was excluded in DepMgt, but version " |
162 | |
+ exclusion.getVersion() + " has been found in the dependency tree." ); |
163 | 2 | foundError = true; |
164 | 2 | } |
165 | |
|
166 | |
|
167 | 3 | Map mismatch = getMismatch( depMgtMap, allDependencyArtifacts ); |
168 | 3 | Iterator mismatchIter = mismatch.keySet().iterator(); |
169 | 5 | while ( mismatchIter.hasNext() ) |
170 | |
{ |
171 | 2 | Artifact resolvedArtifact = (Artifact) mismatchIter.next(); |
172 | 2 | Dependency depMgtDependency = (Dependency) mismatch.get( resolvedArtifact ); |
173 | 2 | logMismatch( resolvedArtifact, depMgtDependency ); |
174 | 2 | } |
175 | 3 | if ( !foundError ) |
176 | |
{ |
177 | 1 | getLog().info( " None" ); |
178 | |
} |
179 | 3 | } |
180 | |
else |
181 | |
{ |
182 | 1 | getLog().info( " Nothing in DepMgt." ); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | 4 | return foundError; |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public Map addExclusions( List exclusionList ) |
200 | |
{ |
201 | 6 | Map exclusions = new HashMap(); |
202 | 6 | if ( exclusionList != null ) |
203 | |
{ |
204 | 5 | Iterator exclusionIter = exclusionList.iterator(); |
205 | 10 | while ( exclusionIter.hasNext() ) |
206 | |
{ |
207 | 5 | Exclusion exclusion = (Exclusion) exclusionIter.next(); |
208 | 5 | exclusions.put( getExclusionKey( exclusion ), exclusion ); |
209 | 5 | } |
210 | |
} |
211 | 6 | return exclusions; |
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public List getExclusionErrors( Map exclusions, Set allDependencyArtifacts ) |
226 | |
{ |
227 | 4 | List list = new ArrayList(); |
228 | |
|
229 | 4 | Iterator iter = allDependencyArtifacts.iterator(); |
230 | 51 | while ( iter.hasNext() ) |
231 | |
{ |
232 | 47 | Artifact artifact = (Artifact) iter.next(); |
233 | 47 | if ( exclusions.containsKey( getExclusionKey( artifact ) ) ) |
234 | |
{ |
235 | 3 | list.add( artifact ); |
236 | |
} |
237 | 47 | } |
238 | |
|
239 | 4 | return list; |
240 | |
} |
241 | |
|
242 | |
public String getExclusionKey(Artifact artifact) |
243 | |
{ |
244 | 48 | return artifact.getGroupId()+":"+artifact.getArtifactId(); |
245 | |
} |
246 | |
|
247 | |
public String getExclusionKey(Exclusion ex) |
248 | |
{ |
249 | 8 | return ex.getGroupId()+":"+ex.getArtifactId(); |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
public Map getMismatch( Map depMgtMap, Set allDependencyArtifacts ) |
265 | |
{ |
266 | 4 | Map mismatchMap = new HashMap(); |
267 | |
|
268 | 4 | Iterator iter = allDependencyArtifacts.iterator(); |
269 | 51 | while ( iter.hasNext() ) |
270 | |
{ |
271 | 47 | Artifact dependencyArtifact = (Artifact) iter.next(); |
272 | 47 | Dependency depFromDepMgt = (Dependency) depMgtMap.get( getArtifactManagementKey( dependencyArtifact ) ); |
273 | 47 | if ( depFromDepMgt != null ) |
274 | |
{ |
275 | |
|
276 | |
|
277 | 3 | dependencyArtifact.isSnapshot(); |
278 | |
|
279 | 3 | if (!depFromDepMgt.getVersion().equals( dependencyArtifact.getBaseVersion()) ) |
280 | |
{ |
281 | 3 | mismatchMap.put( dependencyArtifact, depFromDepMgt ); |
282 | |
} |
283 | |
} |
284 | 47 | } |
285 | 4 | return mismatchMap; |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public void logMismatch( Artifact dependencyArtifact, Dependency dependencyFromDepMgt ) |
299 | |
throws MojoExecutionException |
300 | |
{ |
301 | 2 | if ( dependencyArtifact == null || dependencyFromDepMgt == null ) |
302 | |
{ |
303 | 0 | throw new MojoExecutionException( "Invalid params: Artifact:" + dependencyArtifact + " Dependency:" |
304 | |
+ dependencyFromDepMgt ); |
305 | |
} |
306 | |
|
307 | 2 | getLog().info( "\tDependency: " + StringUtils.stripEnd(dependencyFromDepMgt.getManagementKey(),":") ); |
308 | 2 | getLog().info( "\t\tDepMgt : " + dependencyFromDepMgt.getVersion() ); |
309 | 2 | getLog().info( "\t\tResolved: " + dependencyArtifact.getBaseVersion() ); |
310 | 2 | } |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public String getArtifactManagementKey( Artifact artifact ) |
321 | |
{ |
322 | 59 | return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getType() |
323 | |
+ (( artifact.getClassifier() !=null ) ? ":" + artifact.getClassifier() : "" ); |
324 | |
} |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public boolean isFailBuild() |
330 | |
{ |
331 | 0 | return this.failBuild; |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
public void setFailBuild( boolean theFailBuild ) |
339 | |
{ |
340 | 2 | this.failBuild = theFailBuild; |
341 | 2 | } |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
public MavenProject getProject() |
347 | |
{ |
348 | 5 | return this.project; |
349 | |
} |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public void setProject( MavenProject theProject ) |
356 | |
{ |
357 | 5 | this.project = theProject; |
358 | 5 | } |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public boolean isIgnoreDirect() |
364 | |
{ |
365 | 0 | return this.ignoreDirect; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
public void setIgnoreDirect( boolean theIgnoreDirect ) |
373 | |
{ |
374 | 2 | this.ignoreDirect = theIgnoreDirect; |
375 | 2 | } |
376 | |
} |