documentation/snapshot/docs/install/integrations.md
By adding the plugin definition below to the <plugins> section in the pom.xml:
ktlint task is bound to the Maven compile lifecycle and will be executed each time the mvn compile is executed. It can also be executed with command mvn exec:exec@ktlint-format.See cli usage for arguments that can be supplied to ktlint.
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<!--
Format and autocorrect lint errors when possible during compile. This helps in committing autocorrect changes in the
same commit as where the original code changes were made.
-->
<id>ktlint-format</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies, also adding the project build directory -->
<classpath/>
<argument>com.pinterest.ktlint.Main</argument>
<!-- Actual wanted arguments to run ktlint with formatting -->
<argument>--format</argument>
<argument>--relative</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-cli</artifactId>
<version>1.8.0</version>
<!-- Use fat jar of ktlint-cli -->
<classifier>all</classifier>
<type>jar</type>
</dependency>
<!-- additional 3rd party ruleset(s) can be specified here -->
</dependencies>
</plugin>
...
!!! Info "ktlint-maven-plugin" You might be interested to use the dedicated gantsign/ktlint-maven-plugin.
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:
ktlintCheck is bound to the Gradle check task. It can also be executed with command ./gradlew ktlintCheck.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.8.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
}
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"
// Suppress "sun.misc.Unsafe::objectFieldOffset" on Java24 (warning) (https://github.com/pinterest/ktlint/issues/2973)
// jvmArgs("--sun-misc-unsafe-memory-access=allow") // Java 24+
// 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.
!!! Warning It is recommended to use one of the Gradle plugins mentioned before.
The configuration below, defines following task:
ktlintCheck is bound to the Gradle check task. It can also be executed with command ./gradlew ktlintCheck.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.8.0") {
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")
// Suppress "sun.misc.Unsafe::objectFieldOffset" on Java24 (warning) (https://github.com/pinterest/ktlint/issues/2973)
jvmArgs("--sun-misc-unsafe-memory-access=allow") // Java 24+
// see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
args(
"-F",
"**/src/**/*.kt",
"**.kts",
"!**/build/**",
)
}
See w0rp/ale.
The Mega-Linter integrates 70+ linters in a single tool for CI, including ktlint activated out of the box
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.
Do you know any other integration with ktlint then please create a PR to add this integration to our documentation.