-
Notifications
You must be signed in to change notification settings - Fork 173
/
AbstractDependencyFilterMojo.java
569 lines (498 loc) · 20.5 KB
/
AbstractDependencyFilterMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package org.apache.maven.plugins.dependency.fromDependencies;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.plugins.dependency.utils.translators.ArtifactTranslator;
import org.apache.maven.plugins.dependency.utils.translators.ClassifierTypeTranslator;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
import org.apache.maven.shared.transfer.repository.RepositoryManager;
import org.codehaus.plexus.util.StringUtils;
/**
* Class that encapsulates the plugin parameters, and contains methods that handle dependency filtering
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
* @see org.apache.maven.plugins.dependency.AbstractDependencyMojo
*/
public abstract class AbstractDependencyFilterMojo
extends AbstractDependencyMojo
{
@Component
private ArtifactResolver artifactResolver;
@Component
private DependencyResolver dependencyResolver;
@Component
private RepositoryManager repositoryManager;
/**
* Overwrite release artifacts
*
* @since 1.0
*/
@Parameter( property = "overWriteReleases", defaultValue = "false" )
protected boolean overWriteReleases;
/**
* Overwrite snapshot artifacts
*
* @since 1.0
*/
@Parameter( property = "overWriteSnapshots", defaultValue = "false" )
protected boolean overWriteSnapshots;
/**
* Overwrite artifacts that don't exist or are older than the source.
*
* @since 2.0
*/
@Parameter( property = "overWriteIfNewer", defaultValue = "true" )
protected boolean overWriteIfNewer;
/**
* If we should exclude transitive dependencies
*
* @since 2.0
*/
@Parameter( property = "excludeTransitive", defaultValue = "false" )
protected boolean excludeTransitive;
/**
* Comma Separated list of Types to include. Empty String indicates include everything (default).
*
* @since 2.0
*/
@Parameter( property = "includeTypes", defaultValue = "" )
protected String includeTypes;
/**
* Comma Separated list of Types to exclude. Empty String indicates don't exclude anything (default).
*
* @since 2.0
*/
@Parameter( property = "excludeTypes", defaultValue = "" )
protected String excludeTypes;
/**
* Scope threshold to include. An empty string indicates include all dependencies (default).<br>
* The scope threshold value being interpreted is the scope as
* Maven filters for creating a classpath, not as specified in the pom. In summary:
* <ul>
* <li><code>runtime</code> include scope gives runtime and compile dependencies,</li>
* <li><code>compile</code> include scope gives compile, provided, and system dependencies,</li>
* <li><code>test</code> include scope gives all dependencies (equivalent to default),</li>
* <li><code>provided</code> include scope just gives provided dependencies,</li>
* <li><code>system</code> include scope just gives system dependencies.</li>
* </ul>
*
* @since 2.0
*/
@Parameter( property = "includeScope", defaultValue = "" )
protected String includeScope;
/**
* Scope threshold to exclude, if no value is defined for include.
* An empty string indicates no dependencies (default).<br>
* The scope threshold value being interpreted is the scope as
* Maven filters for creating a classpath, not as specified in the pom. In summary:
* <ul>
* <li><code>runtime</code> exclude scope excludes runtime and compile dependencies,</li>
* <li><code>compile</code> exclude scope excludes compile, provided, and system dependencies,</li>
* <li><code>test</code> exclude scope excludes all dependencies, then not really a legitimate option: it will
* fail, you probably meant to configure includeScope = compile</li>
* <li><code>provided</code> exclude scope just excludes provided dependencies,</li>
* <li><code>system</code> exclude scope just excludes system dependencies.</li>
* </ul>
*
* @since 2.0
*/
@Parameter( property = "excludeScope", defaultValue = "" )
protected String excludeScope;
/**
* Comma Separated list of Classifiers to include. Empty String indicates include everything (default).
*
* @since 2.0
*/
@Parameter( property = "includeClassifiers", defaultValue = "" )
protected String includeClassifiers;
/**
* Comma Separated list of Classifiers to exclude. Empty String indicates don't exclude anything (default).
*
* @since 2.0
*/
@Parameter( property = "excludeClassifiers", defaultValue = "" )
protected String excludeClassifiers;
/**
* Specify classifier to look for. Example: sources
*
* @since 2.0
*/
@Parameter( property = "classifier", defaultValue = "" )
protected String classifier;
/**
* Specify type to look for when constructing artifact based on classifier. Example: java-source,jar,war
*
* @since 2.0
*/
@Parameter( property = "type", defaultValue = "" )
protected String type;
/**
* Comma separated list of Artifact names to exclude.
*
* @since 2.0
*/
@Parameter( property = "excludeArtifactIds", defaultValue = "" )
protected String excludeArtifactIds;
/**
* Comma separated list of Artifact names to include. Empty String indicates include everything (default).
*
* @since 2.0
*/
@Parameter( property = "includeArtifactIds", defaultValue = "" )
protected String includeArtifactIds;
/**
* Comma separated list of GroupId Names to exclude.
*
* @since 2.0
*/
@Parameter( property = "excludeGroupIds", defaultValue = "" )
protected String excludeGroupIds;
/**
* Comma separated list of GroupIds to include. Empty String indicates include everything (default).
*
* @since 2.0
*/
@Parameter( property = "includeGroupIds", defaultValue = "" )
protected String includeGroupIds;
/**
* Directory to store flag files
*
* @since 2.0
*/
//CHECKSTYLE_OFF: LineLength
@Parameter( property = "markersDirectory", defaultValue = "${project.build.directory}/dependency-maven-plugin-markers" )
//CHECKSTYLE_ON: LineLength
protected File markersDirectory;
/**
* Prepend the groupId during copy.
*
* @since 2.2
*/
@Parameter( property = "mdep.prependGroupId", defaultValue = "false" )
protected boolean prependGroupId = false;
@Component
private ProjectBuilder projectBuilder;
@Component
private ArtifactHandlerManager artifactHandlerManager;
/**
* Return an {@link ArtifactsFilter} indicating which artifacts must be filtered out.
*
* @return an {@link ArtifactsFilter} indicating which artifacts must be filtered out.
*/
protected abstract ArtifactsFilter getMarkedArtifactFilter();
/**
* Retrieves dependencies, either direct only or all including transitive.
*
* @param stopOnFailure true to fail if resolution does not work or false not to fail.
* @return A set of artifacts
* @throws MojoExecutionException in case of errors.
*/
protected Set<Artifact> getResolvedDependencies( boolean stopOnFailure )
throws MojoExecutionException
{
DependencyStatusSets status = getDependencySets( stopOnFailure );
return status.getResolvedDependencies();
}
/**
* @param stopOnFailure true/false.
* @return {@link DependencyStatusSets}
* @throws MojoExecutionException in case of an error.
*/
protected DependencyStatusSets getDependencySets( boolean stopOnFailure )
throws MojoExecutionException
{
return getDependencySets( stopOnFailure, false );
}
/**
* Method creates filters and filters the projects dependencies. This method also transforms the dependencies if
* classifier is set. The dependencies are filtered in least specific to most specific order
*
* @param stopOnFailure true to fail if artifacts can't be resolved false otherwise.
* @param includeParents <code>true</code> if parents should be included or not <code>false</code>.
* @return DependencyStatusSets - Bean of TreeSets that contains information on the projects dependencies
* @throws MojoExecutionException in case of errors.
*/
protected DependencyStatusSets getDependencySets( boolean stopOnFailure, boolean includeParents )
throws MojoExecutionException
{
// add filters in well known order, least specific to most specific
FilterArtifacts filter = new FilterArtifacts();
filter.addFilter( new ProjectTransitivityFilter( getProject().getDependencyArtifacts(),
this.excludeTransitive ) );
if ( "test".equals( this.excludeScope ) )
{
throw new MojoExecutionException( "Excluding every artifact inside 'test' resolution scope means "
+ "excluding everything: you probably want includeScope='compile', "
+ "read parameters documentation for detailed explanations" );
}
filter.addFilter( new ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
filter.addFilter( new ClassifierFilter( DependencyUtil.cleanToBeTokenizedString( this.includeClassifiers ),
DependencyUtil.cleanToBeTokenizedString( this.excludeClassifiers ) ) );
filter.addFilter( new GroupIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeGroupIds ),
DependencyUtil.cleanToBeTokenizedString( this.excludeGroupIds ) ) );
filter.addFilter( new ArtifactIdFilter( DependencyUtil.cleanToBeTokenizedString( this.includeArtifactIds ),
DependencyUtil.cleanToBeTokenizedString( this.excludeArtifactIds ) ) );
// start with all artifacts.
Set<Artifact> artifacts = getProject().getArtifacts();
if ( includeParents )
{
// add dependencies parents
for ( Artifact dep : new ArrayList<>( artifacts ) )
{
addParentArtifacts( buildProjectFromArtifact( dep ), artifacts );
}
// add current project parent
addParentArtifacts( getProject(), artifacts );
}
// perform filtering
try
{
artifacts = filter.filter( artifacts );
}
catch ( ArtifactFilterException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
// transform artifacts if classifier is set
DependencyStatusSets status;
if ( StringUtils.isNotEmpty( classifier ) )
{
status = getClassifierTranslatedDependencies( artifacts, stopOnFailure );
}
else
{
status = filterMarkedDependencies( artifacts );
}
return status;
}
private MavenProject buildProjectFromArtifact( Artifact artifact )
throws MojoExecutionException
{
try
{
return projectBuilder.build( artifact, session.getProjectBuildingRequest() ).getProject();
}
catch ( ProjectBuildingException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
private void addParentArtifacts( MavenProject project, Set<Artifact> artifacts )
throws MojoExecutionException
{
while ( project.hasParent() )
{
project = project.getParent();
if ( artifacts.contains( project.getArtifact() ) )
{
// artifact already in the set
break;
}
try
{
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
Artifact resolvedArtifact =
artifactResolver.resolveArtifact( buildingRequest, project.getArtifact() ).getArtifact();
artifacts.add( resolvedArtifact );
}
catch ( ArtifactResolverException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
}
/**
* Transform artifacts
*
* @param artifacts set of artifacts {@link Artifact}.
* @param stopOnFailure true/false.
* @return DependencyStatusSets - Bean of TreeSets that contains information on the projects dependencies
* @throws MojoExecutionException in case of an error.
*/
protected DependencyStatusSets getClassifierTranslatedDependencies( Set<Artifact> artifacts, boolean stopOnFailure )
throws MojoExecutionException
{
Set<Artifact> unResolvedArtifacts = new LinkedHashSet<>();
Set<Artifact> resolvedArtifacts = artifacts;
DependencyStatusSets status = new DependencyStatusSets();
// possibly translate artifacts into a new set of artifacts based on the
// classifier and type
// if this did something, we need to resolve the new artifacts
if ( StringUtils.isNotEmpty( classifier ) )
{
ArtifactTranslator translator =
new ClassifierTypeTranslator( artifactHandlerManager, this.classifier, this.type );
Collection<ArtifactCoordinate> coordinates = translator.translate( artifacts, getLog() );
status = filterMarkedDependencies( artifacts );
// the unskipped artifacts are in the resolved set.
artifacts = status.getResolvedDependencies();
// resolve the rest of the artifacts
resolvedArtifacts = resolve( new LinkedHashSet<>( coordinates ), stopOnFailure );
// calculate the artifacts not resolved.
unResolvedArtifacts.addAll( artifacts );
unResolvedArtifacts.removeAll( resolvedArtifacts );
}
// return a bean of all 3 sets.
status.setResolvedDependencies( resolvedArtifacts );
status.setUnResolvedDependencies( unResolvedArtifacts );
return status;
}
/**
* Filter the marked dependencies
*
* @param artifacts The artifacts set {@link Artifact}.
* @return status set {@link DependencyStatusSets}.
* @throws MojoExecutionException in case of an error.
*/
protected DependencyStatusSets filterMarkedDependencies( Set<Artifact> artifacts )
throws MojoExecutionException
{
// remove files that have markers already
FilterArtifacts filter = new FilterArtifacts();
filter.clearFilters();
filter.addFilter( getMarkedArtifactFilter() );
Set<Artifact> unMarkedArtifacts;
try
{
unMarkedArtifacts = filter.filter( artifacts );
}
catch ( ArtifactFilterException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
// calculate the skipped artifacts
Set<Artifact> skippedArtifacts = new LinkedHashSet<>();
skippedArtifacts.addAll( artifacts );
skippedArtifacts.removeAll( unMarkedArtifacts );
return new DependencyStatusSets( unMarkedArtifacts, null, skippedArtifacts );
}
/**
* @param coordinates The set of artifact coordinates{@link ArtifactCoordinate}.
* @param stopOnFailure <code>true</code> if we should fail with exception if an artifact couldn't be resolved
* <code>false</code> otherwise.
* @return the resolved artifacts. {@link Artifact}.
* @throws MojoExecutionException in case of error.
*/
protected Set<Artifact> resolve( Set<ArtifactCoordinate> coordinates, boolean stopOnFailure )
throws MojoExecutionException
{
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
Set<Artifact> resolvedArtifacts = new LinkedHashSet<>();
for ( ArtifactCoordinate coordinate : coordinates )
{
try
{
Artifact artifact = artifactResolver.resolveArtifact( buildingRequest, coordinate ).getArtifact();
resolvedArtifacts.add( artifact );
}
catch ( ArtifactResolverException ex )
{
// an error occurred during resolution, log it an continue
getLog().debug( "error resolving: " + coordinate );
getLog().debug( ex );
if ( stopOnFailure )
{
throw new MojoExecutionException( "error resolving: " + coordinate, ex );
}
}
}
return resolvedArtifacts;
}
/**
* @return Returns the markersDirectory.
*/
public File getMarkersDirectory()
{
return this.markersDirectory;
}
/**
* @param theMarkersDirectory The markersDirectory to set.
*/
public void setMarkersDirectory( File theMarkersDirectory )
{
this.markersDirectory = theMarkersDirectory;
}
// TODO: Set marker files.
/**
* @return true, if the groupId should be prepended to the filename.
*/
public boolean isPrependGroupId()
{
return prependGroupId;
}
/**
* @param prependGroupId - true if the groupId must be prepended during the copy.
*/
public void setPrependGroupId( boolean prependGroupId )
{
this.prependGroupId = prependGroupId;
}
/**
* @return {@link #artifactResolver}
*/
protected final ArtifactResolver getArtifactResolver()
{
return artifactResolver;
}
/**
* @return {@link #dependencyResolver}
*/
protected final DependencyResolver getDependencyResolver()
{
return dependencyResolver;
}
/**
* @return {@link #repositoryManager}
*/
protected final RepositoryManager getRepositoryManager()
{
return repositoryManager;
}
}