jeremymailen / kotlinter-gradle

Painless, fast ktlint plugin for Gradle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please add a sample for how to apply kotlinter on a multi module Android project

nuhkoca opened this issue · comments

Hello,

Thanks for the up-to-date ktlint plugin. This is what I am looking for nowadays. I set up the plugin well but still didn't understand how to exclude some packages in a multi module project. It was enough with ktlint-gradle to exclude project-wide packages within the plugin block itself like below

ktlint {
    exclude = ...
}

but for this one, from what I understand from the doc, I need to configure the task(lintKotlinMain, formatKotlinMain) created by the plugin in each module or is there a better and centralized way of doing so? Thank you!

That is correct, a task exists for each SourceSet, so you need to customize them as shown here https://github.com/jeremymailen/kotlinter-gradle#customizing-tasks

As with all things gradle you can programmatically iterate through tasks of that type and apply the customization.

Thanks @jeremymailen. You mean like I can iterate through the respective LintTask inside allprojects block to configure all modules in one go, right?

allprojects {
    apply(plugin = "org.jmailen.kotlinter")

    repositories {
        google()
        mavenCentral()
    }

    kotlinter {
        ignoreFailures = false
        reporters = arrayOf("html")
        experimentalRules = true
        disabledRules = emptyArray()
    }

    tasks.withType<LintTask>().configureEach {
        exclude("**/generated/**", "**/build/**")
    }
}

That's what I was thinking.

Do note when you are excluding file paths above the package root, you need to use a slight different incantation to exclude the path. Two styles:

#242 (comment)

#242 (comment)

@jeremymailen I was struggling with finding the right syntax for excluding generated files.

Maybe one of the solutions in the comments that you mentioned could be added to the documentation?
I personally went with:

tasks.withType<LintTask>().configureEach {
    exclude { it.file.path.contains("/build/generated/") }
}

Thank you very much for this great Gradle plugin 👍🏼

I agree, we should document a simple example like that to help people so they don't need to go digging.
This would be a great little PR for someone to contribute :).

I spent a few hours today figuring out how to set up the pre push hook task to a multi module project. A sample project would've been mightily helpful.

I spent a few hours today figuring out how to set up the pre push hook task to a multi module project. A sample project would've been mightily helpful.

+1 on this. It doesn't make any sense to apply the plugin to the root project of a multi-module/Android build configuration but even when I tried, it still didn't work. In order to add this to the root project (as described in the README):

tasks.check {
    dependsOn("installKotlinterPrePushHook")
}

you'd need to apply the com.android.application plugin as well (because that's where the check task comes from) and that triggers a whole other bunch of nightmares