|
19 | 19 | import org.gradle.api.DefaultTask; |
20 | 20 | import org.gradle.api.Plugin; |
21 | 21 | import org.gradle.api.Project; |
| 22 | +import org.gradle.api.artifacts.Dependency; |
22 | 23 | import org.gradle.api.artifacts.MinimalExternalModuleDependency; |
23 | 24 | import org.gradle.api.artifacts.VersionCatalog; |
24 | 25 | import org.gradle.api.artifacts.VersionCatalogsExtension; |
| 26 | +import org.gradle.api.file.RegularFile; |
| 27 | +import org.gradle.api.file.RegularFileProperty; |
25 | 28 | import org.gradle.api.plugins.JavaBasePlugin; |
| 29 | +import org.gradle.api.provider.Property; |
| 30 | +import org.gradle.api.provider.Provider; |
| 31 | +import org.gradle.api.tasks.CacheableTask; |
| 32 | +import org.gradle.api.tasks.Input; |
| 33 | +import org.gradle.api.tasks.OutputFile; |
26 | 34 | import org.gradle.api.tasks.TaskAction; |
| 35 | +import org.gradle.api.tasks.TaskExecutionException; |
27 | 36 | import org.gradle.api.tasks.TaskProvider; |
| 37 | +import org.gradle.api.tasks.VerificationException; |
| 38 | + |
| 39 | +import java.io.File; |
| 40 | +import java.io.IOException; |
| 41 | +import java.nio.file.Files; |
| 42 | +import java.util.Optional; |
28 | 43 |
|
29 | 44 | public class VerifyDependenciesVersionsPlugin implements Plugin<Project> { |
30 | 45 |
|
31 | 46 | @Override |
32 | 47 | public void apply(Project project) { |
| 48 | + VersionCatalog versionCatalog = project.getExtensions().getByType(VersionCatalogsExtension.class).named("libs"); |
| 49 | + Optional<Provider<MinimalExternalModuleDependency>> oauth2OidcSdk = versionCatalog.findLibrary("com-nimbusds-oauth2-oidc-sdk"); |
| 50 | + Optional<Provider<MinimalExternalModuleDependency>> nimbusJoseJwt = versionCatalog.findLibrary("com-nimbusds-nimbus-jose-jwt"); |
| 51 | + |
| 52 | + if (oauth2OidcSdk.isEmpty()) { |
| 53 | + throw new VerificationException("Library [com-nimbusds-oauth2-oidc-sdk] does not exist in the version catalog named libs."); |
| 54 | + } |
| 55 | + |
| 56 | + if (nimbusJoseJwt.isEmpty()) { |
| 57 | + throw new VerificationException("Library [com-nimbusds-nimbus-jose-jwt] does not exist in the version catalog named libs."); |
| 58 | + } |
| 59 | + |
33 | 60 | TaskProvider<VerifyDependenciesVersionsTask> verifyDependenciesVersionsTaskProvider = project.getTasks().register("verifyDependenciesVersions", VerifyDependenciesVersionsTask.class, (task) -> { |
34 | 61 | task.setGroup("Verification"); |
35 | 62 | task.setDescription("Verify that specific dependencies are using the same version"); |
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.getVersion()); |
40 | | - task.setExpectedNimbusJoseJwtVersion(nimbusJoseJwt.getVersion()); |
| 63 | + task.getOauth2OidcSdkVersion().convention(oauth2OidcSdk.get().map(Dependency::getVersion)); |
| 64 | + task.getExpectedNimbusJoseJwtVersion().convention(nimbusJoseJwt.get().map(Dependency::getVersion)); |
| 65 | + task.getOutputFile().convention(project.getLayout().getBuildDirectory().file("verify-dependencies-versions")); |
41 | 66 | }); |
42 | 67 | project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, checkTask -> checkTask.dependsOn(verifyDependenciesVersionsTaskProvider)); |
43 | 68 | } |
44 | 69 |
|
45 | | - public static class VerifyDependenciesVersionsTask extends DefaultTask { |
| 70 | + @CacheableTask |
| 71 | + public abstract static class VerifyDependenciesVersionsTask extends DefaultTask { |
46 | 72 |
|
47 | | - private String oauth2OidcSdkVersion; |
| 73 | + @Input |
| 74 | + abstract Property<String> getOauth2OidcSdkVersion(); |
48 | 75 |
|
49 | | - private String expectedNimbusJoseJwtVersion; |
| 76 | + @Input |
| 77 | + abstract Property<String> getExpectedNimbusJoseJwtVersion(); |
50 | 78 |
|
51 | | - public void setOauth2OidcSdkVersion(String oauth2OidcSdkVersion) { |
52 | | - this.oauth2OidcSdkVersion = oauth2OidcSdkVersion; |
53 | | - } |
54 | | - |
55 | | - public void setExpectedNimbusJoseJwtVersion(String expectedNimbusJoseJwtVersion) { |
56 | | - this.expectedNimbusJoseJwtVersion = expectedNimbusJoseJwtVersion; |
57 | | - } |
| 79 | + @OutputFile |
| 80 | + abstract RegularFileProperty getOutputFile(); |
58 | 81 |
|
59 | 82 | @TaskAction |
60 | | - public void verify() { |
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); |
64 | | - throw new IllegalStateException(message); |
| 83 | + public void verify() { |
| 84 | + String oauth2OidcSdkVersion = this.getOauth2OidcSdkVersion().get(); |
| 85 | + String transitiveNimbusJoseJwtVersion = TransitiveDependencyLookupUtils.lookupJwtVersion(oauth2OidcSdkVersion); |
| 86 | + String expectedNimbusJoseJwtVersion = this.getExpectedNimbusJoseJwtVersion().get(); |
| 87 | + if (!transitiveNimbusJoseJwtVersion.equals(expectedNimbusJoseJwtVersion)) { |
| 88 | + 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, oauth2OidcSdkVersion, expectedNimbusJoseJwtVersion); |
| 89 | + throw new VerificationException(message); |
| 90 | + } |
| 91 | + String message = String.format("Found transitive nimbus-jose-jwt:%s in oauth2-oidc-sdk:%s, the project contains expected version of nimbus-jose-jwt [%s]. Verified all versions align.", transitiveNimbusJoseJwtVersion, oauth2OidcSdkVersion, expectedNimbusJoseJwtVersion); |
| 92 | + try { |
| 93 | + Files.writeString(getOutputFile().get().getAsFile().toPath(), message); |
| 94 | + } catch (IOException e) { |
| 95 | + throw new TaskExecutionException(this, e); |
65 | 96 | } |
66 | 97 | } |
67 | | - |
68 | 98 | } |
69 | | - |
70 | 99 | } |
0 commit comments