16
16
17
17
package org .springframework .security .convention .versions ;
18
18
19
- import java .util .ArrayList ;
20
- import java .util .List ;
21
- import java .util .Map ;
22
- import java .util .Objects ;
23
- import java .util .stream .Collectors ;
24
-
25
19
import org .gradle .api .DefaultTask ;
26
20
import org .gradle .api .Plugin ;
27
21
import org .gradle .api .Project ;
28
- import org .gradle .api .artifacts .Configuration ;
22
+ import org .gradle .api .artifacts .MinimalExternalModuleDependency ;
23
+ import org .gradle .api .artifacts .VersionCatalog ;
24
+ import org .gradle .api .artifacts .VersionCatalogsExtension ;
29
25
import org .gradle .api .plugins .JavaBasePlugin ;
30
26
import org .gradle .api .tasks .TaskAction ;
31
27
import org .gradle .api .tasks .TaskProvider ;
@@ -37,101 +33,38 @@ public void apply(Project project) {
37
33
TaskProvider <VerifyDependenciesVersionsTask > verifyDependenciesVersionsTaskProvider = project .getTasks ().register ("verifyDependenciesVersions" , VerifyDependenciesVersionsTask .class , (task ) -> {
38
34
task .setGroup ("Verification" );
39
35
task .setDescription ("Verify that specific dependencies are using the same version" );
40
- List <Configuration > allConfigurations = new ArrayList <>(getConfigurations (project ));
41
- task .setConfigurations (allConfigurations );
36
+ VersionCatalog versionCatalog = project .getExtensions ().getByType (VersionCatalogsExtension .class ).named ("libs" );
37
+ MinimalExternalModuleDependency oauth2OidcSdk = versionCatalog .findLibrary ("com-nimbusds-oauth2-oidc-sdk" ).get ().get ();
38
+ MinimalExternalModuleDependency nimbusJoseJwt = versionCatalog .findLibrary ("com-nimbusds-nimbus-jose-jwt" ).get ().get ();
39
+ task .setOauth2OidcSdkVersion (oauth2OidcSdk .getVersionConstraint ().getDisplayName ());
40
+ task .setExpectedNimbusJoseJwtVersion (nimbusJoseJwt .getVersionConstraint ().getDisplayName ());
42
41
});
43
42
project .getTasks ().named (JavaBasePlugin .CHECK_TASK_NAME , checkTask -> checkTask .dependsOn (verifyDependenciesVersionsTaskProvider ));
44
43
}
45
44
46
- private List <Configuration > getConfigurations (Project rootProject ) {
47
- List <Configuration > configurations = new ArrayList <>();
48
- for (Project project : rootProject .getAllprojects ()) {
49
- List <Configuration > runtimeClasspath = project .getConfigurations ().stream ()
50
- .filter (Configuration ::isCanBeResolved )
51
- .filter ((config ) -> config .getName ().equals ("runtimeClasspath" ))
52
- .collect (Collectors .toList ());
53
- configurations .addAll (runtimeClasspath );
54
- }
55
- return configurations ;
56
- }
57
-
58
45
public static class VerifyDependenciesVersionsTask extends DefaultTask {
59
46
60
- private List <Configuration > configurations ;
47
+ private String oauth2OidcSdkVersion ;
48
+
49
+ private String expectedNimbusJoseJwtVersion ;
50
+
51
+ public void setOauth2OidcSdkVersion (String oauth2OidcSdkVersion ) {
52
+ this .oauth2OidcSdkVersion = oauth2OidcSdkVersion ;
53
+ }
61
54
62
- public void setConfigurations ( List < Configuration > configurations ) {
63
- this .configurations = configurations ;
55
+ public void setExpectedNimbusJoseJwtVersion ( String expectedNimbusJoseJwtVersion ) {
56
+ this .expectedNimbusJoseJwtVersion = expectedNimbusJoseJwtVersion ;
64
57
}
65
58
66
59
@ TaskAction
67
60
public void verify () {
68
- Map <String , List <Artifact >> artifacts = getDependencies (this .configurations );
69
- List <Artifact > oauth2OidcSdk = artifacts .get ("oauth2-oidc-sdk" );
70
- List <Artifact > nimbusJoseJwt = artifacts .get ("nimbus-jose-jwt" );
71
- if (oauth2OidcSdk == null ) {
72
- // Could not resolve oauth2-oidc-sdk
73
- return ;
74
- }
75
- if (oauth2OidcSdk .size () > 1 ) {
76
- throw new IllegalStateException ("Found multiple versions of oauth2-oidc-sdk: " + oauth2OidcSdk );
77
- }
78
- Artifact oauth2OidcSdkArtifact = oauth2OidcSdk .get (0 );
79
- String nimbusJoseJwtVersion = TransitiveDependencyLookupUtils .lookupJwtVersion (oauth2OidcSdkArtifact .version ());
80
- List <Artifact > differentVersions = nimbusJoseJwt .stream ()
81
- .filter ((artifact ) -> !artifact .version ().equals (nimbusJoseJwtVersion ))
82
- .filter ((artifact -> !artifact .configurationName ().contains ("spring-security-cas" ))) // CAS uses a different version
83
- .collect (Collectors .toList ());
84
- if (!differentVersions .isEmpty ()) {
85
- String message = "Found transitive nimbus-jose-jwt version [" + nimbusJoseJwtVersion + "] in oauth2-oidc-sdk " + oauth2OidcSdkArtifact
86
- + ", but the project contains a different version of nimbus-jose-jwt " + differentVersions
87
- + ". Please align the versions of nimbus-jose-jwt." ;
61
+ String transitiveNimbusJoseJwtVersion = TransitiveDependencyLookupUtils .lookupJwtVersion (this .oauth2OidcSdkVersion );
62
+ if (!transitiveNimbusJoseJwtVersion .equals (this .expectedNimbusJoseJwtVersion )) {
63
+ String message = String .format ("Found transitive nimbus-jose-jwt:%s in oauth2-oidc-sdk:%s, but the project contains a different version of nimbus-jose-jwt [%s]. Please align the versions." , transitiveNimbusJoseJwtVersion , this .oauth2OidcSdkVersion , this .expectedNimbusJoseJwtVersion );
88
64
throw new IllegalStateException (message );
89
65
}
90
66
}
91
67
92
- private Map <String , List <Artifact >> getDependencies (List <Configuration > configurations ) {
93
- return configurations .stream ()
94
- .flatMap ((configuration ) -> {
95
- return configuration .getIncoming ().getResolutionResult ().getAllDependencies ().stream ()
96
- .map ((dep ) -> {
97
- String [] nameParts = dep .getRequested ().getDisplayName ().split (":" );
98
- if (nameParts .length > 2 ) {
99
- return new Artifact (nameParts [1 ], nameParts [2 ], configuration .toString ());
100
- }
101
- return null ;
102
- });
103
- })
104
- .filter (Objects ::nonNull )
105
- .distinct ()
106
- .collect (Collectors .groupingBy (Artifact ::name ));
107
- }
108
-
109
- }
110
-
111
- private static class Artifact {
112
-
113
- private final String name ;
114
- private final String version ;
115
- private final String configurationName ;
116
-
117
- private Artifact (String name , String version , String configurationName ) {
118
- this .name = name ;
119
- this .version = version ;
120
- this .configurationName = configurationName ;
121
- }
122
-
123
- public String name () {
124
- return this .name ;
125
- }
126
-
127
- public String version () {
128
- return this .version ;
129
- }
130
-
131
- public String configurationName () {
132
- return this .configurationName ;
133
- }
134
-
135
68
}
136
69
137
70
}
0 commit comments