Print Gradle Module Tree

Sep 5, 2020

A simple way to create an indented representation of the module structure tree of a gradle project is by creating a custom task for it. Just add these lines to your root build.gradle:

task printModuleTree {
    doLast {
        def printProject
        printProject = { project, indent = "" ->
            println(indent + project.name)

            project.childProjects.values().each { childProject ->
                printProject(childProject, indent + "  ")
            }
        }

        printProject(rootProject)
    }
}

By running ./gradlew printModuleTree the module structure is printed to stdout and will look similar to this:

example
  module1
  module2
    submoduleA
    submoduleB
  module3

That’s it, not even 15 lines of code!

You might also like