Identifying the list of projects that have been invoked in gradle

988 Views Asked by At

I have a settings plugin that is multi-project aware. It currently applies itself to every project identified in settings.gradle. However, this is a problem because each subproject doesn't have the same configuration, and I control some plugin behavior through project properties set on the command-line via -P.

The problem is that this fails on projects that don't have the configuration necessary to use that property, and I know of know way to apply a property to a specific subproject via the command-line.

Instead of iterating over settings.gradle.allprojects, is there a way to know what projects have actually been included as part of the build? For example when I do:

gradle :subproject-name:build :other-subproject-name:build -PsomeProperty

I would like to know that only subproject-name and other-subproject-name were called so that I can apply the settings plugin to only those projects.

Or is there a way to "scope" project properties somehow, to only a particular project?

1

There are 1 best solutions below

6
nickb On

... is there a way to know what projects have actually been included as part of the build?

This is a misconception. All projects are part of the build when settings.gradle includes them, and they'll all get configured so they have an associated Project.

What you're ultimately looking for is, given the tasks that will execute, what are the subprojects who own those tasks? To do that, you can grab Gradle's task graph, and for all of the tasks that will execute, find the project that owns each task.

gradle.taskGraph.whenReady { graph ->
    def projects = graph.allTasks.collect { it.project }.toSet()
    projects.each {
        println "Project is being used in this build: " + it
    }
}