1 package org.apache.maven.plugin.dependency.fromDependencies;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
24 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.dependency.AbstractDependencyMojo;
27 import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
28 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
29 import org.apache.maven.plugin.dependency.utils.resolvers.ArtifactsResolver;
30 import org.apache.maven.plugin.dependency.utils.resolvers.DefaultArtifactsResolver;
31 import org.apache.maven.plugin.dependency.utils.translators.ArtifactTranslator;
32 import org.apache.maven.plugin.dependency.utils.translators.ClassifierTypeTranslator;
33 import org.apache.maven.plugins.annotations.Component;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.project.MavenProjectBuilder;
37 import org.apache.maven.project.ProjectBuildingException;
38 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
39 import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
40 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
41 import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
42 import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
43 import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
44 import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
45 import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
46 import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
47 import org.codehaus.plexus.util.StringUtils;
48
49 import java.io.File;
50 import java.util.ArrayList;
51 import java.util.HashSet;
52 import java.util.Set;
53
54
55
56
57
58
59
60
61
62 public abstract class AbstractDependencyFilterMojo
63 extends AbstractDependencyMojo
64 {
65
66
67
68
69
70 @Parameter( property = "overWriteReleases", defaultValue = "false" )
71 protected boolean overWriteReleases;
72
73
74
75
76
77
78 @Parameter( property = "overWriteSnapshots", defaultValue = "false" )
79 protected boolean overWriteSnapshots;
80
81
82
83
84
85
86 @Parameter( property = "overWriteIfNewer", defaultValue = "true" )
87 protected boolean overWriteIfNewer;
88
89
90
91
92
93
94 @Parameter( property = "excludeTransitive", defaultValue = "false" )
95 protected boolean excludeTransitive;
96
97
98
99
100
101
102
103 @Parameter( property = "includeTypes", defaultValue = "" )
104 protected String includeTypes;
105
106
107
108
109
110
111
112 @Parameter( property = "excludeTypes", defaultValue = "" )
113 protected String excludeTypes;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 @Parameter( property = "includeScope", defaultValue = "" )
129 protected String includeScope;
130
131
132
133
134
135
136 @Parameter( property = "excludeScope", defaultValue = "" )
137 protected String excludeScope;
138
139
140
141
142
143
144
145 @Parameter( property = "includeClassifiers", defaultValue = "" )
146 protected String includeClassifiers;
147
148
149
150
151
152
153
154 @Parameter( property = "excludeClassifiers", defaultValue = "" )
155 protected String excludeClassifiers;
156
157
158
159
160
161
162 @Parameter( property = "classifier", defaultValue = "" )
163 protected String classifier;
164
165
166
167
168
169
170
171 @Parameter( property = "type", defaultValue = "" )
172 protected String type;
173
174
175
176
177
178
179 @Parameter( property = "excludeArtifactIds", defaultValue = "" )
180 protected String excludeArtifactIds;
181
182
183
184
185
186
187 @Parameter( property = "includeArtifactIds", defaultValue = "" )
188 protected String includeArtifactIds;
189
190
191
192
193
194
195 @Parameter( property = "excludeGroupIds", defaultValue = "" )
196 protected String excludeGroupIds;
197
198
199
200
201
202
203 @Parameter( property = "includeGroupIds", defaultValue = "" )
204 protected String includeGroupIds;
205
206
207
208
209
210
211 @Parameter( property = "markersDirectory",
212 defaultValue = "${project.build.directory}/dependency-maven-plugin-markers" )
213 protected File markersDirectory;
214
215
216
217
218
219
220 @Parameter( property = "mdep.prependGroupId", defaultValue = "false" )
221 protected boolean prependGroupId = false;
222
223 @Component
224 MavenProjectBuilder projectBuilder;
225
226
227
228
229
230
231 protected abstract ArtifactsFilter getMarkedArtifactFilter();
232
233
234
235
236
237
238
239 protected Set<Artifact> getResolvedDependencies( boolean stopOnFailure )
240 throws MojoExecutionException
241
242 {
243 DependencyStatusSets status = getDependencySets( stopOnFailure );
244
245 return status.getResolvedDependencies();
246 }
247
248 protected DependencyStatusSets getDependencySets( boolean stopOnFailure )
249 throws MojoExecutionException
250 {
251 return getDependencySets( stopOnFailure, false );
252 }
253
254
255
256
257
258
259
260
261
262
263
264 protected DependencyStatusSets getDependencySets( boolean stopOnFailure, boolean includeParents )
265 throws MojoExecutionException
266 {
267
268 FilterArtifacts filter = new FilterArtifacts();
269
270 filter.addFilter( new ProjectTransitivityFilter( project.getDependencyArtifacts(), this.excludeTransitive ) );
271
272 filter.addFilter( new ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
273 DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
274
275 filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
276 DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
277
278 filter.addFilter( new ClassifierFilter( DependencyUtil.cleanToBeTokenizedString( this.includeClassifiers ),
279 DependencyUtil.cleanToBeTokenizedString( this.excludeClassifiers ) ) );
280
281 filter.addFilter( new GroupIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeGroupIds ),
282 DependencyUtil.cleanToBeTokenizedString( this.excludeGroupIds ) ) );
283
284 filter.addFilter( new ArtifactIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeArtifactIds ),
285 DependencyUtil.cleanToBeTokenizedString( this.excludeArtifactIds ) ) );
286
287
288 @SuppressWarnings( "unchecked" ) Set<Artifact> artifacts = project.getArtifacts();
289
290 if ( includeParents )
291 {
292
293 for ( Artifact dep : new ArrayList<Artifact>( artifacts ) )
294 {
295 addParentArtifacts( buildProjectFromArtifact( dep ), artifacts );
296 }
297
298
299 addParentArtifacts( project, artifacts );
300 }
301
302
303 try
304 {
305 artifacts = filter.filter( artifacts );
306 }
307 catch ( ArtifactFilterException e )
308 {
309 throw new MojoExecutionException( e.getMessage(), e );
310 }
311
312
313 DependencyStatusSets status = null;
314 if ( StringUtils.isNotEmpty( classifier ) )
315 {
316 status = getClassifierTranslatedDependencies( artifacts, stopOnFailure );
317 }
318 else
319 {
320 status = filterMarkedDependencies( artifacts );
321 }
322
323 return status;
324 }
325
326 private MavenProject buildProjectFromArtifact( Artifact artifact )
327 throws MojoExecutionException
328 {
329 try
330 {
331 return projectBuilder.buildFromRepository( artifact, remoteRepos, getLocal() );
332 }
333 catch ( ProjectBuildingException e )
334 {
335 throw new MojoExecutionException( e.getMessage(), e );
336 }
337 }
338
339 private void addParentArtifacts( MavenProject project, Set<Artifact> artifacts )
340 throws MojoExecutionException
341 {
342 while ( project.hasParent() )
343 {
344 project = project.getParent();
345
346 if ( project.getArtifact() == null )
347 {
348
349 Artifact artifact =
350 factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(),
351 project.getPackaging() );
352 project.setArtifact( artifact );
353 }
354
355 if ( !artifacts.add( project.getArtifact() ) )
356 {
357
358 break;
359 }
360 try
361 {
362 resolver.resolve( project.getArtifact(), this.remoteRepos, this.getLocal() );
363 }
364 catch ( ArtifactResolutionException e )
365 {
366 throw new MojoExecutionException( e.getMessage(), e );
367 }
368 catch ( ArtifactNotFoundException e )
369 {
370 throw new MojoExecutionException( e.getMessage(), e );
371 }
372 }
373 }
374
375
376
377
378
379
380
381
382
383 protected DependencyStatusSets getClassifierTranslatedDependencies( Set<Artifact> artifacts, boolean stopOnFailure )
384 throws MojoExecutionException
385 {
386 Set<Artifact> unResolvedArtifacts = new HashSet<Artifact>();
387 Set<Artifact> resolvedArtifacts = artifacts;
388 DependencyStatusSets status = new DependencyStatusSets();
389
390
391
392
393 if ( StringUtils.isNotEmpty( classifier ) )
394 {
395 ArtifactTranslator translator = new ClassifierTypeTranslator( this.classifier, this.type, this.factory );
396 artifacts = translator.translate( artifacts, getLog() );
397
398 status = filterMarkedDependencies( artifacts );
399
400
401 artifacts = status.getResolvedDependencies();
402
403
404 ArtifactsResolver artifactsResolver =
405 new DefaultArtifactsResolver( this.resolver, this.getLocal(), this.remoteRepos, stopOnFailure );
406 resolvedArtifacts = artifactsResolver.resolve( artifacts, getLog() );
407
408
409 unResolvedArtifacts.addAll( artifacts );
410 unResolvedArtifacts.removeAll( resolvedArtifacts );
411 }
412
413
414 status.setResolvedDependencies( resolvedArtifacts );
415 status.setUnResolvedDependencies( unResolvedArtifacts );
416
417 return status;
418 }
419
420
421
422
423
424
425
426
427 protected DependencyStatusSets filterMarkedDependencies( Set<Artifact> artifacts )
428 throws MojoExecutionException
429 {
430
431 FilterArtifacts filter = new FilterArtifacts();
432 filter.clearFilters();
433 filter.addFilter( getMarkedArtifactFilter() );
434
435 Set<Artifact> unMarkedArtifacts;
436 try
437 {
438 unMarkedArtifacts = filter.filter( artifacts );
439 }
440 catch ( ArtifactFilterException e )
441 {
442 throw new MojoExecutionException( e.getMessage(), e );
443 }
444
445
446 Set<Artifact> skippedArtifacts = new HashSet<Artifact>();
447 skippedArtifacts.addAll( artifacts );
448 skippedArtifacts.removeAll( unMarkedArtifacts );
449
450 return new DependencyStatusSets( unMarkedArtifacts, null, skippedArtifacts );
451 }
452
453
454
455
456 public File getMarkersDirectory()
457 {
458 return this.markersDirectory;
459 }
460
461
462
463
464 public void setMarkersDirectory( File theMarkersDirectory )
465 {
466 this.markersDirectory = theMarkersDirectory;
467 }
468
469
470
471
472
473
474 public boolean isPrependGroupId()
475 {
476 return prependGroupId;
477 }
478
479
480
481
482
483 public void setPrependGroupId( boolean prependGroupId )
484 {
485 this.prependGroupId = prependGroupId;
486 }
487 }