Integrations
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.48.0</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).
ktlint-maven-plugin
You might be interested to use the dedicated gantsign/ktlint-maven-plugin.
Gradle integration¶
jlleitschuh/ktlint-gradle¶
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.
jeremymailen/kotlinter-gradle¶
The jeremymailen/kotlinter-gradle Gradle plugin features incremental build support, file reports, and *.kts
source support.
diffplug/spotless¶
The diffplug/spotless Gradle plugin is a general-purpose formatting plugin which amongst many others also supports ktlint
.
autostyle/autostyle¶
The autostyle/autostyle Gradle plugin is a general-purpose formatting plugin which amongst others also supports ktlint
.
Custom Gradle integration¶
Custom Gradle integration with Groovy¶
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.48.0") {
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.
Custom Gradle integration with Kotlin DSL¶
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.48.0") {
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")
}
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
Other integration¶
Do you know any other integration with ktlint
then please create a PR to add this integration to our documentation.