kotlinter-gradle

Introduction: Kotlin lint gradle plugin using ktlint
More: Author   ReportBugs   
Tags:

Build Status

Gradle plugin for linting and formatting Kotlin source files using the awesome ktlint engine.

Installation

Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jmailen.kotlinter

Single module

Kotlin kotlin plugins { id("org.jmailen.kotlinter") version "1.20.1" }
Groovy groovy plugins { id "org.jmailen.kotlinter" version "1.20.1" }

Multi-module and Android

Kotlin Root build.gradle.kts kotlin buildscript { repositories { maven { url = uri("https://plugins.gradle.org/m2/") } } dependencies { classpath("org.jmailen.gradle:kotlinter-gradle:1.20.1") } } Each module build.gradle.kts with Kotlin source kotlin apply(plugin = "org.jmailen.kotlinter")
Groovy Root build.gradle groovy buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "org.jmailen.gradle:kotlinter-gradle:1.20.1" } } Each module build.gradle with Kotlin source groovy apply plugin: "org.jmailen.kotlinter"

Compatibility

Kotlinter 1.12.0 and later compatible with Kotlin Gradle plugins 1.2.41+ and Java 9/8.

Kotlinter 1.8.0 and later compatible with Kotlin Gradle plugins 1.2.21+ and Java 9/8.

Kotlinter 1.7.0 and later compatible with Kotlin Gradle plugins 1.2.20+

Kotlinter 1.4.0 and later compatible with Kotlin Gradle plugins 1.1.50+

Kotlinter 1.2.0 and later compatible with Kotlin Gradle plugins 1.1.3+

Kotlinter 1.1.0 and earlier compatible with Kotlin Gradle plugins 1.1 - 1.1.2-5

Features

  • Extends Kotlin JVM and Android projects with lint and format tasks for each SourceSet
  • Standalone LintTask and FormatTask types for defining custom tasks
  • Incremental build support
  • .kt and .kts source support
  • Console output and configurable reporters

Tasks

If your project uses the JetBrains Kotlin JVM or Android Gradle plugins, standard tasks are created:

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 exist for each source set in the project: formatKotlinSourceSet and lintKotlinSourceSet.

Custom Tasks

If you haven't applied these plugins you can create custom tasks:

Kotlin kotlin import org.jmailen.gradle.kotlinter.tasks.LintTask import org.jmailen.gradle.kotlinter.tasks.FormatTask val ktLint by tasks.creating(LintTask::class) { group = "verification" source(files("src")) reports = mapOf( "plain" to file("build/lint-report.txt"), "json" to file("build/lint-report.json") ) } val ktFormat by tasks.creating(FormatTask::class) { group = "formatting" source(files("src")) report = file("build/format-report.txt") }
Groovy 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') ] } task ktFormat(type: FormatTask, group: 'formatting') { source files('src') report = file('build/format-report.txt') }

Configuration

Options are configured in the kotlinter extension. Defaults shown (you may omit the configuration block entirely if you want these values).

Kotlin kotlin kotlinter { ignoreFailures = false indentSize = 4 continuationIndentSize = 4 reporter = arrayOf("checkstyle", "plain") }
Groovy groovy kotlinter { ignoreFailures = false indentSize = 4 continuationIndentSize = 4 reporters = ['checkstyle', 'plain'] }

Options for reporters: checkstyle, html, json, plain

The html reporter is provided by ktlint-html-reporter.

Reporters behave as described at: https://github.com/shyiko/ktlint

*Note: reporter with a single value is deprecated but supported for backwards compatibility.

Customizing Tasks

The formatKotlinSourceSet and lintKotlinSourceSet tasks inherit from SourceTask so you can customize includes, excludes, and source.

Kotlin kotlin import org.jmailen.gradle.kotlinter.tasks.LintTask tasks { "lintKotlinMain"(LintTask::class) { exclude("**/*Generated.kt") } }
Groovy groovy lintKotlinMain { exclude '**/*Generated.kt' }

Custom ktlint version

If you need to use a different version of ktlint you can override the dependency.

Kotlin kotlin buildscript { configurations.classpath .resolutionStrategy.force("com.github.shyiko:ktlint:0.28.0") }
Groovy groovy buildscript { configurations.classpath { resolutionStrategy { force 'com.github.shyiko:ktlint:0.28.0' } } }

Custom Rules

You can add custom ktlint RuleSets using the buildscript classpath:

Kotlin kotlin buildscript { dependencies { classpath(files("libs/my-custom-ktlint-rules.jar")) classpath("org.other.ktlint:custom-rules:1.0") } }
Groovy groovy buildscript { dependencies { classpath files('libs/my-custom-ktlint-rules.jar') classpath 'org.other.ktlint:custom-rules:1.0' } }
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea