30
30
import org .gradle .api .Project ;
31
31
import org .gradle .api .artifacts .Configuration ;
32
32
import org .gradle .api .artifacts .Dependency ;
33
- import org .gradle .api .artifacts .dsl .DependencyHandler ;
34
33
import org .gradle .api .artifacts .result .DependencyResult ;
35
34
36
35
import org .springframework .boot .build .bom .bomr .version .DependencyVersion ;
@@ -334,17 +333,10 @@ public static class VersionAlignment {
334
333
}
335
334
336
335
public Set <String > resolve () {
337
- if (this .managedBy == null ) {
338
- throw new IllegalStateException ("Version alignment without managedBy is not supported" );
339
- }
340
336
if (this .alignedVersions != null ) {
341
337
return this .alignedVersions ;
342
338
}
343
- Library managingLibrary = this .libraries .stream ()
344
- .filter ((candidate ) -> this .managedBy .equals (candidate .getName ()))
345
- .findFirst ()
346
- .orElseThrow (() -> new IllegalStateException ("Managing library '" + this .managedBy + "' not found." ));
347
- Map <String , String > versions = resolveAligningDependencies (managingLibrary );
339
+ Map <String , String > versions = resolveAligningDependencies ();
348
340
Set <String > versionsInLibrary = new HashSet <>();
349
341
for (Group group : this .groups ) {
350
342
for (Module module : group .getModules ()) {
@@ -364,18 +356,8 @@ public Set<String> resolve() {
364
356
return this .alignedVersions ;
365
357
}
366
358
367
- private Map <String , String > resolveAligningDependencies (Library manager ) {
368
- DependencyHandler dependencyHandler = this .project .getDependencies ();
369
- List <Dependency > boms = manager .getGroups ()
370
- .stream ()
371
- .flatMap ((group ) -> group .getBoms ()
372
- .stream ()
373
- .map ((bom ) -> dependencyHandler
374
- .platform (group .getId () + ":" + bom + ":" + manager .getVersion ().getVersion ())))
375
- .toList ();
376
- List <Dependency > dependencies = new ArrayList <>();
377
- dependencies .addAll (boms );
378
- dependencies .add (dependencyHandler .create (this .from ));
359
+ private Map <String , String > resolveAligningDependencies () {
360
+ List <Dependency > dependencies = getAligningDependencies ();
379
361
Configuration alignmentConfiguration = this .project .getConfigurations ()
380
362
.detachedConfiguration (dependencies .toArray (new Dependency [0 ]));
381
363
Map <String , String > versions = new HashMap <>();
@@ -388,6 +370,58 @@ private Map<String, String> resolveAligningDependencies(Library manager) {
388
370
return versions ;
389
371
}
390
372
373
+ private List <Dependency > getAligningDependencies () {
374
+ if (this .managedBy == null ) {
375
+ Library fromLibrary = findFromLibrary ();
376
+ return List
377
+ .of (this .project .getDependencies ().create (this .from + ":" + fromLibrary .getVersion ().getVersion ()));
378
+ }
379
+ else {
380
+ Library managingLibrary = findManagingLibrary ();
381
+ List <Dependency > boms = getBomDependencies (managingLibrary );
382
+ List <Dependency > dependencies = new ArrayList <>();
383
+ dependencies .addAll (boms );
384
+ dependencies .add (this .project .getDependencies ().create (this .from ));
385
+ return dependencies ;
386
+ }
387
+ }
388
+
389
+ private Library findFromLibrary () {
390
+ for (Library library : this .libraries ) {
391
+ for (Group group : library .getGroups ()) {
392
+ for (Module module : group .getModules ()) {
393
+ if (this .from .equals (group .getId () + ":" + module .getName ())) {
394
+ return library ;
395
+ }
396
+ }
397
+ }
398
+ }
399
+ return null ;
400
+ }
401
+
402
+ private Library findManagingLibrary () {
403
+ if (this .managedBy == null ) {
404
+ return null ;
405
+ }
406
+ return this .libraries .stream ()
407
+ .filter ((candidate ) -> this .managedBy .equals (candidate .getName ()))
408
+ .findFirst ()
409
+ .orElseThrow (() -> new IllegalStateException ("Managing library '" + this .managedBy + "' not found." ));
410
+ }
411
+
412
+ private List <Dependency > getBomDependencies (Library manager ) {
413
+ if (manager == null ) {
414
+ return Collections .emptyList ();
415
+ }
416
+ return manager .getGroups ()
417
+ .stream ()
418
+ .flatMap ((group ) -> group .getBoms ()
419
+ .stream ()
420
+ .map ((bom ) -> this .project .getDependencies ()
421
+ .platform (group .getId () + ":" + bom + ":" + manager .getVersion ().getVersion ())))
422
+ .toList ();
423
+ }
424
+
391
425
String getFrom () {
392
426
return this .from ;
393
427
}
@@ -398,7 +432,11 @@ String getManagedBy() {
398
432
399
433
@ Override
400
434
public String toString () {
401
- return "version from dependencies of " + this .from + " that is managed by " + this .managedBy ;
435
+ String result = "version from dependencies of " + this .from ;
436
+ if (this .managedBy != null ) {
437
+ result += " that is managed by " + this .managedBy ;
438
+ }
439
+ return result ;
402
440
}
403
441
404
442
}
0 commit comments