28
28
import java .util .Objects ;
29
29
import java .util .Set ;
30
30
import java .util .TreeSet ;
31
+ import java .util .stream .Collectors ;
31
32
32
33
import org .apache .maven .artifact .ArtifactUtils ;
33
34
import org .apache .maven .artifact .metadata .ArtifactMetadataRetrievalException ;
49
50
import org .codehaus .mojo .versions .utils .DependencyComparator ;
50
51
import org .codehaus .plexus .util .StringUtils ;
51
52
53
+ import static org .apache .commons .lang3 .StringUtils .countMatches ;
54
+
52
55
/**
53
56
* Displays all dependencies that have newer versions available.
54
57
* It will also display dependencies which are used by a plugin or
@@ -107,7 +110,7 @@ public class DisplayDependencyUpdatesMojo
107
110
* </p>
108
111
*
109
112
* <p>
110
- * Example: "mygroup:artifact:*,*:*:*:*:*:compile"
113
+ * Example: {@code "mygroup:artifact:*,*:*:*:*:*:compile"}
111
114
* </p>
112
115
*
113
116
* @since 2.12.0
@@ -129,7 +132,7 @@ public class DisplayDependencyUpdatesMojo
129
132
* </p>
130
133
*
131
134
* <p>
132
- * Example: "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"
135
+ * Example: {@code "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"}
133
136
* </p>
134
137
*
135
138
* @since 2.12.0
@@ -159,7 +162,7 @@ public class DisplayDependencyUpdatesMojo
159
162
* </p>
160
163
*
161
164
* <p>
162
- * Example: "mygroup:artifact:*,*:*:*:*:*:compile"
165
+ * Example: {@code "mygroup:artifact:*,*:*:*:*:*:compile"}
163
166
* </p>
164
167
*
165
168
* @since 2.12.0
@@ -181,7 +184,7 @@ public class DisplayDependencyUpdatesMojo
181
184
* </p>
182
185
*
183
186
* <p>
184
- * Example: "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"
187
+ * Example: {@code "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"}
185
188
* </p>
186
189
*
187
190
* @since 2.12.0
@@ -257,6 +260,77 @@ public class DisplayDependencyUpdatesMojo
257
260
@ Parameter ( property = "verbose" , defaultValue = "false" )
258
261
private boolean verbose ;
259
262
263
+ /**
264
+ * <p>Only take these artifacts into consideration:<br/>
265
+ * Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
266
+ *
267
+ * <p>
268
+ * The wildcard "*" can be used as the only, first, last or both characters in each token.
269
+ * The version token does support version ranges.
270
+ * </p>
271
+ *
272
+ * <p>
273
+ * Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
274
+ * </p>
275
+ *
276
+ * @since 2.12.0
277
+ */
278
+ @ Parameter ( property = "pluginDependencyIncludes" , defaultValue = WildcardMatcher .WILDCARD )
279
+ private List <String > pluginDependencyIncludes ;
280
+
281
+ /**
282
+ * <p>Exclude these artifacts into consideration:<br/>
283
+ * Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
284
+ *
285
+ * <p>
286
+ * The wildcard "*" can be used as the only, first, last or both characters in each token.
287
+ * The version token does support version ranges.
288
+ * </p>
289
+ *
290
+ * <p>
291
+ * Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
292
+ * </p>
293
+ *
294
+ * @since 2.12.0
295
+ */
296
+ @ Parameter ( property = "pluginDependencyExcludes" )
297
+ private List <String > pluginDependencyExcludes ;
298
+
299
+ /**
300
+ * <p>Only take these artifacts into consideration:<br/>
301
+ * Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
302
+
303
+ * The wildcard "*" can be used as the only, first, last or both characters in each token.
304
+ * The version token does support version ranges.
305
+ * </p>
306
+ *
307
+ * <p>
308
+ * Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
309
+ * </p>
310
+ *
311
+ * @since 2.12.0
312
+ */
313
+ @ Parameter ( property = "pluginManagementDependencyIncludes" , defaultValue = WildcardMatcher .WILDCARD )
314
+ private List <String > pluginManagementDependencyIncludes ;
315
+
316
+ /**
317
+ * <p>Exclude these artifacts into consideration:<br/>
318
+ * Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
319
+ *
320
+ * <p>
321
+ * The wildcard "*" can be used as the only, first, last or both characters in each token.
322
+ * The version token does support version ranges.
323
+ * </p>
324
+ *
325
+ * <p>
326
+ * Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
327
+ * </p>
328
+ *
329
+ * @since 2.12.0
330
+ */
331
+ @ Parameter ( property = "pluginManagementDependencyExcludes" )
332
+ private List <String > pluginManagementDependencyExcludes ;
333
+
260
334
// --------------------- GETTER / SETTER METHODS ---------------------
261
335
262
336
private static Set <Dependency > extractPluginDependenciesFromPluginsInPluginManagement ( Build build )
@@ -392,6 +466,8 @@ public void execute()
392
466
{
393
467
logInit ();
394
468
469
+ validateInput ();
470
+
395
471
Set <Dependency > dependencyManagement = new TreeSet <>( new DependencyComparator () );
396
472
DependencyManagement projectDependencyManagement = getProjectDependencyManagement ( getProject () );
397
473
if ( projectDependencyManagement != null )
@@ -480,11 +556,16 @@ public void execute()
480
556
}
481
557
if ( isProcessPluginDependenciesInDependencyManagement () )
482
558
{
559
+ pluginDependenciesInPluginManagement =
560
+ filterPluginManagementIncludes ( pluginDependenciesInPluginManagement );
561
+
483
562
logUpdates ( getHelper ().lookupDependenciesUpdates ( pluginDependenciesInPluginManagement , false ),
484
563
"pluginManagement of plugins" );
485
564
}
486
565
if ( isProcessingPluginDependencies () )
487
566
{
567
+ pluginDependencies = filterPluginDependencyIncludes ( pluginDependencies );
568
+
488
569
logUpdates ( getHelper ().lookupDependenciesUpdates ( pluginDependencies , false ), "Plugin Dependencies" );
489
570
}
490
571
}
@@ -494,15 +575,56 @@ public void execute()
494
575
}
495
576
}
496
577
578
+ private void validateInput () throws MojoExecutionException
579
+ {
580
+ validateGAVList ( dependencyIncludes , 6 , "dependencyIncludes" );
581
+ validateGAVList ( dependencyExcludes , 6 , "dependencyExcludes" );
582
+ validateGAVList ( dependencyManagementIncludes , 6 , "dependencyManagementIncludes" );
583
+ validateGAVList ( dependencyManagementIncludes , 6 , "dependencyManagementExcludes" );
584
+ validateGAVList ( pluginDependencyIncludes , 3 , "pluginDependencyIncludes" );
585
+ validateGAVList ( pluginDependencyExcludes , 3 , "pluginDependencyExcludes" );
586
+ validateGAVList ( pluginManagementDependencyIncludes , 3 , "pluginManagementDependencyIncludes" );
587
+ validateGAVList ( pluginManagementDependencyExcludes , 3 , "pluginManagementDependencyExcludes" );
588
+ }
589
+
590
+ /**
591
+ * Validates a list of GAV strings
592
+ * @param gavList list of the input GAV strings
593
+ * @param numSections number of sections in the GAV to verify against
594
+ * @param argumentName argument name to indicate in the exception
595
+ * @throws MojoExecutionException if the argument is invalid
596
+ */
597
+ static void validateGAVList ( List <String > gavList , int numSections , String argumentName )
598
+ throws MojoExecutionException
599
+ {
600
+ if ( gavList != null && gavList .stream ().anyMatch ( gav -> countMatches ( gav , ":" ) >= numSections ) )
601
+ {
602
+ throw new MojoExecutionException ( argumentName + " should not contain more than 6 segments" );
603
+ }
604
+ }
605
+
497
606
private Set <Dependency > filterDependencyIncludes ( Set <Dependency > dependencies )
498
607
{
499
- return filterDependencies ( dependencies , dependencyIncludes , dependencyExcludes , "dependencies " );
608
+ return filterDependencies ( dependencies , dependencyIncludes , dependencyExcludes , "Dependencies " );
500
609
}
501
610
502
611
private Set <Dependency > filterDependencyManagementIncludes ( Set <Dependency > dependencyManagement )
503
612
{
504
613
return filterDependencies ( dependencyManagement ,
505
- dependencyManagementIncludes , dependencyManagementExcludes , "dependecyManagement" );
614
+ dependencyManagementIncludes , dependencyManagementExcludes , "Dependecy Management" );
615
+ }
616
+
617
+ private Set <Dependency > filterPluginDependencyIncludes ( Set <Dependency > dependencies )
618
+ {
619
+ return filterDependencies ( dependencies , pluginDependencyIncludes , pluginDependencyExcludes ,
620
+ "Plugin Dependencies" );
621
+ }
622
+
623
+ private Set <Dependency > filterPluginManagementIncludes ( Set <Dependency > dependencyManagement )
624
+ {
625
+ return filterDependencies ( dependencyManagement ,
626
+ pluginManagementDependencyIncludes , pluginManagementDependencyExcludes ,
627
+ "Plugin Management Dependencies" );
506
628
}
507
629
508
630
private Set <Dependency > filterDependencies (
@@ -515,15 +637,26 @@ private Set<Dependency> filterDependencies(
515
637
DependencyFilter includeDeps = DependencyFilter .parseFrom ( includes );
516
638
DependencyFilter excludeDeps = DependencyFilter .parseFrom ( excludes );
517
639
518
- getLog (). debug ( String . format ( "parsed includes in %s: %s -> %s" , section , includes , includeDeps ) );
519
- getLog (). debug ( String . format ( "parsed excludes in %s: %s -> %s" , section , excludes , excludeDeps ) );
640
+ Set < Dependency > filtered = includeDeps . retainingIn ( dependencies );
641
+ filtered = excludeDeps . removingFrom ( filtered );
520
642
521
- Set <Dependency > onlyIncludes = includeDeps .retainingIn ( dependencies );
522
- Set <Dependency > filtered = excludeDeps .removingFrom ( onlyIncludes );
643
+ if ( getLog ().isDebugEnabled () )
644
+ {
645
+ getLog ().debug ( String .format ( "parsed includes in %s: %s -> %s" , section , includes , includeDeps ) );
646
+ getLog ().debug ( String .format ( "parsed excludes in %s: %s -> %s" , section , excludes , excludeDeps ) );
647
+ getLog ().debug ( String .format ( "Unfiltered %s: " , section ) + output ( dependencies ) );
648
+ getLog ().debug ( String .format ( "Filtered %s: " , section ) + output ( filtered ) );
649
+ }
523
650
524
651
return filtered ;
525
652
}
526
653
654
+ private String output ( Set <Dependency > dependencies )
655
+ {
656
+ return dependencies .stream ()
657
+ .map ( d -> String .format ( "%s:%s:%s" , d .getGroupId (), d .getArtifactId (), d .getVersion () ) )
658
+ .collect ( Collectors .joining ( ", " ) );
659
+ }
527
660
private DependencyManagement getProjectDependencyManagement ( MavenProject project )
528
661
{
529
662
if ( processDependencyManagementTransitive )
0 commit comments