ktlint不检查kotlin文件

问题描述

我希望我的项目对所有kotlin文件执行ktlintCheck,但仅对build.gradle.kts文件进行检查。 build.gradle.kts文件如下

ktlint {
    version.set("9.4.0")
    debug.set(true)
    verbose.set(true)
    android.set(false)
    outputToConsole.set(true)
    reporters {
        reporter(ReporterType.PLAIN)
        reporter(ReporterType.CHECKSTYLE)
    }
    ignoreFailures.set(false)
    kotlinScriptAdditionalPaths {
        include(filetree("src/"))
    }
    filter {
        exclude("**/generated/**")
        include("**/kotlin/**")
    }
}
subprojects {
    apply(plugin = "org.jlleitschuh.gradle.ktlint")
    ktlint {
        debug.set(true)
    }
}

当我运行gradlew ktlintCheck时,终端输出如下:

gradlew ktlintCheck

> Task :ktlintKotlinScriptCheck
[DEBUG] discovered ruleset with " standard" id.
[DEBUG] discovered reporter with "checkstyle" id.
[DEBUG] discovered reporter with "json" id.
[DEBUG] discovered reporter with "html" id.
[DEBUG] discovered reporter with "plain" id.
[DEBUG] Initializing "plain" reporter with {verbose=true,color=true,color_name=DARK_GRAY}
[DEBUG] Initializing "plain" reporter with {verbose=true,color_name=DARK_GRAY},output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.txt
[DEBUG] Initializing "checkstyle" reporter with {verbose=true,output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.xml
[DEBUG] Checking C:\Code\XXXX\build.gradle.kts
Resolving .editorconfig files for C:\Code\XXXX\build.gradle.kts file path
[DEBUG] 809ms / 1 file(s) / 0 error(s)

解决方法

首先,在build.gradle文件的配置中存在错误,在块Ktlint设置中,其显示为“ version.set(“ 9.4.0”)”的行是错误的,并且是不必要的。无论如何,如果您仍然决定使用此设置,则应为 version.set(“ 0.37.2”),因为它引用的是Ktlint版本而不是jlleitschuh / ktlint-gradle插件

要解决该问题,请对build.gradle.kts文件进行以下修改(我在Intellij IDEA社区版中将Gradle 6.6.1与Kotlin DSL配合使用)

请用以前的版本(9.3.0)替换插件

根项目中的

build.gradle.kts文件

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType // for Ktlint reports 

plugins {
    kotlin("jvm") version "1.4.10"  // Kotlin Compiler
    id("org.jetbrains.dokka") version "1.4.10" // Documentation Engine For Kotlin
    id("org.jlleitschuh.gradle.ktlint") version "9.3.0" // Kotlin Linter
}

group = "my group" // Replace with your group
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation("org.junit.jupiter:junit-jupiter:5.4.2")
    testImplementation(kotlin("test-junit5"))
    implementation(kotlin("stdlib-jdk8"))
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

tasks.test {
    useJUnitPlatform()
}

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
    jvmTarget = "1.8"
}

val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
    jvmTarget = "1.8"
}

ktlint {
    // THIS LINE IS NOT necessary and is incorrect -> version.set("9.4.0") 
    verbose.set(true)
    outputToConsole.set(true)
    coloredOutput.set(true)
    debug.set(false) // in your configuration this option must be set to true
    android.set(false)
    outputColorName.set("RED")
    ignoreFailures.set(false)
    enableExperimentalRules.set(false)
    reporters {
        reporter(ReporterType.CHECKSTYLE)
        reporter(ReporterType.JSON)
        reporter(ReporterType.HTML)
    }
    filter {
        exclude("**/style-violations.kt")
        exclude("**/generated/**")
        include("**/kotlin/**")
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...