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.

springboot Article's
30 articles in total
Favicon
Launched a Web version of my Project using Java Spring Framework, Spring Boot Web
Favicon
Understanding Spring Security and OAuth 2.0
Favicon
Spring Oauth2 - App-Token based Hybrid Token Verification Methods
Favicon
Static variables in Java
Favicon
What is Spring AI ? Example of a chat API with multiple LLMs
Favicon
Quando usar ResponseEntity?
Favicon
App-Token based easy OAuth2 implementation built to grow with Spring Boot
Favicon
[Boost]
Favicon
Spring Boot 3 application on AWS Lambda - Part 14 Measuring cold and warm starts with GraalVM Native Image and memory settings
Favicon
SpringBoot Web Service - Part 5 - Github Action
Favicon
Apache wicket with spring boot example application: notice board
Favicon
OTP Authentication: The Passwordless Superhero of Your App! πŸ¦Έβ€β™‚οΈβœ¨
Favicon
SpringBoot Web Service - Part 1 - Create Repository
Favicon
SpringBoot Web Service - Part 2 - Preparing Using Spring Initializr
Favicon
Zero Config Spring Batch: Just Write Business Logic
Favicon
Generate a REST API Using Java and Spring Boot for your Postgres database
Favicon
How to optimize SpringBoot startup
Favicon
Spring Boot: About @SpringBootApplication
Favicon
Introduction to Spring Boot: A Complete Guide
Favicon
Wednesday Links - Edition 2025-01-01πŸŽ‰
Favicon
SpringBoot Web Service - Part 4 - Initial Configuration
Favicon
How to implement detekt in Spring Boot + Kotlin + Gradle project
Favicon
Building Resilient APIs: Mistakes I Made and How I Overcame Them
Favicon
Web Scraping Summarization with Vite React TypeScript and Spring Boot
Favicon
Handling NullPointerException with Optional
Favicon
Overview of Lock API in java
Favicon
Optimizing Serverless Lambda with GraalVM Native Image
Favicon
With Spring can I make an optional path variable?
Favicon
Building a Vue CRUD App with a Spring Boot API
Favicon
Hexagonal Architecture β€” A Favorite Lyrics Spring Boot β€” Java Example

Featured ones: