From f1c4ea78a2081b03f2280f07de6e92486df94456 Mon Sep 17 00:00:00 2001 From: lukasgraef Date: Sun, 1 Dec 2024 15:03:10 +0100 Subject: [PATCH] fix: broken serialization of NamedDomainObjectFactory on Gradle 7 --- .../snom/CacheabilityFunctionalTest.groovy | 2 +- .../snom/StandardFunctionalTest.groovy | 4 +-- .../com/github/spotbugs/snom/SpotBugsTask.kt | 32 ++++++++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/functionalTest/groovy/com/github/spotbugs/snom/CacheabilityFunctionalTest.groovy b/src/functionalTest/groovy/com/github/spotbugs/snom/CacheabilityFunctionalTest.groovy index 6fba61ba..8dfb6a31 100644 --- a/src/functionalTest/groovy/com/github/spotbugs/snom/CacheabilityFunctionalTest.groovy +++ b/src/functionalTest/groovy/com/github/spotbugs/snom/CacheabilityFunctionalTest.groovy @@ -115,7 +115,7 @@ class CacheabilityFunctionalTest extends BaseFunctionalTest { when: gradleRunner - .withArguments(':spotbugsMain', '--no-configuration-cache', '--build-cache') + .withArguments(':spotbugsMain', '--build-cache') .build() BuildResult result = gradleRunner .withArguments(':spotbugsMain', '--build-cache') diff --git a/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy b/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy index 2f1c7dff..96c38f15 100644 --- a/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy +++ b/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy @@ -159,7 +159,7 @@ spotbugsMain { when: gradleRunner - .withArguments(":spotbugsMain", "--no-configuration-cache") + .withArguments(":spotbugsMain") .build() def result = gradleRunner .withArguments(":spotbugsMain") @@ -486,7 +486,7 @@ public class MyFoo { when: BuildResult result = gradleRunner - .withArguments('--debug', "--no-configuration-cache", "spotbugsMain") + .withArguments('--debug', "spotbugsMain") .build() then: diff --git a/src/main/kotlin/com/github/spotbugs/snom/SpotBugsTask.kt b/src/main/kotlin/com/github/spotbugs/snom/SpotBugsTask.kt index 99a86107..83944622 100644 --- a/src/main/kotlin/com/github/spotbugs/snom/SpotBugsTask.kt +++ b/src/main/kotlin/com/github/spotbugs/snom/SpotBugsTask.kt @@ -24,6 +24,7 @@ import org.gradle.api.Action import org.gradle.api.DefaultTask import org.gradle.api.InvalidUserDataException import org.gradle.api.NamedDomainObjectContainer +import org.gradle.api.NamedDomainObjectFactory import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.FileCollection @@ -308,17 +309,26 @@ abstract class SpotBugsTask : DefaultTask(), VerificationTask { init { val objects = project.objects - reports = objects.domainObjectContainer(SpotBugsReport::class.java) { name: String -> - when (name) { - "html" -> objects.newInstance(SpotBugsHtmlReport::class.java, name, objects, this) - "xml" -> objects.newInstance(SpotBugsXmlReport::class.java, name, objects, this) - "text" -> objects.newInstance(SpotBugsTextReport::class.java, name, objects, this) - "sarif" -> objects.newInstance(SpotBugsSarifReport::class.java, name, objects, this) - else -> throw InvalidUserDataException("$name is invalid as the report name") - }.also { - (outputs as org.gradle.api.tasks.TaskOutputs).file(it.outputLocation) - } - } + val taskRef = this + reports = objects.domainObjectContainer( + SpotBugsReport::class.java, + // This anonymous object is necessary + // Otherwise the serialization of this lambda is broken with config-cache on Gradle 7 + @Suppress("ObjectLiteralToLambda") + object : NamedDomainObjectFactory { + override fun create(name: String): SpotBugsReport { + return when (name) { + "html" -> objects.newInstance(SpotBugsHtmlReport::class.java, name, objects, taskRef) + "xml" -> objects.newInstance(SpotBugsXmlReport::class.java, name, objects, taskRef) + "text" -> objects.newInstance(SpotBugsTextReport::class.java, name, objects, taskRef) + "sarif" -> objects.newInstance(SpotBugsSarifReport::class.java, name, objects, taskRef) + else -> throw InvalidUserDataException("$name is invalid as the report name") + }.also { + (outputs as org.gradle.api.tasks.TaskOutputs).file(it.outputLocation) + } + } + }, + ) description = "Run SpotBugs analysis." group = JavaBasePlugin.VERIFICATION_GROUP }