Skip to main content

Howto: configure detekt for gradle root project

· 2 min read

When configuring detekt for your Gradle based project, you basically have two options:

  • for each sub module a new gradle task should be created
  • or one uber-task analyzes your whole project

For the first option, please see how detekt itself creates a task for each module:

subprojecs {
...
detekt {
debug = true
toolVersion = usedDetektVersion
buildUponDefaultConfig = true
config = files(project.rootDir.resolve("reports/failfast.yml"))
baseline = project.rootDir.resolve("reports/baseline.xml")

reports {
xml.enabled = true
html.enabled = true
}
}
}

Sometimes it makes sense to add an additional detekt task which runs over the whole project and produces one big report. Such a setup could look like the following in its simplest form:

plugins {
id "io.gitlab.arturbosch.detekt" version "1.0.0-RC14"
}

repositories {
jcenter()
}

detekt {
source = files(rootProject.rootDir)
buildUponDefaultConfig = true
}

Make sure to specify the input parameter or no sources are found and detekt won't run!

If you need more fine grained detekt tasks, you could register more tasks using the Detekt task as the base task. Using the Kotlin-Dsl it could look like this:

val detektAll by tasks.registering(Detekt::class) {
description = "Runs over whole code base without the starting overhead for each module."
parallel = true
buildUponDefaultConfig = true
setSource(files(projectDir))
config = files(project.rootDir.resolve("reports/failfast.yml"))
include("**/*.kt")
include("**/*.kts")
exclude("resources/")
exclude("build/")
baseline.set(project.rootDir.resolve("reports/baseline.xml"))
reports {
xml.enabled = false
html.enabled = false
}
}

{% include links.html %}