diktat

diktat

严格的Kotlin代码规范与自动修复工具

diKTat是一款严格的Kotlin代码规范工具,基于KTlint开发。它提供100多项代码风格检查和自动修复功能,可集成到CI/CD流程中检测和修复代码异味。diKTat具有独特的检查项、高度可配置性和详细的代码风格指南,适合需要执行统一编码标准的团队使用。工具支持Maven、Gradle等构建系统,并能与GitHub Actions集成,提供原生问题报告。

diKTatKotlin代码风格静态分析CI/CDGithub开源项目
<img src="/logo.svg" width="64px"/>

Build and test deteKT static analysis diKTat code style codecov

Releases Maven Central FOSSA Status Chat on Telegram

Hits-of-Code Lines of code GitHub repo size Awesome Kotlin Badge

DiKTat is a strict coding standard for Kotlin, consisting of a collection of Kotlin code style rules implemented as Abstract Syntax Tree (AST) visitors built on top of KTlint. It serves the purpose of detecting and automatically fixing code smells in the Continuous Integration/Continuous Deployment (CI/CD) process. You can find the comprehensive list of supported rules and inspections here.

DiKTat has gained recognition and has been added to the lists of static analysis tools, kotlin-awesome, and kompar. We extend our gratitude to the community for this support!

See first

CodestyleInspectionsExamplesDemoWhite PaperGroups of Inspections

Why Choose DiKTat for CI/CD?

While there are other tools like detekt and ktlint performing static analysis, you might wonder why DiKTat is necessary. Here are the key reasons:

  1. More Inspections: DiKTat boasts over 100 inspections tightly coupled with its Codestyle.

  2. Unique Inspections: DiKTat introduces unique inspections not found in other linters.

  3. Highly Configurable: Every inspection is highly configurable, allowing customization and suppression. Check configuration options and suppression.

  4. Strict Codestyle: DiKTat enforces a detailed Codestyle that can be adopted and applied in your project.

Run as CLI-application

Download binary

  1. Download diKTat manually: here

    OR use curl:

    curl -sSLO https://github.com/saveourtool/diktat/releases/download/v2.0.0/diktat && chmod a+x diktat

For Windows only. Download diKTat.cmd manually: here

Run diKTat

Finally, run KTlint (with diKTat injected) to check your '*.kt' files in 'dir/your/dir':

$ ./diktat "dir/your/dir/**/*.kt"

On Windows

diktat.bat "dir/your/dir/**/*.kt"

To autofix all code style violations, use --mode fix option.

Run with Maven using diktat-maven-plugin

You can see how it is configured in our examples:

<details> <summary>Add this plugin to your pom.xml:</summary>
<plugin> <groupId>com.saveourtool.diktat</groupId> <artifactId>diktat-maven-plugin</artifactId> <version>${diktat.version}</version> <executions> <execution> <id>diktat</id> <phase>none</phase> <goals> <goal>check</goal> <goal>fix</goal> </goals> <configuration> <inputs> <input>${project.basedir}/src/main/kotlin</input> <input>${project.basedir}/src/test/kotlin</input> </inputs> <diktatConfigFile>diktat-analysis.yml</diktatConfigFile> <excludes> <exclude>${project.basedir}/src/test/kotlin/excluded</exclude> </excludes> </configuration> </execution> </executions> </plugin>
</details>

To run diktat in only-check mode use command $ mvn diktat:check@diktat. To run diktat in autocorrect mode use command $ mvn diktat:fix@diktat.

Requesting a specific Maven executionId on the command line (the trailing diktat in the above example) may be essential in these cases:

  • In your pom.xml, you have multiple executions with different configurations (e. g.: multiple rule sets):

    <details>
    <executions> <execution> <id>diktat-basic</id> <configuration> <diktatConfigFile>diktat-analysis.yml</diktatConfigFile> </configuration> </execution> <execution> <id>diktat-advanced</id> <configuration> <diktatConfigFile>diktat-analysis-advanced.yml</diktatConfigFile> </configuration> </execution> </executions>
    </details>
  • Your YAML file with DiKTat rules has a non-default name and/or resides in a non-default location:

    <details>
    <executions> <execution> <id>diktat</id> <configuration> <diktatConfigFile>/non/default/rule-set-file.yml</diktatConfigFile> </configuration> </execution> </executions>
    </details>
    • You can omit the diktatConfigFile or if it points to non-existed file then DiKTat runs with default configuration.

If you omit the executionId:

$ mvn diktat:check

— the plug-in will use the default configuration and search for diktat-analysis.yml file in the project directory (you can still customize the rule sets by editing the YAML file).

Run with Gradle using diktat-gradle-plugin

Requires a gradle version no lower than 7.0

You can see how the plugin is configured in our examples:

<details> <summary>Add this plugin to your `build.gradle.kts`:</summary>
plugins { id("com.saveourtool.diktat") version "2.0.0" }

Note If you want to apply the plugin to multi-module projects"

import com.saveourtool.diktat.plugin.gradle.DiktatGradlePlugin plugins { id("com.saveourtool.diktat") version "2.0.0" apply false } allprojects { apply<DiktatGradlePlugin>() }

You can then configure diktat using diktat extension:

diktat { inputs { include("src/**/*.kt") // path matching this pattern (per PatternFilterable) that will be checked by diktat exclude("src/test/kotlin/excluded/**") // path matching this pattern will not be checked by diktat } debug = true // turn on debug logging }

Also in diktat extension you can configure different reporters and their output. You can specify json, html, sarif, plain (default). If output is set, it should be a file path. If not set, results will be printed to stdout. You can specify multiple reporters. If no reporter is specified, plain will be used with stdout as output.

diktat { reporters { plain() json() html { output = file("someFile.html") } // checkstyle() // sarif() // gitHubActions() } }
</details>

You can run diktat checks using task ./gradlew diktatCheck and automatically fix errors with task ./gradlew diktatFix.

Run with Spotless

Spotless is a linter aggregator.

Gradle

Diktat can be run via spotless-gradle-plugin since version 5.10.0

<details> <summary>Add this plugin to your build.gradle.kts</summary>
plugins { id("com.diffplug.spotless") version "5.10.0" } spotless { kotlin { diktat() } kotlinGradle { diktat() } }
</details> <details> <summary>You can provide a version and configuration path manually as configFile.</summary>
spotless { kotlin { diktat("2.0.0").configFile("full/path/to/diktat-analysis.yml") } }
</details>

Maven

Diktat can be run via spotless-maven-plugin since version 2.8.0

<details> <summary>Add this plugin to your pom.xml</summary>
<plugin> <groupId>com.diffplug.spotless</groupId> <artifactId>spotless-maven-plugin</artifactId> <version>${spotless.version}</version> <configuration> <kotlin> <diktat /> </kotlin> </configuration> </plugin>
</details> <details> <summary>You can provide a version and configuration path manually as configFile</summary>
<diktat> <version>2.0.0</version> <!-- optional --> <configFile>full/path/to/diktat-analysis.yml</configFile> <!-- optional, configuration file path --> </diktat>
</details>

GitHub Integration

We suggest everyone to use common "sarif" format as a reporter in CI/CD. GitHub has an integration with SARIF format and provides you a native reporting of diktat issues in Pull Requests.

img.png

<details> <summary> Github Integration</summary> 1) Add the following configuration to your project's setup for GitHub Actions:

Gradle Plugin:

githubActions = true

Maven Plugin (pom.xml):

<githubActions>true</githubActions>

Maven Plugin (cli options):

mvn -B diktat:check@diktat -Ddiktat.githubActions=true
  1. Add the following code to your GitHub Action to upload diktat SARIF report (after it was generated):
- name: Upload SARIF to Github using the upload-sarif action uses: github/codeql-action/upload-sarif@v1 if: ${{ always() }} with: sarif_file: ${{ github.workspace }}

Note: codeql-action/upload-sarif limits the number of uploaded files at 15. If your project has more than 15 subprojects, the limit will be exceeded and the step will fail. To solve this issue one can merge SARIF reports.

diktat-gradle-plugin provides this capability with mergeDiktatReports task. This task aggregates reports of all diktat tasks of all Gradle project, which produce SARIF reports, and outputs the merged report into root project's build directory. Then this single file can be used as an input for GitHub action:

with: sarif_file: build/reports/diktat/diktat-merged.sarif
</details>

<a name="config"></a> Customizations via diktat-analysis.yml

In Diktat we have supported diktat-analysis.yml that can be easily changed and help in customization of your own rule set. It has simple fields: name — name of the rule, enabled (true/false) — to enable or disable that rule (all rules are enabled by the default), configuration — a simple map of some extra unique configurations for this particular rule. For example:

- name: HEADER_MISSING_OR_WRONG_COPYRIGHT # all rules are enabled by the default. To disable add 'enabled: false' to the config. enabled: true configuration: isCopyrightMandatory: true copyrightText: Copyright (c) Jeff Lebowski, 2012-2020. All rights reserved.

Note, that you can specify and put diktat-analysis.yml that contains configuration of diktat in the parent directory of your project on the same level where build.gradle/pom.xml is stored.
See default configuration in diktat-analysis.yml
Also see the list of all rules supported by diKTat.

<a name="suppress"></a> Suppress warnings/inspections

<details> <summary>Suppress warnings on individual code blocks</summary> In addition to enabling/disabling warning globally via config file (`enable = false`), you can suppress warnings by adding `@Suppress` annotation on individual code blocks or `@file:Suppress()` annotation on a file-level.

For example:

@Suppress("FUNCTION_NAME_INCORRECT_CASE") class SomeClass { fun methODTREE(): String { } }
</details> <details> <summary>Disable all inspections on selected code blocks</summary> Also you can suppress **all** warnings by adding `@Suppress("diktat")` annotation on individual code blocks.

For example:

@Suppress("diktat") class SomeClass { fun methODTREE(): String { } }
</details> <details> <summary>ignoreAnnotated: disable inspections on blocks with predefined annotation</summary> In the `diktat-analysis.yml` file for each inspection it is possible to define a list of annotations that will cause disabling of the inspection on that particular code block:
- name: HEADER_NOT_BEFORE_PACKAGE enabled: true ignoreAnnotated: [MyAnnotation, Compose, Controller]
</details> <details> <summary>Suppress groups of inspections by chapters</summary> It is easy to suppress even groups of inspections in diKTat.

These groups are linked to chapters of Codestyle.

To disable chapters, you will need to add the following configuration to common configuration (- name: DIKTAT_COMMON):

disabledChapters: "1, 2, 3"

Mapping of inspections to chapters can be found in Groups of Inspections.

</details>

Running against the baseline

When setting up code style analysis on a large existing project, one often doesn't have an ability to fix all findings at once. To allow gradual adoption, diktat and ktlint support baseline mode. When running ktlint for the first time with active baseline, the baseline file will be generated. It is a xml file with a complete list of findings by the tool. On later invocations, only the findings that are not in the baseline file will be reported. Baseline can be activated with CLI flag:

./diktat --baseline=diktat-baseline.xml

编辑推荐精选

音述AI

音述AI

全球首个AI音乐社区

音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。

lynote.ai

lynote.ai

一站式搞定所有学习需求

不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。

AniShort

AniShort

为AI短剧协作而生

专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。

seedancetwo2.0

seedancetwo2.0

能听懂你表达的视频模型

Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。

nano-banana纳米香蕉中文站

nano-banana纳米香蕉中文站

国内直接访问,限时3折

输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

下拉加载更多