25
25
import java .util .ArrayList ;
26
26
import java .util .Arrays ;
27
27
import java .util .Date ;
28
+ import java .util .HashMap ;
29
+ import java .util .HashSet ;
28
30
import java .util .LinkedHashSet ;
29
31
import java .util .LinkedList ;
30
32
import java .util .List ;
31
33
import java .util .Map ;
32
34
import java .util .Objects ;
35
+ import java .util .Optional ;
33
36
import java .util .Set ;
34
37
import java .util .SortedMap ;
35
38
import java .util .TimeZone ;
36
39
import java .util .TreeMap ;
37
40
import java .util .regex .Pattern ;
38
41
42
+ import org .apache .commons .lang3 .tuple .ImmutablePair ;
43
+ import org .apache .commons .lang3 .tuple .Pair ;
39
44
import org .apache .maven .artifact .ArtifactUtils ;
40
45
import org .apache .maven .model .Model ;
41
- import org .apache .maven .model .Parent ;
42
46
import org .apache .maven .plugin .MojoExecutionException ;
43
47
import org .apache .maven .plugin .MojoFailureException ;
44
48
import org .apache .maven .plugins .annotations .Mojo ;
@@ -334,6 +338,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
334
338
Map <File , Model > reactorModels = PomHelper .getChildModels (project , getLog ());
335
339
final SortedMap <File , Model > reactor = new TreeMap <>(new ReactorDepthComparator (reactorModels ));
336
340
reactor .putAll (reactorModels );
341
+ final Map <Pair <String , String >, Set <Model >> children = computeChildren (reactorModels );
337
342
338
343
// set of files to update
339
344
final Set <File > files = new LinkedHashSet <>();
@@ -379,8 +384,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
379
384
|| oldVersionIdRegex .matcher (mVersion ).matches ())
380
385
&& !newVersion .equals (mVersion )) {
381
386
applyChange (
382
- project ,
383
387
reactor ,
388
+ children ,
384
389
files ,
385
390
mGroupId ,
386
391
m .getArtifactId (),
@@ -407,6 +412,34 @@ public void execute() throws MojoExecutionException, MojoFailureException {
407
412
}
408
413
}
409
414
415
+ static Map <Pair <String , String >, Set <Model >> computeChildren (Map <File , Model > reactor ) {
416
+ return reactor .values ().stream ()
417
+ .filter (v -> v .getParent () != null )
418
+ .reduce (
419
+ new HashMap <>(),
420
+ (map , child ) -> {
421
+ map .compute (
422
+ new ImmutablePair <>(
423
+ child .getParent ().getGroupId (),
424
+ child .getParent ().getArtifactId ()),
425
+ (pair , set ) -> Optional .ofNullable (set )
426
+ .map (children -> {
427
+ children .add (child );
428
+ return children ;
429
+ })
430
+ .orElse (new HashSet <Model >() {
431
+ {
432
+ add (child );
433
+ }
434
+ }));
435
+ return map ;
436
+ },
437
+ (m1 , m2 ) -> {
438
+ m1 .putAll (m2 );
439
+ return m1 ;
440
+ });
441
+ }
442
+
410
443
/**
411
444
* Returns the incremented version, with the nextSnapshotIndexToIncrement indicating the 1-based index,
412
445
* from the left, or the most major version component, of the version string.
@@ -436,28 +469,21 @@ protected String getIncrementedVersion(String version, Integer nextSnapshotIndex
436
469
return StringUtils .join (numbers .toArray (new String [0 ]), "." ) + "-SNAPSHOT" ;
437
470
}
438
471
439
- private static String fixNullOrEmpty (String value , String defaultValue ) {
440
- return StringUtils .isBlank (value ) ? defaultValue : value ;
441
- }
442
-
443
472
private void applyChange (
444
- MavenProject project ,
445
473
SortedMap <File , Model > reactor ,
474
+ Map <Pair <String , String >, Set <Model >> children ,
446
475
Set <File > files ,
447
476
String groupId ,
448
477
String artifactId ,
449
478
String oldVersion ) {
450
479
451
480
getLog ().debug ("Applying change " + groupId + ":" + artifactId + ":" + oldVersion + " -> " + newVersion );
452
- // this is a triggering change
453
481
addChange (groupId , artifactId , oldVersion , newVersion );
454
- // now fake out the triggering change
455
482
456
- Map .Entry <File , Model > current = PomHelper .getModelEntry (reactor , groupId , artifactId );
457
- if (current != null ) {
458
- current .getValue ().setVersion (newVersion );
459
- files .add (current .getValue ().getPomFile ());
460
- }
483
+ Optional .ofNullable (PomHelper .getModelEntry (reactor , groupId , artifactId ))
484
+ .map (Map .Entry ::getValue )
485
+ .map (Model ::getPomFile )
486
+ .ifPresent (files ::add );
461
487
462
488
for (Map .Entry <File , Model > sourceEntry : reactor .entrySet ()) {
463
489
final File sourcePath = sourceEntry .getKey ();
@@ -488,50 +514,27 @@ private void applyChange(
488
514
489
515
getLog ().debug ("Looking for modules which use "
490
516
+ ArtifactUtils .versionlessKey (sourceGroupId , sourceArtifactId ) + " as their parent" );
491
-
492
- for (Map .Entry <File , Model > stringModelEntry : processAllModules
493
- ? reactor .entrySet ()
494
- : PomHelper .getChildModels (reactor , sourceGroupId , sourceArtifactId )
495
- .entrySet ()) {
496
- final Model targetModel = stringModelEntry .getValue ();
497
- final Parent parent = targetModel .getParent ();
498
- getLog ().debug ("Module: " + stringModelEntry .getKey ());
499
- if (parent != null && sourceVersion .equals (parent .getVersion ())) {
500
- getLog ().debug (" parent already is "
501
- + ArtifactUtils .versionlessKey (sourceGroupId , sourceArtifactId ) + ":" + sourceVersion );
502
- } else {
503
- getLog ().debug (" parent is " + ArtifactUtils .versionlessKey (sourceGroupId , sourceArtifactId )
504
- + ":" + (parent == null ? "" : parent .getVersion ()));
505
- getLog ().debug (" will become " + ArtifactUtils .versionlessKey (sourceGroupId , sourceArtifactId )
506
- + ":" + sourceVersion );
507
- }
508
- final boolean targetExplicit = PomHelper .isExplicitVersion (targetModel );
509
- if ((updateMatchingVersions || !targetExplicit ) //
510
- && (parent != null
511
- && StringUtils .equals (parent .getVersion (), PomHelper .getVersion (targetModel )))) {
512
- getLog ().debug (" module is "
513
- + ArtifactUtils .versionlessKey (
514
- PomHelper .getGroupId (targetModel ), PomHelper .getArtifactId (targetModel ))
515
- + ":"
516
- + PomHelper .getVersion (targetModel ));
517
- getLog ().debug (" will become "
518
- + ArtifactUtils .versionlessKey (
519
- PomHelper .getGroupId (targetModel ), PomHelper .getArtifactId (targetModel ))
520
- + ":" + sourceVersion );
521
- addChange (
522
- PomHelper .getGroupId (targetModel ),
523
- PomHelper .getArtifactId (targetModel ),
524
- PomHelper .getVersion (targetModel ),
525
- sourceVersion );
526
- targetModel .setVersion (sourceVersion );
527
- } else {
528
- getLog ().debug (" module is "
529
- + ArtifactUtils .versionlessKey (
530
- PomHelper .getGroupId (targetModel ), PomHelper .getArtifactId (targetModel ))
531
- + ":"
532
- + PomHelper .getVersion (targetModel ));
533
- }
534
- }
517
+ Optional .ofNullable (children .get (new ImmutablePair <>(sourceGroupId , sourceArtifactId )))
518
+ .ifPresent (set -> set .forEach (model -> {
519
+ final boolean hasExplicitVersion = PomHelper .isExplicitVersion (model );
520
+ if ((updateMatchingVersions || !hasExplicitVersion )
521
+ && (StringUtils .equals (sourceVersion , PomHelper .getVersion (model )))) {
522
+ getLog ().debug (" module is "
523
+ + ArtifactUtils .versionlessKey (
524
+ PomHelper .getGroupId (model ), PomHelper .getArtifactId (model ))
525
+ + ":"
526
+ + PomHelper .getVersion (model ));
527
+ getLog ().debug (" will become "
528
+ + ArtifactUtils .versionlessKey (
529
+ PomHelper .getGroupId (model ), PomHelper .getArtifactId (model ))
530
+ + ":" + newVersion );
531
+ addChange (
532
+ PomHelper .getGroupId (model ),
533
+ PomHelper .getArtifactId (model ),
534
+ PomHelper .getVersion (model ),
535
+ newVersion );
536
+ }
537
+ }));
535
538
}
536
539
}
537
540
0 commit comments