ktlint不检查Kotlin文件

i5desfxk  于 2023-04-21  发布在  Kotlin
关注(0)|答案(2)|浏览(178)

我希望我的项目对所有Kotlin文件执行ktlintCheck,但它只检查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=true, color_name=DARK_GRAY}, output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.txt
[DEBUG] Initializing "checkstyle" reporter with {verbose=true, color=true, color_name=DARK_GRAY}, 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)
whlutmcx

whlutmcx1#

开始,在您的build.gradle文件的配置中有一个错误,在块Ktlint设置中,出现“version.set”的行(“9.4.0“)”是不正确的,也是不必要的。无论如何,如果您仍然决定使用此设置,则应该使用例如version.set(“0.37.2”),因为它指的是Ktlint版本,而不是jlleitschuh / ktlint-gradle插件
为了解决这个问题,对build.gradle.kts文件进行以下修改(我在Intellij IDEA社区版中使用带有KotlinDSL的Gradle 6.6.1)

请将插件替换为之前的版本(9.3.0)

在根项目中构建.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/**")
    }
}
3b6akqbq

3b6akqbq2#

我遇到了类似的问题,我找到了一个适合我的解决方案。您可以尝试在模块中将以下配置添加到您的build.gradle文件中:

android {
// Your existing android configuration

sourceSets {
    getByName("main") {
        java.srcDir("src/main/kotlin")
    }
}

通过在android块中添加此配置,您可以告诉Gradle将src/main/Kotlin目录也视为Java源目录。这样,ktlint就可以正确分析该目录中的Kotlin文件。
请确保在添加此配置后同步项目。

相关问题