16
16
17
17
package org .springframework .boot .build .bom ;
18
18
19
+ import java .util .ArrayList ;
20
+ import java .util .List ;
19
21
import java .util .Set ;
20
22
import java .util .TreeSet ;
21
23
import java .util .stream .Collectors ;
22
24
23
25
import javax .inject .Inject ;
24
26
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 ;
25
31
import org .gradle .api .DefaultTask ;
26
- import org .gradle .api .InvalidUserDataException ;
32
+ import org .gradle .api .GradleException ;
27
33
import org .gradle .api .tasks .TaskAction ;
28
34
29
35
import org .springframework .boot .build .bom .Library .Group ;
30
36
import org .springframework .boot .build .bom .Library .Module ;
37
+ import org .springframework .boot .build .bom .Library .ProhibitedVersion ;
31
38
import org .springframework .boot .build .bom .bomr .version .DependencyVersion ;
32
39
33
40
/**
@@ -46,18 +53,41 @@ public CheckBom(BomExtension bom) {
46
53
47
54
@ TaskAction
48
55
void checkBom () {
56
+ List <String > errors = new ArrayList <>();
49
57
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 );
55
85
}
56
86
}
57
87
}
58
88
}
59
89
60
- private void checkExclusions (String groupId , Module module , DependencyVersion version ) {
90
+ private void checkExclusions (String groupId , Module module , DependencyVersion version , List < String > errors ) {
61
91
Set <String > resolved = getProject ().getConfigurations ()
62
92
.detachedConfiguration (
63
93
getProject ().getDependencies ().create (groupId + ":" + module .getName () + ":" + version ))
@@ -87,8 +117,34 @@ private void checkExclusions(String groupId, Module module, DependencyVersion ve
87
117
}
88
118
exclusions .removeAll (resolved );
89
119
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
+ }
92
148
}
93
149
}
94
150
0 commit comments