Detekt is an open-source static code analyzer developed by the Kotlin community. It's flexible and easy to extend with custom rules, which helps you track anti-patterns and fix them in your code base.
Integration
To use detekt in your project, you need to follow a few simple steps:
First, enable the detekt plugin in the build.gradle.kts
file at the root of the project. You can find the plugin version you need here.
plugins {
id("io.gitlab.arturbosch.detekt") version "1.23.7"
}
Then, download the rules configuration from the detekt GitHub repository and place it in your project directory or wherever you prefer.
Next, set the path to the detekt configuration file in build.gradel.kts
file.
detekt {
basePath = projectDir.path
config.setFrom("detekt/config.yml")
buildUponDefaultConfig = false
parallel = true
}
Finally, learn about detekt rules and customize the configuration as needed.
Some custom detekt configurations for example:
Disable rule:
SomeProperty:
active: false
Disable rule for files and folders.
SomeProperty:
active: true
excludes: ['**/test/**', '**/*SomeClass*']
How to run
There are several ways to run detekt analyze.
Execute cli command in the root directory of your project
gradle detekt
or
gradle check
You can also start detekt in the gradle menu of IntelliJ IDEA:
Examples of detekt output
Failure report in the terminal:
terminal:~/IdeaProjects/template$ gradle detekt
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :detekt FAILED
/home/mikhail/IdeaProjects/template/src/main/kotlin/ru/epatko/template/Application.kt:10:29: In most cases using a spread operator causes a full copy of the array to be created before calling a method. This may result in a performance penalty. [SpreadOperator]
FAILURE: Build failed with an exception.
Success report in the terminal:
terminal:~/IdeaProjects/template$ gradle detekt
BUILD SUCCESSFUL in 780ms
1 actionable task: 1 executed
You can also find the detekt report with various code metrics in the <project-root-directory>/build/detekt/detekt.html
file.
Conclusion
I hope that these simple steps, outlined in the configuration and usage instructions, will allow you to quickly integrate detekt into your projects and use its capabilities to improve the quality of your code and maintain it.
The entire test project is available on GitHub.
Top comments (0)