33
33
import org .gradle .api .Project ;
34
34
import org .gradle .api .artifacts .Configuration ;
35
35
import org .gradle .api .artifacts .Dependency ;
36
- import org .gradle .api .artifacts .dsl .DependencyHandler ;
37
36
import org .gradle .api .artifacts .result .DependencyResult ;
38
37
39
38
import org .springframework .boot .build .bom .bomr .version .DependencyVersion ;
@@ -398,17 +397,10 @@ public static class VersionAlignment {
398
397
}
399
398
400
399
public Set <String > resolve () {
401
- if (this .managedBy == null ) {
402
- throw new IllegalStateException ("Version alignment without managedBy is not supported" );
403
- }
404
400
if (this .alignedVersions != null ) {
405
401
return this .alignedVersions ;
406
402
}
407
- Library managingLibrary = this .libraries .stream ()
408
- .filter ((candidate ) -> this .managedBy .equals (candidate .getName ()))
409
- .findFirst ()
410
- .orElseThrow (() -> new IllegalStateException ("Managing library '" + this .managedBy + "' not found." ));
411
- Map <String , String > versions = resolveAligningDependencies (managingLibrary );
403
+ Map <String , String > versions = resolveAligningDependencies ();
412
404
Set <String > versionsInLibrary = new HashSet <>();
413
405
for (Group group : this .groups ) {
414
406
for (Module module : group .getModules ()) {
@@ -428,18 +420,8 @@ public Set<String> resolve() {
428
420
return this .alignedVersions ;
429
421
}
430
422
431
- private Map <String , String > resolveAligningDependencies (Library manager ) {
432
- DependencyHandler dependencyHandler = this .project .getDependencies ();
433
- List <Dependency > boms = manager .getGroups ()
434
- .stream ()
435
- .flatMap ((group ) -> group .getBoms ()
436
- .stream ()
437
- .map ((bom ) -> dependencyHandler
438
- .platform (group .getId () + ":" + bom + ":" + manager .getVersion ().getVersion ())))
439
- .toList ();
440
- List <Dependency > dependencies = new ArrayList <>();
441
- dependencies .addAll (boms );
442
- dependencies .add (dependencyHandler .create (this .from ));
423
+ private Map <String , String > resolveAligningDependencies () {
424
+ List <Dependency > dependencies = getAligningDependencies ();
443
425
Configuration alignmentConfiguration = this .project .getConfigurations ()
444
426
.detachedConfiguration (dependencies .toArray (new Dependency [0 ]));
445
427
Map <String , String > versions = new HashMap <>();
@@ -452,6 +434,58 @@ private Map<String, String> resolveAligningDependencies(Library manager) {
452
434
return versions ;
453
435
}
454
436
437
+ private List <Dependency > getAligningDependencies () {
438
+ if (this .managedBy == null ) {
439
+ Library fromLibrary = findFromLibrary ();
440
+ return List
441
+ .of (this .project .getDependencies ().create (this .from + ":" + fromLibrary .getVersion ().getVersion ()));
442
+ }
443
+ else {
444
+ Library managingLibrary = findManagingLibrary ();
445
+ List <Dependency > boms = getBomDependencies (managingLibrary );
446
+ List <Dependency > dependencies = new ArrayList <>();
447
+ dependencies .addAll (boms );
448
+ dependencies .add (this .project .getDependencies ().create (this .from ));
449
+ return dependencies ;
450
+ }
451
+ }
452
+
453
+ private Library findFromLibrary () {
454
+ for (Library library : this .libraries ) {
455
+ for (Group group : library .getGroups ()) {
456
+ for (Module module : group .getModules ()) {
457
+ if (this .from .equals (group .getId () + ":" + module .getName ())) {
458
+ return library ;
459
+ }
460
+ }
461
+ }
462
+ }
463
+ return null ;
464
+ }
465
+
466
+ private Library findManagingLibrary () {
467
+ if (this .managedBy == null ) {
468
+ return null ;
469
+ }
470
+ return this .libraries .stream ()
471
+ .filter ((candidate ) -> this .managedBy .equals (candidate .getName ()))
472
+ .findFirst ()
473
+ .orElseThrow (() -> new IllegalStateException ("Managing library '" + this .managedBy + "' not found." ));
474
+ }
475
+
476
+ private List <Dependency > getBomDependencies (Library manager ) {
477
+ if (manager == null ) {
478
+ return Collections .emptyList ();
479
+ }
480
+ return manager .getGroups ()
481
+ .stream ()
482
+ .flatMap ((group ) -> group .getBoms ()
483
+ .stream ()
484
+ .map ((bom ) -> this .project .getDependencies ()
485
+ .platform (group .getId () + ":" + bom + ":" + manager .getVersion ().getVersion ())))
486
+ .toList ();
487
+ }
488
+
455
489
String getFrom () {
456
490
return this .from ;
457
491
}
@@ -462,7 +496,11 @@ String getManagedBy() {
462
496
463
497
@ Override
464
498
public String toString () {
465
- return "version from dependencies of " + this .from + " that is managed by " + this .managedBy ;
499
+ String result = "version from dependencies of " + this .from ;
500
+ if (this .managedBy != null ) {
501
+ result += " that is managed by " + this .managedBy ;
502
+ }
503
+ return result ;
466
504
}
467
505
468
506
}
0 commit comments