I've made my Gradle `sourceDirs` task more rubust....
# kobalt
h
I've made my Gradle
sourceDirs
task more rubust. one problem was, that
project.sourceSets
is only available for projects that have applied the
java
or
kotlin
plugin. I have a
rootProject
that only applied the
base
plugin. also confusing is, that the
task
closure contains a delegated
sourceSets
variable (IDEA knows about this variable; I think it is hardcoded into the IDEA Gradle plugin). This delegated
sourceSets
variable is equivalent to
project.sourceSets
.
Copy code
allprojects { project ->
    // task to list main and test source directories.
    task("sourceDirs", group: "help").doLast {
        // ignore source directories for projects without source sets.
        if (!project.hasProperty("sourceSets")) { return }

        //def sourceSets = project.sourceSets as SourceSetContainer
        def sourceDirs = sourceSets*.allSource.srcDirs.flatten() as List<File>
        sourceDirs.each { println(it) }
    }
}