Logo

dev-resources.site

for different kinds of informations.

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

Published at
12/30/2024
Categories
kotlin
springboot
detekt
codequality
Author
mikhailepatko
Author
13 person written this
mikhailepatko
open
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.

kotlin Article's
30 articles in total
Favicon
Using SVGs on Canvas with Compose Multiplatform
Favicon
Kotlin Generics Simplified
Favicon
Understanding Quick Sort in Kotlin : A Beginner's Guide
Favicon
Understanding Selection Sort in Kotlin: A Beginner's Guide
Favicon
Wednesday Links - Edition 2025-01-08
Favicon
Testing Pi Logic integrations.
Favicon
Understanding (a bit of) the Gradle Kotlin DSL
Favicon
Android TreeView(Hierarchy based) with Kotlin
Favicon
Creando un notebook con Jupyter y Kotlin
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 3/3)
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 2/3)
Favicon
Understanding Room Database in Android: A Beginner's Guide
Favicon
Fixing Rounded Navigation Bar Corner Padding in Jetpack Compose
Favicon
Getting Started with Android Testing: Building Reliable Apps with Confidence (Part 1/3)
Favicon
My conference year
Favicon
Authentication in Android Project with Firebase.
Favicon
Learn Firebase for Android Development from Scratch, a beginner guide.
Favicon
๐Ÿงน Improve Filtering with the Predicate Interface!
Favicon
How to make the best of a slow machine running on limited resources with a Windows environment as a Java Engineer
Favicon
How to implement detekt in Spring Boot + Kotlin + Gradle project
Favicon
How to Create and Publish an Android Library for Beginners
Favicon
Pub-sub Redis in Micronaut
Favicon
ISBN Stacks โ€” A look at a possible Spring Application implementation without annotations
Favicon
Protecting Applications with Kong security plugins and using StatsD to monitor system states โ€” A healthy camera story
Favicon
Configurable Kong API Gateway with Micronaut Services in Kotlin โ€” A very odd Yucca tribute concert
Favicon
Learning AWS with Localstack and Reactive Kotlin โ€” A stamps and coins implementation
Favicon
Coroutines, Distributed Cache, Resilience, and Replication in Kotlin โ€” Making a VMAโ€™s application
Favicon
From Paris to Berlin โ€” Creating Circuit-Breakers in Kotlin
Favicon
Understanding Merge Sort in Kotlin: A Beginner's Guide
Favicon
City Library โ€” An advanced guide to Circuit Breakers in Kotlin

Featured ones: