I have a task copyDetektService which copies a jar file (with custom Detekt rules) into a directory, and another task replaceDetektYaml which unzips the jar and copies the file detekt.yml from it to the directory /config/detekt/. The task detekt depends on the replaceDetektYaml task. If I just run ./gradlew replaceDetektYaml, it is successful. However, ./gradlew detekt fails with the error:
Reason: Task ‘detekt’ uses the output of task ‘replaceDetektYaml’ without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on the order in which the tasks are executed.
Possible solutions:
- Declare task ‘replaceDetektYaml’ as an input of ‘detekt’.
- Declare an explicit dependency on ‘replaceDetektYaml’ from ‘detekt’ using Task#dependsOn.
- Declare an explicit dependency on ‘replaceDetektYaml’ from ‘detekt’ using Task#mustRunAfter.
Here is the build.gradle.kts file:
object Detekt {
const val version = "1.23.5"
const val detektLib = "io.gitlab.arturbosch.detekt"
}
allprojects {
apply(plugin = Plugins.Detekt.detektLib)
detekt {
toolVersion = Plugins.Detekt.version
config.setFrom(files("$rootDir/config/detekt/detekt.yml"))
buildUponDefaultConfig = false
}
}
subprojects {
@Suppress("UnstableApiUsage")
configurations {
create("detektService") {
isTransitive = false
}
}
dependencies {
"detektService"(Libs.Detekt.detektService)
detektPlugins(files("$rootDir/detekt/detekt-custom-rules.jar"))
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:${Plugins.Detekt.version}")
}
tasks.register<Copy>("copyDetektService") {
group = "Custom Copy Tasks"
description = "Copies detekt service files to the root directory"
from(configurations["detektService"].files)
into("$rootDir/detekt")
rename { fileName: String ->
fileName.replaceAfter("rules", ".jar")
}
outputs.dir("$rootDir/detekt")
}
tasks.register("replaceDetektYaml") {
group = "Custom Copy Tasks"
description = "Copies detekt yaml file to config directory"
dependsOn("copyDetektService")
mustRunAfter("copyDetektService")
doLast {
copy {
delete("$rootDir/config/detekt")
from(zipTree(file("$rootDir/detekt/detekt-custom-rules.jar"))
.matching { include("META-INF/services/detekt.yml") }.files)
into("$rootDir/config/detekt")
}
}
outputs.file("$rootDir/config/detekt/detekt.yml")
}
tasks.named("detekt") {
inputs.files("$rootDir/config/detekt/detekt.yml")
dependsOn("replaceDetektYaml")
mustRunAfter("replaceDetektYaml")
}
}
Has anyone faced a similar issue? I wasn’t facing this issue on Gradle 6.2, but I started getting it after upgrading to Gradle 8.5. I am not facing any issues with other custom tasks. I tried to register a new task setup and made it depend on replaceDetektYaml, and there were no errors. It seems this issue is specific to the detekt task. Thanks!