1616
1717package org .springframework .boot .build .bom ;
1818
19+ import java .util .ArrayList ;
20+ import java .util .List ;
1921import java .util .Set ;
2022import java .util .TreeSet ;
2123import java .util .stream .Collectors ;
2224
2325import javax .inject .Inject ;
2426
27+ import org .apache .maven .artifact .versioning .ArtifactVersion ;
28+ import org .apache .maven .artifact .versioning .DefaultArtifactVersion ;
29+ import org .apache .maven .artifact .versioning .Restriction ;
30+ import org .apache .maven .artifact .versioning .VersionRange ;
2531import org .gradle .api .DefaultTask ;
26- import org .gradle .api .InvalidUserDataException ;
32+ import org .gradle .api .GradleException ;
2733import org .gradle .api .tasks .TaskAction ;
2834
2935import org .springframework .boot .build .bom .Library .Group ;
3036import org .springframework .boot .build .bom .Library .Module ;
37+ import org .springframework .boot .build .bom .Library .ProhibitedVersion ;
3138import org .springframework .boot .build .bom .bomr .version .DependencyVersion ;
3239
3340/**
@@ -46,18 +53,41 @@ public CheckBom(BomExtension bom) {
4653
4754 @ TaskAction
4855 void checkBom () {
56+ List <String > errors = new ArrayList <>();
4957 for (Library library : this .bom .getLibraries ()) {
50- for (Group group : library .getGroups ()) {
51- for (Module module : group .getModules ()) {
52- if (!module .getExclusions ().isEmpty ()) {
53- checkExclusions (group .getId (), module , library .getVersion ().getVersion ());
54- }
58+ checkLibrary (library , errors );
59+ }
60+ if (!errors .isEmpty ()) {
61+ System .out .println ();
62+ errors .forEach (System .out ::println );
63+ System .out .println ();
64+ throw new GradleException ("Bom check failed. See previous output for details." );
65+ }
66+ }
67+
68+ private void checkLibrary (Library library , List <String > errors ) {
69+ List <String > libraryErrors = new ArrayList <>();
70+ checkExclusions (library , libraryErrors );
71+ checkProhibitedVersions (library , libraryErrors );
72+ if (!libraryErrors .isEmpty ()) {
73+ errors .add (library .getName ());
74+ for (String libraryError : libraryErrors ) {
75+ errors .add (" - " + libraryError );
76+ }
77+ }
78+ }
79+
80+ private void checkExclusions (Library library , List <String > errors ) {
81+ for (Group group : library .getGroups ()) {
82+ for (Module module : group .getModules ()) {
83+ if (!module .getExclusions ().isEmpty ()) {
84+ checkExclusions (group .getId (), module , library .getVersion ().getVersion (), errors );
5585 }
5686 }
5787 }
5888 }
5989
60- private void checkExclusions (String groupId , Module module , DependencyVersion version ) {
90+ private void checkExclusions (String groupId , Module module , DependencyVersion version , List < String > errors ) {
6191 Set <String > resolved = getProject ().getConfigurations ()
6292 .detachedConfiguration (
6393 getProject ().getDependencies ().create (groupId + ":" + module .getName () + ":" + version ))
@@ -87,8 +117,34 @@ private void checkExclusions(String groupId, Module module, DependencyVersion ve
87117 }
88118 exclusions .removeAll (resolved );
89119 if (!unused .isEmpty ()) {
90- throw new InvalidUserDataException (
91- "Unnecessary exclusions on " + groupId + ":" + module .getName () + ": " + exclusions );
120+ errors .add ("Unnecessary exclusions on " + groupId + ":" + module .getName () + ": " + exclusions );
121+ }
122+ }
123+
124+ private void checkProhibitedVersions (Library library , List <String > errors ) {
125+ ArtifactVersion currentVersion = new DefaultArtifactVersion (library .getVersion ().getVersion ().toString ());
126+ for (ProhibitedVersion prohibited : library .getProhibitedVersions ()) {
127+ if (prohibited .isProhibited (library .getVersion ().getVersion ().toString ())) {
128+ errors .add ("Current version " + currentVersion + " is prohibited" );
129+ }
130+ else {
131+ VersionRange versionRange = prohibited .getRange ();
132+ if (versionRange != null ) {
133+ for (Restriction restriction : versionRange .getRestrictions ()) {
134+ ArtifactVersion upperBound = restriction .getUpperBound ();
135+ if (upperBound == null ) {
136+ return ;
137+ }
138+ int comparison = currentVersion .compareTo (upperBound );
139+ if ((restriction .isUpperBoundInclusive () && comparison <= 0 )
140+ || ((!restriction .isUpperBoundInclusive ()) && comparison < 0 )) {
141+ return ;
142+ }
143+ }
144+ errors .add ("Version range " + versionRange + " is ineffective as the current version, "
145+ + currentVersion + ", is greater than its upper bound" );
146+ }
147+ }
92148 }
93149 }
94150
0 commit comments