diff --git a/maven-sympathy/build.gradle b/maven-sympathy/build.gradle index e57bf26..2883d80 100644 --- a/maven-sympathy/build.gradle +++ b/maven-sympathy/build.gradle @@ -34,8 +34,6 @@ gradlePlugin { } dependencies { - compileOnly(libs.kotlin.gradle.api) - testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.junit.jupiter.engine) diff --git a/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/MavenSympathyPlugin.kt b/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/MavenSympathyPlugin.kt index 8b4f80e..8cea8bf 100644 --- a/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/MavenSympathyPlugin.kt +++ b/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/MavenSympathyPlugin.kt @@ -9,12 +9,11 @@ public class MavenSympathyPlugin : Plugin { override fun apply(target: Project): Unit = with(target) { val reportingExtension = extensions.getByType(ReportingExtension::class.java) val task = tasks.register("sympathyForMrMaven", SympathyForMrMavenTask::class.java) { - outputFile.set(reportingExtension.baseDirectory.map { it.dir("sympathyForMrMaven").file("output.txt") }) + outputFile.set(reportingExtension.baseDirectory.map { it.dir(name).file("output.txt") }) } - configurations.configureEach { - if (!isCanBeResolved) return@configureEach - + configurations.matching { it.isCanBeResolved }.configureEach { task.configure { + if (!isCanBeResolved) return@configure configurationWithDependencies.put(this@configureEach.name, incoming.resolutionResult.rootComponent) } } diff --git a/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/SympathyForMrMavenTask.kt b/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/SympathyForMrMavenTask.kt index e3af256..bfe32a8 100644 --- a/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/SympathyForMrMavenTask.kt +++ b/maven-sympathy/src/main/kotlin/io/github/usefulness/mavensympathy/SympathyForMrMavenTask.kt @@ -27,20 +27,26 @@ public open class SympathyForMrMavenTask @Inject constructor(objectFactory: Obje public fun run() { var fail = false configurationWithDependencies.get().forEach { (name, root) -> - root.dependencies.filterIsInstance().forEach { rdr -> - val requested = rdr.requested as? ModuleComponentSelector ?: return@forEach - val selected = rdr.selected - val requestedVersion = requested.version - val selectedVersion = selected.moduleVersion?.version - if (requestedVersion != selectedVersion) { - logger.error("[$name] requested: $requested changed to $selectedVersion") - fail = true + root.dependencies.asSequence() + .filterIsInstance() + .filter { it.requested is ModuleComponentSelector } + .forEach { rdr -> + val requested = rdr.requested as? ModuleComponentSelector + val selected = rdr.selected + val requestedVersion = requested?.version + val selectedVersion = selected.moduleVersion?.version + if (!requestedVersion.isNullOrBlank() && requestedVersion != selectedVersion) { + logger.error("[$name] dependency $requested version changed $requestedVersion -> $selectedVersion") + fail = true + } } - } } + val report = outputFile.get().asFile if (fail) { + report.writeText("NOT OK") error("Declared dependencies were upgraded transitively. See task output above. Please update their versions.") + } else { + report.writeText("OK") } - outputFile.get().asFile.writeText("OK") } } diff --git a/maven-sympathy/src/test/kotlin/io/github/usefulness/mavensympathy/IntegrationTest.kt b/maven-sympathy/src/test/kotlin/io/github/usefulness/mavensympathy/IntegrationTest.kt index 57b3848..5da9ec5 100644 --- a/maven-sympathy/src/test/kotlin/io/github/usefulness/mavensympathy/IntegrationTest.kt +++ b/maven-sympathy/src/test/kotlin/io/github/usefulness/mavensympathy/IntegrationTest.kt @@ -20,12 +20,19 @@ class IntegrationTest { // language=groovy writeText( """ - dependencyResolutionManagement { - repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) - repositories { - mavenCentral() + pluginManagement { + repositories { + mavenCentral() + gradlePluginPortal() + } } - } + dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + mavenCentral() + } + } + """.trimIndent(), ) } @@ -41,6 +48,15 @@ class IntegrationTest { id("java-library") id("io.github.usefulness.maven-sympathy") } + + tasks.withType(Test).configureEach { + useJUnitPlatform() + } + + dependencies { + testRuntimeOnly("org.junit.platform:junit-platform-launcher") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.2") + } """.trimIndent(), ) } @@ -96,6 +112,6 @@ class IntegrationTest { } val result = runGradle(projectDir = rootDirectory, shouldFail = true) - assertThat(result.output).contains("com.squareup.okhttp3:okhttp:3.14.8 changed to 3.14.9") + assertThat(result.output).contains("com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9") } }