I want to wrote custom grade taks that “will be ru...
# gradle
i
I want to wrote custom grade taks that “will be running” all of the static analysis checks at once - in my case it is
detect
,
ktlint
,
android lint
. These translates to below grade tasks:
Copy code
./gradlew detektCheck
./gradlew ktlintCheck
./gradlew lint (this task does not exist gradle will select task from app module :app:lint)
As far I I know grade task can’t run another grade task and it can jus depend on another task (with makes perfect sense). Now I was able to write simple task (Kotlin Grade script) that runs
detekt
and
ktlint
checks (by using dependency mechanizm)
Copy code
task("staticCheck") {
    dependsOn("detekt", "ktlintCheck")
}
Now I am struggling with adding the (Android)
lint
. I can’t simply add
lint
to dependencies (
dependsOn("detekt", "ktlintCheck", "lint")
) because
lint
task does not exists in configuration phase:
Copy code
task("staticCheck") {
    val lintDependencies = subprojects.mapNotNull { it.tasks.findByName("lint") } //empty list

    doLast {
        val lintDependencies = subprojects.mapNotNull { it.tasks.findByName("lint") } //list containing few modules, however I can't use dependsOn here
    }
}
What is the best way of running all static analysis checks in single task?
c
i
Thanks to your advice I was able to achieve what I wanted
Copy code
task("staticCheck") {
    afterEvaluate {
        val lintDependencies = subprojects.mapNotNull { "${it.name}:lint" }
        dependsOn(lintDependencies + listOf("ktlintCheck", "detekt"))
    }
}
The only issue now is that I am assuming that
lint
task is available in all modules (witch is not true for pure java modules) . I tried to retrieve list of lint tasks or plugin for module (
subprojects.filter {  it.plugins.hasPlugin(GradlePluginId.androidApplication) }
) but in botch cases these variables are empty - like module would not be evaluated
👍 1