Painless Gradle plugin for linting and formatting Kotlin source files using the awesome ktlint engine.
It aims to be easy to set up with zero required configuration and behaves as you'd expect out of the box.
It's also fast because it integrates the ktlint engine directly with Gradle's incremental build and uses the Worker API to parallelize work.
Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jmailen.kotlinter
Kotlin
plugins {
id("org.jmailen.kotlinter") version "3.5.0"
}
Groovy
plugins {
id "org.jmailen.kotlinter" version "3.5.0"
}
Kotlin
Root `build.gradle.kts`buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:3.5.0")
}
}
Each module build.gradle.kts
with Kotlin source
apply(plugin = "org.jmailen.kotlinter")
Groovy
Root `build.gradle`buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:3.5.0"
}
}
Each module build.gradle
with Kotlin source
apply plugin: "org.jmailen.kotlinter"
kotlinter versions | min kotlin version | max kotlin version | min gradle version |
---|---|---|---|
3.5.0+ | 1.5.0 | - | 6.8 |
3.0.0+ | 1.4.0 | 1.4.30 | 6.8 |
2.0.0+ | 1.3.0 | 1.3.30 | - |
- Supports Kotlin Gradle plugins:
- Supports
.kt
and.kts
files - Standalone
LintTask
andFormatTask
types for defining custom tasks - Incremental build support and fast parallelization with Gradle Worker API
- Configures from
.editorconfig
when available - Configurable reporters
When your project uses one of the supported Kotlin Gradle plugins, Kotlinter adds these tasks:
formatKotlin
: format Kotlin source code according to ktlint
rules or warn when auto-format not possible.
lintKotlin
: report Kotlin lint errors and by default fail the build.
Also check
becomes dependent on lintKotlin
.
Granular tasks are added for each source set in the project: formatKotlin
SourceSet
and lintKotlin
SourceSet
.
Kotlinter can install a hook to run pre-push (installKotlinterPrePushHook
). The hook runs lintKotlin
and, if there are errors, formatKotlin
and exits non-zero leaving changed files to be committed.
You must apply the kotlinter plugin to your root project to make this task available. If using git worktree
you must install the hook from the parent git directory.
To install the hook automatically when someone runs the build, add this to your root project build.gradle.kts
:
Kotlin
tasks.check {
dependsOn("installKotlinterPrePushHook")
}
Groovy
check {
dependsOn "installKotlinterPrePushHook"
}
Options are configured in the kotlinter
extension. Defaults shown (you may omit the configuration block entirely if you want these defaults).
Kotlin
kotlinter {
ignoreFailures = false
indentSize = 4
reporters = arrayOf("checkstyle", "plain")
experimentalRules = false
disabledRules = emptyArray<String>()
}
Groovy
kotlinter {
ignoreFailures = false
indentSize = 4
reporters = ['checkstyle', 'plain']
experimentalRules = false
disabledRules = []
}
Options for reporters
: checkstyle
, html
, json
, plain
Reporters behave as described at: https://github.com/pinterest/ktlint
The experimentalRules
property enables rules which are part of ktlint's experimental rule set.
The disabledRules
property can includes an array of rule ids you wish to disable. For example to allow wildcard imports:
disabledRules = ["no-wildcard-imports"]
You must prefix rule ids not part of the standard rule set with <rule-set-id>:<rule-id>
. For example experimental:annotation
.
Kotlinter will configure itself using an .editorconfig
file if one is present.
If a non-empty disabledRules
value is specified in the kotlinter
extension, it will take precedence over any disabled_rules
in .editorconfig
.
See Ktlint editorconfig for supported values.
The formatKotlin
SourceSet
and lintKotlin
SourceSet
tasks inherit from SourceTask
so you can customize includes, excludes, and source.
Kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
tasks {
"lintKotlinMain"(LintTask::class) {
exclude("com/example/**/generated/*.kt")
}
}
Groovy
lintKotlinMain {
exclude 'com/example/**/generated/*.kt'
}
Note that exclude paths are relative to the package root.
If you aren't using autoconfiguration from a supported plugin or otherwise need to handle additional source code, you can create custom tasks:
Kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
tasks.create<LintTask>("ktLint") {
group = "verification"
source(files("src"))
reports = mapOf(
"plain" to file("build/lint-report.txt"),
"json" to file("build/lint-report.json")
)
}
tasks.create<FormatTask>("ktFormat") {
group = "formatting"
source(files("src"))
report = file("build/format-report.txt")
}
Groovy
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
task ktLint(type: LintTask, group: 'verification') {
source files('src')
reports = [
'plain': file('build/lint-report.txt'),
'json': file('build/lint-report.json')
]
disabledRules = ["import-ordering"]
}
task ktFormat(type: FormatTask, group: 'formatting') {
source files('src')
report = file('build/format-report.txt')
disabledRules = ["import-ordering"]
}
If you need to use a different version of ktlint
you can override the dependency.
Kotlin
buildscript {
configurations.classpath {
resolutionStrategy {
force(
"com.pinterest.ktlint:ktlint-core:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-json:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-html:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-plain:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.39.0"
)
}
}
}
Groovy
buildscript {
configurations.classpath {
resolutionStrategy {
force(
"com.pinterest.ktlint:ktlint-core:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-checkstyle:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-json:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-html:0.39.0",
"com.pinterest.ktlint:ktlint-reporter-plain:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-experimental:0.39.0",
"com.pinterest.ktlint:ktlint-ruleset-standard:0.39.0"
)
}
}
}
You can add custom ktlint RuleSets using the buildscript
classpath:
Kotlin
buildscript {
dependencies {
classpath(files("libs/my-custom-ktlint-rules.jar"))
classpath("org.other.ktlint:custom-rules:1.0")
}
}
Groovy
buildscript {
dependencies {
classpath files('libs/my-custom-ktlint-rules.jar')
classpath 'org.other.ktlint:custom-rules:1.0'
}
}