ktlint未检查kotlin文件

ktlint未检查kotlin文件,kotlin,gradle-kotlin-dsl,ktlint,Kotlin,Gradle Kotlin Dsl,Ktlint,我希望我的项目对所有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(

我希望我的项目对所有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=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)


首先,build.gradle文件的配置中存在错误,在block Ktlint设置中,显示“version.set(“9.4.0”)”的行不正确且不必要。在任何情况下,如果您仍然决定使用此设置,它应该是,例如,version.set(“0.37.2”),因为它指的是Ktlint版本,而不是JLLeitschhuh/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/**")
    }
}
import org.jetbrains.kotlin.gradle.tasks.kotlincomfile
导入org.jlleitschhuh.gradle.ktlint.reporter.reporter type//for ktlint报告
插件{
kotlin(“jvm”)版本“1.4.10”//kotlin编译器
id(“org.jetbrains.dokka”)版本“1.4.10”//Kotlin文档引擎
id(“org.jlleitschhuh.gradle.ktlint”)版本“9.3.0”//Kotlin Linter
}
group=“我的组”//替换为您的组
version=“1.0-SNAPSHOT”
存储库{
mavenCentral()
jcenter()
}
依赖关系{
实现(“org.junit.jupiter:junit-jupiter:5.4.2”)
测试实施(kotlin(“test-junit5”))
实施(kotlin(“stdlib-jdk8”))
}
tasks.withType{
kotlinOptions.jvmTarget=“1.8”
}
任务.测试{
useJUnitPlatform()
}
val compileKotlin:kotluncompileby tasks
compileKotlin.kotlinOptions{
jvmTarget=“1.8”
}
val compileTestKotlin:KotlUncompile by tasks
compileTestKotlin.kotlinoctions{
jvmTarget=“1.8”
}
克林特{
//该行不是必需的,并且不正确->version.set(“9.4.0”)
verbose.set(true)
outputToConsole.set(真)
coloredOutput.set(真)
debug.set(false)//在配置中,此选项必须设置为true
android.set(false)
outputColorName.set(“红色”)
ignoreFailures.set(false)
enableExperimentalRules.set(false)
记者{
reporter(ReporterType.CHECKSTYLE)
reporter(ReporterType.JSON)
reporter(ReporterType.HTML)
}
滤器{
排除(“***/style inflictions.kt”)
排除(“**/generated/**”)
包括(“**/kotlin/**”)
}
}

您的项目结构是什么?项目结构如下:\project\src\main\kotlin\com\abc\tara\api\UserController.kt