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">
<!-- Note: the JVM arg below is only required when running ktlint with Java 16+ in format mode.
<jvmarg value="--add-opens=java.base/java.lang=ALL-UNNAMED"/>
-->
<!-- see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information -->
<arg value="src/**/*.kt"/>
</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">
<!-- Note: the JVM args below is only required when running ktlint with Java 16+ in format mode -->
<jvmarg value="--add-opens=java.base/java.lang=ALL-UNNAMED"/>
<!-- see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information -->
<arg value="-F"/>
<arg value="src/**/*.kt"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-cli</artifactId>
<version>1.4.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).
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
ktlintCheck
is bound to the Gradle check task. It can also be executed with command./gradlew ktlintCheck
. - The
ktlintFormat
task is not bound to any other task. It can be executed with command./gradlew ktlintFormat
.
// kotlin-gradle-plugin must be applied for configuration below to work
// (see https://kotlinlang.org/docs/reference/using-gradle.html)
plugins {
id 'java'
}
repositories {
mavenCentral()
}
configurations {
ktlint
}
dependencies {
ktlint("com.pinterest.ktlint:ktlint-cli:1.4.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
}
tasks.register("ktlintCheck", JavaExec) {
group = "verification"
description = "Check Kotlin code style."
classpath = configurations.ktlint
mainClass = "com.pinterest.ktlint.Main"
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args "src/**/*.kt", "**.kts", "!**/build/**"
}
tasks.named("check") {
dependsOn tasks.named("ktlintCheck")
}
tasks.register("ktlintFormat", JavaExec) {
group = "formatting"
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
mainClass = "com.pinterest.ktlint.Main"
jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args "-F", "src/**/*.kt", "**.kts", "!**/build/**"
}
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
ktlintCheck
is bound to the Gradle check task. It can also be executed with command./gradlew ktlintCheck
. - The
ktlintFormat
task is not bound to any other task. It can be executed with command./gradlew ktlintFormat
.
val ktlint by configurations.creating
dependencies {
ktlint("com.pinterest.ktlint:ktlint-cli:1.4.1") {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
}
}
// ktlint(project(":custom-ktlint-ruleset")) // in case of custom ruleset
}
val ktlintCheck by tasks.registering(JavaExec::class) {
group = LifecycleBasePlugin.VERIFICATION_GROUP
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(
"**/src/**/*.kt",
"**.kts",
"!**/build/**",
)
}
tasks.check {
dependsOn(ktlintCheck)
}
tasks.register<JavaExec>("ktlintFormat") {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Check Kotlin code style and format"
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args(
"-F",
"**/src/**/*.kt",
"**.kts",
"!**/build/**",
)
}
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
TCA integration¶
Tencent Cloud Code Analysis (TCA for short, code-named CodeDog inside the company early) is a comprehensive platform for code analysis and issue tracking. TCA consist of three components, server, web and client. It integrates of a number of self-developed tools, and also supports dynamic integration of code analysis tools in various programming languages.
- Homepage: http://tca.tencent.com/
- Source code: https://github.com/Tencent/CodeAnalysis
- Documentation: https://tencent.github.io/CodeAnalysis
Other integration¶
Do you know any other integration with ktlint
then please create a PR to add this integration to our documentation.