Best way to identify subproject 'roots' in plugin applied to hierarchical multi-module build?

Nearing the end of this big ant+maven migration and need a little guidance on one last part.

This build has around 100 subprojects in a structure somewhat like this:

Top/
    -- build.gradle
    -- Base/
        -- build.gradle
        -- Base A/
              --build.gradle
              --src/
        -- Base B/
              -- build.gradle
              --src/
        -- Base <...>/
              -- build.gradle
              -- src
    -- Modules/
        -- build.gradle
        -- Module 1/
            -- build.gradle
            -- SubA/
                - build.gradle
                - src/
            -- SubB/
                - build.gradle
                - src/
            -- SubC/
                - build.gradle
                - src/
        -- Module 2/
            -- build.gradle
            -- SubE/
                - build.gradle
                - src/
            -- SubF/
                - build.gradle
                - src/
        -- Module <...>

Module deliverables are created via a custom plugin. I want to be able to build any Module on its own, or trigger the building of all submodules from the Modules root. Currently, calling gradle buildModule from an individual Module root works exactly how I want. The problem I run into is when I attempt to build all the modules by calling subprojects.buildModule. Currently the plugin uses task rules that incorporate logic in places that is essentially if (project.name == project.rootProject.name) { <do something> }. Of course, when running from the Modules/ dir, the subproject ‘root’ is no longer the root, so tasks aren’t created/run for the right projects.

Question:

What is the best built-in way to determine if an intermediate project is a ‘root’ during task execution/application of the plugin? I could easily add a list of ‘module roots’, or add a plugin extension, but I would really like to avoid both of those for cleanliness and long term maintenance.

Still pretty new to Gradle, so I could be ‘doing it all wrong’ and appreciate any feedback.

edit: In the interest of keeping our build process moving forward, I have ended up adding a “moduleRoot” value to a plugin extension that is applied to allprojects of each Module root. Then when building configuration rules, we check if project.name == extension.moduleRoot and create/apply our subproject task dependencies from there. It is functional, but if there is some built-in way of identifying internal ‘roots’ that I am overlooking, I’d like to know about it. I didn’t see anything obvious when looking through the source/docs.

1 Like