How to run a task for a subset of multiproject-build

I am (ab)using Gradle to build a report tool and also to run the report in multiple configurations. As the inputs for each report configuration can be complex and can vary frequently I’ve modeled each report as a subproject with custom plugin for a structure that looks something like this:

/buildSrc # custom logic to hook the reports and generate IDE ssettings
/src      # the code for the actual report tool
/reportA  # aggregating project for reports of type A
   / xxx  # a project that runs report A against XXX target
   / yyy  # a project that runs report A against YYY target
/reportB  # aggregating project for reports of type B
   / yyy  # a project that runs report A against YYY target
   / zzz  # a project that runs report A against XXX target

To run all my reports and publish them to a permanent storagw (raw Nexus repo in fact), I call Gradle as gradlew report pub. Works great!

Now for the question - sometimes I would like to get only the reportA, and sometimes I would like to only run all reports for yyy.

Running gradlew :reportA:report understandably complains there is no such task. For the second requirement I can’t even think how to express it on the command line.

Ideally I would like something like a wildcard that can match multiple levels ** or a single level *. If it existed, my scenarios would have been addressed by something like gradlew :reportA:**:report and gradlew :**:yyy:report.

Has anybody had this problem before? Is there an existing feature I’m missing?

I think you can use a task rule

Eg:

tasks.addRule('Pattern: report<ID>') { taskName ->
   if (taskName.startsWith('report')) {
      String pathToken = ":${taskName.substring(6).toLowerCase()}:" 
      def matchingTasks = getTasksByName("report", true)
         .findAll { it.path.toLowerCase().contains(pathToken) } 
      if (matchingTasks) {
         task(taskName) {
            dependsOn matchingTasks
         }        
      } 
   } 
}      

Then you could run

gradle reportXxx
gradle reportYyy
gradle reportReportA
1 Like