• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

jeremymailen/kotlinter-gradle: Painless, fast ktlint plugin for Gradle

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

jeremymailen/kotlinter-gradle

开源软件地址(OpenSource Url):

https://github.com/jeremymailen/kotlinter-gradle

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

Kotlinter Gradle

Build Status Latest Version

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

It aims to be easy to set up with zero required configuration and behaves as you'd expect out of the box.

It's also fast because it integrates the ktlint engine directly with Gradle's incremental build and uses the Worker API to parallelize work.

Installation

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

Single module

Kotlin
plugins {
    id("org.jmailen.kotlinter") version "3.11.1"
}
Groovy
plugins {
    id "org.jmailen.kotlinter" version "3.11.1"
}

Multi-module and Android

Kotlin Root `build.gradle.kts`
plugins {
    id("org.jmailen.kotlinter") version "3.11.1" apply false
}

Each module build.gradle.kts with Kotlin source

plugins {
    id("org.jmailen.kotlinter")
}
Groovy Root `build.gradle`
plugins {
    id 'org.jmailen.kotlinter' version "3.11.1" apply false
}

Each module build.gradle with Kotlin source

plugins {
    id 'org.jmailen.kotlinter'
}

Compatibility

kotlinter version min kotlin version max kotlin version min gradle version
3.11.0+ 1.7.0 - 7.0
3.10.0+ 1.6.20 1.6.21 7.0
3.7.0+ 1.5.31 1.6.10 7.0
3.5.0+ 1.5.0 1.5.30 6.8
3.0.0+ 1.4.0 1.4.30 6.8
2.0.0+ 1.3.0 1.3.30 -

Features

  • Supports Kotlin Gradle plugins:
  • Supports .kt and .kts files
  • Standalone LintTask and FormatTask types for defining custom tasks
  • Incremental build support and fast parallelization with Gradle Worker API
  • Configures from .editorconfig when available
  • Configurable reporters

Tasks

When your project uses one of the supported Kotlin Gradle plugins, Kotlinter adds these tasks:

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

Git Hooks

Kotlinter can install a hook to run pre-push (installKotlinterPrePushHook). The hook runs lintKotlin and, if there are errors, formatKotlin and exits non-zero leaving changed files to be committed.

You must apply the kotlinter plugin to your root project to make this task available. If using git worktree you must install the hook from the parent git directory.

To install the hook automatically when someone runs the build, add this to your root project build.gradle.kts:

Kotlin
tasks.check {
    dependsOn("installKotlinterPrePushHook")
}
Groovy
tasks.named('check') {
    dependsOn 'installKotlinterPrePushHook'
}

Configuration

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

Kotlin
kotlinter {
    ignoreFailures = false
    reporters = arrayOf("checkstyle", "plain")
    experimentalRules = false
    disabledRules = emptyArray()
}
Groovy
kotlinter {
    ignoreFailures = false
    reporters = ['checkstyle', 'plain']
    experimentalRules = false
    disabledRules = []
}

Options for reporters: checkstyle, html, json, plain, sarif

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

The experimentalRules property enables rules which are part of ktlint's experimental rule set.

The disabledRules property can includes an array of rule ids you wish to disable. For example to allow wildcard imports:

disabledRules = ["no-wildcard-imports"]

You must prefix rule ids not part of the standard rule set with <rule-set-id>:<rule-id>. For example experimental:annotation.

Editorconfig

Kotlinter will configure itself using an .editorconfig file if one is present.

If a non-empty disabledRules value is specified in the kotlinter extension, it will take precedence over any disabled_rules in .editorconfig.

See Ktlint editorconfig for supported values.

Customizing Tasks

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

Kotlin
tasks.lintKotlinMain {
  exclude("com/example/**/generated/*.kt")
}
Groovy
tasks.named('lintKotlinMain') {
    exclude 'com/example/**/generated/*.kt'
}

Note that exclude paths are relative to the package root.

Custom Tasks

If you aren't using autoconfiguration from a supported plugin or otherwise need to handle additional source code, you can create custom tasks:

Kotlin
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask

tasks.register<LintTask>("ktLint") {
    group = "verification"
    source(files("src"))
    reports.set(
        mapOf(
            "plain" to file("build/lint-report.txt"),
            "json" to file("build/lint-report.json")
        )
    )
}

tasks.register<FormatTask>("ktFormat") {
    group = "formatting"
    source(files("src"))
    report.set(file("build/format-report.txt"))
}
Groovy
import org.jmailen.gradle.kotlinter.tasks.LintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask

tasks.register('ktLint', LintTask) {
    group 'verification'
    source files('src')
    reports = [
            'plain': file('build/lint-report.txt'),
            'json' : file('build/lint-report.json')
    ]
    disabledRules = ['import-ordering']
}


tasks.register('ktFormat', FormatTask) {
  group 'formatting'
  source files('src/test')
  report = file('build/format-report.txt')
  disabledRules = ['import-ordering']
}

Custom ktlint version

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

Kotlin
buildscript {
    configurations.classpath {
        resolutionStrategy {
            force(
                "com.pinterest.ktlint:ktlint-core:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-checkstyle:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-json:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-html:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-plain:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-sarif:0.39.0",
                "com.pinterest.ktlint:ktlint-ruleset-experimental:0.39.0",
                "com.pinterest.ktlint:ktlint-ruleset-standard:0.39.0"
            )
        }
    }
}
Groovy
buildscript {
    configurations.classpath {
        resolutionStrategy {
            force(
                "com.pinterest.ktlint:ktlint-core:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-checkstyle:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-json:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-html:0.39.0",
                "com.pinterest.ktlint:ktlint-reporter-plain:0.39.0", 
                "com.pinterest.ktlint:ktlint-reporter-sarif:0.39.0",
                "com.pinterest.ktlint:ktlint-ruleset-experimental:0.39.0",
                "com.pinterest.ktlint:ktlint-ruleset-standard:0.39.0"
            )
        }
    }
}

Custom Rules

You can add custom ktlint RuleSets using the buildscript classpath:

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



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Jetpack-Missionary/Jetpack-From-Java-To-Kotlin: 本项目专注于提供 Jetpack 核心组 ...发布时间:2022-08-13
下一篇:
breandan/kotlingrad: 发布时间:2022-08-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap