项目中未找到任务“ktlintCheck”

问题描述 投票:0回答:1

我有一个示例项目。 build.gradle(项目)是:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id "org.jlleitschuh.gradle.ktlint" version '10.3.0' apply true
}

subprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent

    // Optionally configure plugin
    ktlint {
        debug = true
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(应用程序)是:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id "org.jlleitschuh.gradle.ktlint"
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.codestyleexpetiment"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

ktlint {
    version = "0.45.2"
}
dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

我已经成功安装了预提交钩子。

即使使用 app:ktlintCheck,下面的 4 次尝试也以相同的方式工作。

  • 我可以从 Android studio 中的 Gradle 窗口运行 ktlintCheck。

  • 我可以从命令行 gradlew ktlintCheck 运行任务

  • 如果我在预提交脚本中用signingReport(或来自android插件的任何其他任务)替换ktlintCheck,我也可以运行它。

  • 但是,我无法从预提交脚本运行 ktlintCheck 和 ktlintFormat ,并且收到此错误:

    提交失败并出现错误 0 个文件已提交,1 个文件提交失败:重新格式化 1 对这些文件运行 ktlint: 应用程序/src/main/java/com/example/codestyleexpeiment/MainActivity.kt

             FAILURE: Build failed with an exception.
    
             * What went wrong:
             Task 'ktlintCheck' not found in project ':app'.
    
             * Try:
             Run gradlew tasks to get a list of available tasks. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
             * Exception is:
             org.gradle.execution.TaskSelectionException: Task 'ktlintCheck' not found in project ':app'.
             at org.gradle.execution.DefaultTaskSelector.getSelection(DefaultTaskSelector.java:113)
             at org.gradle.execution.DefaultTaskSelector.getSelection(DefaultTaskSelector.java:78)
             at org.gradle.execution.CompositeAwareTaskSelector.getSelection(CompositeAwareTaskSelector.java:93)
             at org.gradle.execution.commandline.CommandLineTaskParser.parseTasks(CommandLineTaskParser.java:43)
             at org.gradle.execution.TaskNameResolvingBuildConfigurationAction.configure(TaskNameResolvingB... (show balloon)
    

预提交脚本是:

#!/bin/sh
######## KTLINT-GRADLE HOOK START ########

CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $NF ~ /\.kts?$/ { print $NF }')"

if [ -z "$CHANGED_FILES" ]; then
    echo "No Kotlin staged files."
    exit 0
fi;

echo "Running ktlint over these files:"
echo "$CHANGED_FILES"

diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > $diff
if [ -s $diff ]; then
  git apply -R $diff
fi

./gradlew --stacktrace ktlintCheck -PinternalKtlintGitFilter="$CHANGED_FILES"
gradle_command_exit_code=$?

echo "Completed ktlint run."


if [ -s $diff ]; then
  git apply --ignore-whitespace $diff
fi
rm $diff
unset diff

echo "Completed ktlint hook."
exit $gradle_command_exit_code
######## KTLINT-GRADLE HOOK END ########

我使用的库/插件是this。我错过/忘记了什么吗?

android gradle githooks ktlint
1个回答
0
投票

这也发生在我身上。原因是,在预提交脚本中有这一行:

git apply -R $diff

此行删除所有非暂存文件。就我而言,它还恢复了

build.gradle
文件,其中应用了 ktlint 插件(因此注册了任务
ktlintCheck
)。

简单的解决方案就是在调用预提交挂钩之前暂存所有更改(或至少是

build.gradle
):

git add .

或者先提交

build.gradle
文件。

© www.soinside.com 2019 - 2024. All rights reserved.