Inquiry. I have a multi-module project. (A half ...
# getting-started
l
Inquiry. I have a multi-module project. (A half dozen or so modules, siblings of each other, that all import a large common library.) I want to run Detekt across all of them at once. Single configuration file, single baseline file. Is there a way to do that? Possibly with a custom task in the the top level (outside of any module)
build.gradle.kts
file? (Which, yes, we have.)
j
Not sure about your specific use case, but you don't need a task in the top level project in general. If you run
./gradlew sometask
it will run that task in all projects that have it defined
r
Yeah, we do something pretty similar (but with over 100 modules). We have this in top level gradle (not kotlin but kotlin colored but you can convert):
Copy code
allprojects {
    afterEvaluate { project ->
            project.apply plugin: "io.gitlab.arturbosch.detekt"
            project.detekt {
                config = files("$rootDir/detekt-config.yml")
                //any other common config
            }
}
And as @Joffrey says, you can just run
gradlew detekt
and it'll run for all modules
l
Hm. I was able to make it work like this in my top level file:
Copy code
detekt {
    config.setFrom("detekt.yml")
    baseline = file("detekt-baseline.xml")
    buildUponDefaultConfig = true
    source.setFrom(
        "sub1/src/",
        "sub2/src/",
        "sub3/src/",
    )
}
But then our principle engineer changed it to:
Copy code
subprojects {
    tasks.withType<Detekt> {
        config.setFrom("../detekt.yml")
        baseline = file("../detekt-baseline.xml")
        buildUponDefaultConfig = true
    }
}
I don’t quite understand what the latter is…
r
The first is configuring detekt only on the top level project but adding all the submodule sources manually (wouldn't recommend this because you'll need to remember to update that block whenever you add more modules)
Second way is pretty similar to what I posted: configuring detekt on each project from the top level
l
But does running
detekt
from the top level then run it across all projects automatically, or does it need to get run separately for each project?
r
See @Joffrey's answer: it'll run on all projects that define the task (which is added by the detekt plugin)
l
Er. So it does a forEach loop over all the modules, basically?
moduleList.forEach { detekt }
or something like that?