DEV Community

Mikhail
Mikhail

Posted on

How to implement detekt in Spring Boot + Kotlin + Gradle project

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"
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Finally, learn about detekt rules and customize the configuration as needed.
Some custom detekt configurations for example:
Disable rule:

SomeProperty:
  active: false
Enter fullscreen mode Exit fullscreen mode

Disable rule for files and folders.

SomeProperty:
  active: true
  excludes: ['**/test/**', '**/*SomeClass*']
Enter fullscreen mode Exit fullscreen mode

How to run

There are several ways to run detekt analyze.
Execute cli command in the root directory of your project

gradle detekt
Enter fullscreen mode Exit fullscreen mode

or

gradle check
Enter fullscreen mode Exit fullscreen mode

You can also start detekt in the gradle menu of IntelliJ IDEA:

IntelliJ IDEA menu

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.
Enter fullscreen mode Exit fullscreen mode

Success report in the terminal:

terminal:~/IdeaProjects/template$ gradle detekt

BUILD SUCCESSFUL in 780ms
1 actionable task: 1 executed
Enter fullscreen mode Exit fullscreen mode

You can also find the detekt report with various code metrics in the <project-root-directory>/build/detekt/detekt.html file.
detekt metrics

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay