Skip to content

Commit b0cadc1

Browse files
authored
Merge pull request #1 from GradleUp/configuration-cache
Support configuration cache
2 parents 8d999bb + 77ca617 commit b0cadc1

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ gradlePlugin {
2323
}
2424

2525
group = "com.gradleup.maven-sympathy"
26-
version = "0.0.1"
26+
version = "0.0.2"
2727

2828
tasks.withType(JavaCompile::class.java) {
2929
options.release.set(8)

src/main/kotlin/com/gradleup/maven/sympathy/MavenSympathyPlugin.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class MavenSympathyPlugin: Plugin<Project> {
88
val taskProvider = target.tasks.register("sympathyForMrMaven", SympathyForMrMaven::class.java) {
99
it.group = "verification"
1010
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."
11-
it.configurations = target.project.configurations
1211
}
1312

1413
target.tasks.named("check") {

src/main/kotlin/com/gradleup/maven/sympathy/SympathyForMrMavenTask.kt

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,44 @@ import org.gradle.api.artifacts.ConfigurationContainer
66
import org.gradle.api.artifacts.component.ModuleComponentSelector
77
import org.gradle.api.artifacts.result.ResolvedComponentResult
88
import org.gradle.api.artifacts.result.ResolvedDependencyResult
9-
import org.gradle.api.tasks.Internal
109
import org.gradle.api.tasks.TaskAction
10+
import org.gradle.internal.serialization.Cached
1111
import org.gradle.work.DisableCachingByDefault
1212

1313
@DisableCachingByDefault
1414
abstract class SympathyForMrMaven : DefaultTask() {
15-
@get:Internal
16-
abstract var configurations: ConfigurationContainer
15+
private val model = Cached.of(::calculateReport)
1716

1817
@TaskAction
1918
fun taskAction() {
20-
configurations.filter {
19+
var fail = false
20+
model.get().upgrades.forEach {
21+
logger.error("e: direct dependency ${it.requested} of configuration '${it.configuration}' was changed to ${it.selectedVersion}")
22+
fail = true
23+
}
24+
if (fail) {
25+
throw IllegalStateException("Declared dependencies were upgraded transitively. See task output above. Please update their versions.")
26+
}
27+
}
28+
29+
class Upgrade(val configuration: String, val requested: String, val selectedVersion: String)
30+
class Report(val upgrades: List<Upgrade>)
31+
32+
private fun calculateReport(): Report {
33+
return project.configurations.filter {
2134
it.isCanBeResolved
22-
}.forEach {
23-
checkConfiguration(it)
35+
}.flatMap {
36+
findUpgrades(it)
37+
}.let {
38+
Report(it)
2439
}
2540
}
2641

2742
/**
2843
* See https://jakewharton.com/nonsensical-maven-is-still-a-gradle-problem/
2944
*/
30-
private fun checkConfiguration(configuration: Configuration) {
31-
var fail = false
45+
private fun findUpgrades(configuration: Configuration): List<Upgrade> {
46+
val upgrades = mutableListOf<Upgrade>()
3247
val root = configuration.incoming.resolutionResult.rootComponent.get()
3348
(root as ResolvedComponentResult).dependencies.forEach {
3449
if (it is ResolvedDependencyResult) {
@@ -39,14 +54,12 @@ abstract class SympathyForMrMaven : DefaultTask() {
3954
val requestedVersion = requested.version
4055
val selectedVersion = selected.moduleVersion?.version
4156
if (requestedVersion != selectedVersion) {
42-
logger.error("e: ${rdr.requested} changed to $selectedVersion")
43-
fail = true
57+
upgrades.add(Upgrade(configuration.name, rdr.requested.toString(), selectedVersion.toString()))
4458
}
4559
}
4660
}
4761
}
48-
if (fail) {
49-
throw IllegalStateException("Declared dependencies were upgraded transitively. See task output above. Please update their versions.")
50-
}
62+
63+
return upgrades
5164
}
5265
}

0 commit comments

Comments
 (0)