Search code examples
gradlekotlingradle-kotlin-dsldetekt

Disable automatic Detekt run in Gradle


I have added Detekt to my Gradle project. My intention was to invoke detekt on demand only, since it creates a lot of false positives. However, the detekt task is active by default (and breaks the build). How can I avoid this dependency?

What I tried: I have added a gradle.taskGraph.beforeTask block that sets enabled = false conditionally:

gradle.taskGraph.beforeTask {
    val containsDetektTaskCall = gradle.startParameter.taskNames.contains("detekt")
    if (name.startsWith("detekt") && !containsDetektTaskCall) {
        logger.lifecycle("Skipping all 'detekt-plugin' tasks")
        enabled = false
    }
}

I have the feeling that these 7 lines of code are really a bit much just to override a task dependency. I would appreciate a general Gradle answer as well as some Detekt-specific way.


Solution

  • There are a lot of ways to skip the task. The easiest one is to add onlyIf condition for your task. For example:

    task detect {
        doFirst {
            println 'detect'
        }
    }
    
    detect.onlyIf { project.hasProperty('runDetect') }
    

    The detect task will be executed only when the onlyIf condition is true.

    ./gradlew detect -PrunDetect
    

    Please take a look here for details https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:skipping_tasks