Finding Baseline
With the cli option --baseline or the detekt-gradle-plugin closure-property baseline you can specify a file which is used to generate a baseline.xml.
It is a file where ignored findings are defined.
The intention of CurrentIssues is that only new findings are printed on further analysis.
The ManuallySuppressedIssues can be used to write down false positive detections (instead of suppressing them and pollute your code base).
The ID node has the following structure: <RuleID>:<FileName>:<Finding_Signature>.
When adding a custom issue to the xml file, make sure the RuleID should be self-explaining.
<SmellBaseline>
<ManuallySuppressedIssues>
<ID>TooGenericExceptionCaught:Junk.kt:Junk$e: RuntimeException</ID>
</ManuallySuppressedIssues>
<CurrentIssues>
<ID>UndocumentedPublicClass:Junk.kt:Junk</ID>
<ID>UndocumentedPublicFunction:Junk.kt:Junk$fun compute: Int</ID>
<ID>MagicNumber:Junk.kt:Junk$42</ID>
</CurrentIssues>
</SmellBaseline>
If a rule is run several times with different configurations, the rule instance name is part of
the ID too, e.g. MagicNumber/my-instance:Junk.kt:Junk$42.
Baseline files generated by detekt 1.x are not compatible with detekt 2.x: the separator
between the file name and the signature changed from $ to :, so no 1.x entry will match.
Regenerate your baseline after upgrading — see the migration guide.
CLI
To generate yourself a baseline.xml you need to provide the same config as the the rules you are going to scan your project.
java -jar detekt-cli-all.jar \
--plugins detekt-rules-ktlint-wrapper.jar \
--build-upon-default-config \
--config path/to/config/detekt/detekt.yml \
+ --baseline path/to/new/config/detekt/baseline.xml \
+ --create-baseline
Gradle
If you are using the gradle-plugin run the detektBaseline task to generate yourself a baseline.xml.
This will create one baseline file per Gradle module.
As this might not be the desired behavior for a multi module project, think about implementing
a custom meta baseline task:
Groovy DSL
subprojects {
detekt {
// ...
baseline = file("${rootProject.projectDir}/config/baseline.xml")
// ...
}
}
task detektProjectBaseline(type: dev.detekt.gradle.DetektCreateBaselineTask) {
description = "Overrides current baseline."
ignoreFailures.set(true)
parallel.set(true)
buildUponDefaultConfig.set(true)
setSource(files(rootDir))
config.setFrom(files("$rootDir/config/detekt/detekt.yml"))
baseline.set(file("$rootDir/config/detekt/baseline.xml"))
include("**/*.kt")
include("**/*.kts")
exclude("**/resources/**")
exclude("**/build/**")
}
Kotlin DSL
subprojects {
detekt {
// ...
baseline.set(file("${rootProject.projectDir}/config/detekt/baseline.xml"))
// ...
}
}
val detektProjectBaseline by tasks.registering(DetektCreateBaselineTask::class) {
description = "Overrides current baseline."
buildUponDefaultConfig.set(true)
ignoreFailures.set(true)
parallel.set(true)
setSource(files(rootDir))
config.setFrom(files("$rootDir/config/detekt/detekt.yml"))
baseline.set(file("$rootDir/config/detekt/baseline.xml"))
include("**/*.kt")
include("**/*.kts")
exclude("**/resources/**")
exclude("**/build/**")
}
FAQ
Be aware that auto formatting cannot be combined with the baseline.
The signatures for a ; for example would be too ambiguous.