Maven integration
By adding the plugin definition below to the <plugins>
section in the pom.xml
:
- The
ktlint
task is bound to the Maven verify lifecycle and will be executed each time themvn verify
is executed. It can also be executed with commandmvn antrun:run@ktlint
. - The
ktlint-format
task is not bound to any other maven lifecycle. It can be executed with commandmvn antrun:run@ktlint-format
.
See cli usage for arguments that can be supplied to ktlint
.
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>ktlint</id>
<phase>verify</phase>
<configuration>
<target name="ktlint">
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
<arg value="src/**/*.kt"/>
<!-- see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information -->
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>ktlint-format</id>
<configuration>
<target name="ktlint">
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true"
classpathref="maven.plugin.classpath" classname="com.pinterest.ktlint.Main">
<arg value="-F"/>
<arg value="src/**/*.kt"/>
<!-- see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information -->
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.pinterest</groupId>
<artifactId>ktlint</artifactId>
<version>0.46.1</version>
</dependency>
<!-- additional 3rd party ruleset(s) can be specified here -->
</dependencies>
</plugin>
...
!!! Tip
If you want ktlint to run before code compilation takes place - change <phase>verify</phase>
to <phase>validate</phase>
(see Maven Build Lifecycle for more).
!!! Info "ktlint-maven-plugin" You might be interested to use the dedicated gantsign/ktlint-maven-plugin.
Gradle integration
The jlleitschuh/ktlint-gradle Gradle plugin automatically creates check and format tasks for project Kotlin sources. It supports different kotlin plugins and Gradle build caching.
The jeremymailen/kotlinter-gradle Gradle plugin features incremental build support, file reports, and *.kts
source support.
The diffplug/spotless Gradle plugin is a general-purpose formatting plugin which amongst many others also supports ktlint
.
The autostyle/autostyle Gradle plugin is a general-purpose formatting plugin which amongst others also supports ktlint
.
!!! Warning It is recommended to use one of the Gradle plugins mentioned before.
The configuration below, defines following task:
- The
ktlint
is bound to the Gradle check task. It can also be executed with commandgradle ktlint
. - The
ktlint-format
task is not bound to any other task. It can be executed with commandgradle ktlintFormat
.
// kotlin-gradle-plugin must be applied for configuration below to work
// (see https://kotlinlang.org/docs/reference/using-gradle.html)
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
ktlint
}
dependencies {
ktlint("com.pinterest:ktlint:0.46.1") {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
}
}
// additional 3rd party ruleset(s) can be specified here
// just add them to the classpath (e.g. ktlint 'groupId:artifactId:version') and
// ktlint will pick them up
}
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
mainClass.set("com.pinterest.ktlint.Main")
args "src/**/*.kt"
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
}
check.dependsOn ktlint
task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
mainClass.set("com.pinterest.ktlint.Main")
args "-F", "src/**/*.kt"
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
}
See Making your Gradle tasks incremental by Niklas Baudy on how to make tasks above incremental.
!!! Warning It is recommended to use one of the Gradle plugins mentioned before.
The configuration below, defines following task:
- The
ktlint
is bound to the Gradle check task. It can also be executed with commandgradle ktlint
. - The
ktlint-format
task is not bound to any other task. It can be executed with commandgradle ktlintFormat
.
val ktlint by configurations.creating
dependencies {
ktlint("com.pinterest:ktlint:0.46.1") {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
}
}
// ktlint(project(":custom-ktlint-ruleset")) // in case of custom ruleset
}
val outputDir = "${project.buildDir}/reports/ktlint/"
val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt"))
val ktlintCheck by tasks.creating(JavaExec::class) {
inputs.files(inputFiles)
outputs.dir(outputDir)
description = "Check Kotlin code style."
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args = listOf("src/**/*.kt")
}
val ktlintFormat by tasks.creating(JavaExec::class) {
inputs.files(inputFiles)
outputs.dir(outputDir)
description = "Fix Kotlin code style deviations."
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args = listOf("-F", "src/**/*.kt")
}
IntelliJ IDEA integration
!!! Warning
ktlint
strives to prevent code formatting conflicts with IntelliJ IDEA / Android Studio. We recommend using either IDE formatting or ktlint
formatting. However, if you persist on using both, then please ensure that the formatting settings are aligned as described below. This reduces the chance that code which is formatted by ktlint conflicts with formatting by the IntelliJ IDEA built-in formatter.
Choose any of options below to align the formatting settings of IntelliJ IDEA.
Use ktlint cli to change the code style settings of a single project with any of the commands below.
# Run command below from root directory of project
ktlint applyToIDEAProject
Or if you want to use android specific code style:
# Run command below from root directory of project
ktlint --android applyToIDEAProject
Use ktlint cli to change the code style settings of all projects with any of the commands below.
ktlint applyToIDEA
Or if you want to use android specific code style:
ktlint --android applyToIDEA
Create or update the code style config with .editorconfig
by setting properties below:
[{*.kt,*.kts}]
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_line_comment_at_first_column = false
ij_kotlin_line_comment_add_space = true
# These options can keep to use single name import
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_keep_blank_lines_in_declarations = 1
ij_kotlin_keep_blank_lines_in_code = 1
ij_kotlin_keep_blank_lines_before_right_brace = 0
# optional but recommended
ij_kotlin_align_multiline_parameters = false
# optional but recommended
ij_continuation_indent_size = 4
# Android specific rules
ij_kotlin_import_nested_classes = false
ij_kotlin_imports_layout = *,^
!!! Warning
ktlint
is not reading the IntelliJ IDEA preferences. If your settings are out of sync with the configuration in .editorconfig
or the default values of ktlint
, this results in formatting differences.
Go to File -> Settings... -> Editor
- General -> Auto Import
- check
Kotlin
/Optimize imports on the fly (for current project)
.
- check
- Code Style -> Kotlin
- Set from... on the right -> (Predefined style) -> Kotlin style guide (Kotlin plugin 1.2.20+).
- open Code Generation tab
- uncheck
Line comment at first column
; - select
Add a space at comment start
.
- uncheck
- open Imports tab
- select
Use single name import
(all of them); - remove
import java.util.*
fromPackages to Use Import with '*'
.
- select
- open Blank Lines tab
- change
Keep Maximum Blank Lines
/In declarations
&In code
to 1 andBefore '}'
to 0.
- change
- (optional but recommended) open Wrapping and Braces tab
- uncheck
Function declaration parameters
(ORMethods declartion parameters
for older version) /Align when multiline
.
- uncheck
- (optional but recommended) open Tabs and Indents tab
- change
Continuation indent
to the same value asIndent
(4 by default).
- change
- Inspections
- change
Severity
level ofUnused import directive
andRedundant semicolon
underKotlin
->Redundant constructs
toERROR
.
- change
GNU Emacs integration
Vim integration
See w0rp/ale.
Mega-Linter integration
The Mega-Linter integrates 70+ linters in a single tool for CI, including ktlint activated out of the box
Do you know any other integration with ktlint
then please create a PR to add this integration to our documentation.