/kotlinter-gradle

Kotlin lint gradle plugin using ktlint

Primary LanguageKotlinApache License 2.0Apache-2.0

Kotlinter Gradle

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

plugins {
    id 'org.jmailen.kotlinter' version '1.12.0'
}

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:

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).

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.

lintKotlinMain {
    exclude '**/*Generated.kt'
}

Custom ktlint version

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

buildscript {
    configurations.classpath {
        resolutionStrategy { force 'com.github.shyiko:ktlint:0.19.0' }
    }
}

Custom Rules

You can add custom ktlint RuleSets using the buildscript classpath:

buildscript {
    dependencies {
        classpath files('libs/my-custom-ktlint-rules.jar')
        classpath 'org.other.ktlint:custom-rules:1.0'
    }
}