Skip to content

Commit

Permalink
Merge pull request #1 from GradleUp/configuration-cache
Browse files Browse the repository at this point in the history
Support configuration cache
  • Loading branch information
martinbonnin authored May 7, 2024
2 parents 8d999bb + 77ca617 commit b0cadc1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class MavenSympathyPlugin: Plugin<Project> {
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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Upgrade>)

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<Upgrade> {
val upgrades = mutableListOf<Upgrade>()
val root = configuration.incoming.resolutionResult.rootComponent.get()
(root as ResolvedComponentResult).dependencies.forEach {
if (it is ResolvedDependencyResult) {
Expand All @@ -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
}
}

0 comments on commit b0cadc1

Please sign in to comment.