Best approach for building a specific subset of subprojects?

Suppose I have a project hierarchy like so:

ice cream
| - chocolate
| - vanilla
| - strawberry
\ etc..

Sometimes I may want to only build a server with vanilla ice cream, sometimes with chocolate and strawberry, and sometimes with all 31 flavors. How can I do this with gradle? I am aware that you can pass in properties through a gradle.properties file, and there may be a way to set up some blocks for some logic to build by applying properties from certain .gradle files, but would there be a simple way of simply running something along the lines of:

gradle build all

or

gradle build chocolate vanilla

?

The reason why these modules are structured in this project is because they all typically have the same dependencies.

  1. Create specific tasks that only depends on the flavors you need (.e…g buildChoclate, buildNeapolitan) 2) Create a generic task that will dynamically configure the project dependencies given your input from command line, file, etc…

Thanks for your reply.

Option 2 sounds best because I plan on passing in a parameter/parameters from Jenkins when doing a production build. How would you do this?

I am aware you can pass a project property in with

gradle build -Pflavor=chocolate

but can you pass in more than one property?

If this is not possible, then perhaps I may have to go with specifying flavour groups in certain .gradle files, eg. have a “neapolitan.gradle” file, which when the flavor property is neopolitan, the task uses

apply from: neapolitan.gradle

builds and bundles three different projects. Problem is that this method requires that someone has commited a gradle file for each configuration.

EDIT: Seems like a possible solution I have found might be to split the arguments by a whitespace token.

gradle build -Pflavours="chocolate vanilla"
buildFlavors {
    def flavours = []
    if(project.hasProperty('flavours')){
        flavoursArgs = project.flavours.split('\s')
        flavoursArgs.each {
     println "Including flavour $it"
     flavours.add(project(':' + it))
         }
    }
   flavours.each {
       dependsOn(it.tasks.withType(JavaCompile))
   }
  }

You could also use a TaskRule: https://gradle.org/docs/current/userguide/more_about_tasks.html#N10FE1

e.g., if someone does ‘gradle buildChocolate’ you could intercept that and generate the correct aggregating task.

If the names are unique enough, you could also just intercept that and have a rule that ‘gradle chocolate’ builds the chocolate project.