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 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 private boolean failBuild = false;
76
77
78
79
80
81
82 private boolean ignoreDirect = true;
83
84
85
86
87
88
89 public void execute()
90 throws MojoExecutionException, MojoFailureException
91 {
92 boolean result = checkDependencyManagement();
93 if ( result )
94 {
95 if ( this.failBuild )
96
97 {
98 throw new MojoExecutionException( "Found Dependency errors." );
99 }
100 else
101 {
102 getLog().warn( "Potential problems found in Dependency Management " );
103 }
104 }
105 }
106
107
108
109
110
111
112 private boolean checkDependencyManagement()
113 throws MojoExecutionException
114 {
115 boolean foundError = false;
116
117 getLog().info( "Found Resolved Dependency / DependencyManagement mismatches:" );
118
119 List depMgtDependencies = null;
120
121 DependencyManagement depMgt = project.getDependencyManagement();
122 if ( depMgt != null )
123 {
124 depMgtDependencies = depMgt.getDependencies();
125 }
126
127 if ( depMgtDependencies != null && !depMgtDependencies.isEmpty() )
128 {
129
130 Map depMgtMap = new HashMap();
131 Map exclusions = new HashMap();
132 Iterator iter = depMgtDependencies.iterator();
133 while ( iter.hasNext() )
134 {
135 Dependency depMgtDependency = (Dependency) iter.next();
136 depMgtMap.put( depMgtDependency.getManagementKey(), depMgtDependency );
137
138
139 exclusions.putAll( addExclusions( depMgtDependency.getExclusions() ) );
140 }
141
142
143 Set allDependencyArtifacts = new HashSet( project.getArtifacts() );
144
145
146
147 if ( this.ignoreDirect )
148 {
149 getLog().info( "\tIgnoring Direct Dependencies." );
150 Set directDependencies = project.getDependencyArtifacts();
151 allDependencyArtifacts.removeAll( directDependencies );
152 }
153
154
155 List exclusionErrors = getExclusionErrors( exclusions, allDependencyArtifacts );
156 Iterator exclusionIter = exclusionErrors.iterator();
157 while ( exclusionIter.hasNext() )
158 {
159 Artifact exclusion = (Artifact) exclusionIter.next();
160 getLog().info(
161 StringUtils.stripEnd( getArtifactManagementKey( exclusion ),":") + " was excluded in DepMgt, but version "
162 + exclusion.getVersion() + " has been found in the dependency tree." );
163 foundError = true;
164 }
165
166
167 Map mismatch = getMismatch( depMgtMap, allDependencyArtifacts );
168 Iterator mismatchIter = mismatch.keySet().iterator();
169 while ( mismatchIter.hasNext() )
170 {
171 Artifact resolvedArtifact = (Artifact) mismatchIter.next();
172 Dependency depMgtDependency = (Dependency) mismatch.get( resolvedArtifact );
173 logMismatch( resolvedArtifact, depMgtDependency );
174 }
175 if ( !foundError )
176 {
177 getLog().info( " None" );
178 }
179 }
180 else
181 {
182 getLog().info( " Nothing in DepMgt." );
183 }
184
185
186
187 return foundError;
188 }
189
190
191
192
193
194
195
196
197
198
199 public Map addExclusions( List exclusionList )
200 {
201 Map exclusions = new HashMap();
202 if ( exclusionList != null )
203 {
204 Iterator exclusionIter = exclusionList.iterator();
205 while ( exclusionIter.hasNext() )
206 {
207 Exclusion exclusion = (Exclusion) exclusionIter.next();
208 exclusions.put( getExclusionKey( exclusion ), exclusion );
209 }
210 }
211 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 List list = new ArrayList();
228
229 Iterator iter = allDependencyArtifacts.iterator();
230 while ( iter.hasNext() )
231 {
232 Artifact artifact = (Artifact) iter.next();
233 if ( exclusions.containsKey( getExclusionKey( artifact ) ) )
234 {
235 list.add( artifact );
236 }
237 }
238
239 return list;
240 }
241
242 public String getExclusionKey(Artifact artifact)
243 {
244 return artifact.getGroupId()+":"+artifact.getArtifactId();
245 }
246
247 public String getExclusionKey(Exclusion ex)
248 {
249 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 Map mismatchMap = new HashMap();
267
268 Iterator iter = allDependencyArtifacts.iterator();
269 while ( iter.hasNext() )
270 {
271 Artifact dependencyArtifact = (Artifact) iter.next();
272 Dependency depFromDepMgt = (Dependency) depMgtMap.get( getArtifactManagementKey( dependencyArtifact ) );
273 if ( depFromDepMgt != null )
274 {
275
276
277 dependencyArtifact.isSnapshot();
278
279 if (!depFromDepMgt.getVersion().equals( dependencyArtifact.getBaseVersion()) )
280 {
281 mismatchMap.put( dependencyArtifact, depFromDepMgt );
282 }
283 }
284 }
285 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 if ( dependencyArtifact == null || dependencyFromDepMgt == null )
302 {
303 throw new MojoExecutionException( "Invalid params: Artifact:" + dependencyArtifact + " Dependency:"
304 + dependencyFromDepMgt );
305 }
306
307 getLog().info( "\tDependency: " + StringUtils.stripEnd(dependencyFromDepMgt.getManagementKey(),":") );
308 getLog().info( "\t\tDepMgt : " + dependencyFromDepMgt.getVersion() );
309 getLog().info( "\t\tResolved: " + dependencyArtifact.getBaseVersion() );
310 }
311
312
313
314
315
316
317
318
319
320 public String getArtifactManagementKey( Artifact artifact )
321 {
322 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getType()
323 + (( artifact.getClassifier() !=null ) ? ":" + artifact.getClassifier() : "" );
324 }
325
326
327
328
329 public boolean isFailBuild()
330 {
331 return this.failBuild;
332 }
333
334
335
336
337
338 public void setFailBuild( boolean theFailBuild )
339 {
340 this.failBuild = theFailBuild;
341 }
342
343
344
345
346 public MavenProject getProject()
347 {
348 return this.project;
349 }
350
351
352
353
354
355 public void setProject( MavenProject theProject )
356 {
357 this.project = theProject;
358 }
359
360
361
362
363 public boolean isIgnoreDirect()
364 {
365 return this.ignoreDirect;
366 }
367
368
369
370
371
372 public void setIgnoreDirect( boolean theIgnoreDirect )
373 {
374 this.ignoreDirect = theIgnoreDirect;
375 }
376 }