From 77ca6176fd6bd42d2239f60912092ec482ad7fc4 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Wed, 8 May 2024 00:08:32 +0200 Subject: [PATCH] Support configuration cache --- build.gradle.kts | 2 +- .../maven/sympathy/MavenSympathyPlugin.kt | 1 - .../maven/sympathy/SympathyForMrMavenTask.kt | 39 ++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 99e4f86..16fc2e9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,7 +23,7 @@ gradlePlugin { } group = "com.gradleup.maven-sympathy" -version = "0.0.1" +version = "0.0.2" tasks.withType(JavaCompile::class.java) { options.release.set(8) diff --git a/src/main/kotlin/com/gradleup/maven/sympathy/MavenSympathyPlugin.kt b/src/main/kotlin/com/gradleup/maven/sympathy/MavenSympathyPlugin.kt index 1a487d6..a5fc102 100644 --- a/src/main/kotlin/com/gradleup/maven/sympathy/MavenSympathyPlugin.kt +++ b/src/main/kotlin/com/gradleup/maven/sympathy/MavenSympathyPlugin.kt @@ -8,7 +8,6 @@ class MavenSympathyPlugin: Plugin { val taskProvider = target.tasks.register("sympathyForMrMaven", SympathyForMrMaven::class.java) { it.group = "verification" it.description = "Checks that your project dependencies play nice with Maven resolution strategy. See https://jakewharton.com/nonsensical-maven-is-still-a-gradle-problem/ for more details." - it.configurations = target.project.configurations } target.tasks.named("check") { diff --git a/src/main/kotlin/com/gradleup/maven/sympathy/SympathyForMrMavenTask.kt b/src/main/kotlin/com/gradleup/maven/sympathy/SympathyForMrMavenTask.kt index e8f1b3e..5adbb6b 100644 --- a/src/main/kotlin/com/gradleup/maven/sympathy/SympathyForMrMavenTask.kt +++ b/src/main/kotlin/com/gradleup/maven/sympathy/SympathyForMrMavenTask.kt @@ -6,29 +6,44 @@ import org.gradle.api.artifacts.ConfigurationContainer import org.gradle.api.artifacts.component.ModuleComponentSelector import org.gradle.api.artifacts.result.ResolvedComponentResult import org.gradle.api.artifacts.result.ResolvedDependencyResult -import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction +import org.gradle.internal.serialization.Cached import org.gradle.work.DisableCachingByDefault @DisableCachingByDefault abstract class SympathyForMrMaven : DefaultTask() { - @get:Internal - abstract var configurations: ConfigurationContainer + private val model = Cached.of(::calculateReport) @TaskAction fun taskAction() { - configurations.filter { + var fail = false + model.get().upgrades.forEach { + logger.error("e: direct dependency ${it.requested} of configuration '${it.configuration}' was changed to ${it.selectedVersion}") + fail = true + } + if (fail) { + throw IllegalStateException("Declared dependencies were upgraded transitively. See task output above. Please update their versions.") + } + } + + class Upgrade(val configuration: String, val requested: String, val selectedVersion: String) + class Report(val upgrades: List) + + private fun calculateReport(): Report { + return project.configurations.filter { it.isCanBeResolved - }.forEach { - checkConfiguration(it) + }.flatMap { + findUpgrades(it) + }.let { + Report(it) } } /** * See https://jakewharton.com/nonsensical-maven-is-still-a-gradle-problem/ */ - private fun checkConfiguration(configuration: Configuration) { - var fail = false + private fun findUpgrades(configuration: Configuration): List { + val upgrades = mutableListOf() val root = configuration.incoming.resolutionResult.rootComponent.get() (root as ResolvedComponentResult).dependencies.forEach { if (it is ResolvedDependencyResult) { @@ -39,14 +54,12 @@ abstract class SympathyForMrMaven : DefaultTask() { val requestedVersion = requested.version val selectedVersion = selected.moduleVersion?.version if (requestedVersion != selectedVersion) { - logger.error("e: ${rdr.requested} changed to $selectedVersion") - fail = true + upgrades.add(Upgrade(configuration.name, rdr.requested.toString(), selectedVersion.toString())) } } } } - if (fail) { - throw IllegalStateException("Declared dependencies were upgraded transitively. See task output above. Please update their versions.") - } + + return upgrades } } \ No newline at end of file