Custom Configurable SourceSets

Hello, I’m looking for way to make my SourceSets dynamically configurable, here is the situation: Imagine that I create in “gradle.properties” some Lists where I define my sources:

gradle.properties:

SimpleVersion=[source1,source2...sourceN]
UxVersion=[source1_UX,source2_UX...sourceN_UX]

Now suppose that I use a flag to select which version whether -Pflag=NormalVersion or -Pflag=UxVersion during the build call. Then as we know we going to apply “java plugin” and define the sourceSets:

sourceSets{
  Myproject{
    java.srcDirs=[
     ....
    ]
    output.classesDir=....
    compileClasspath=....
    runtimeClasspath= ...
    }
  unitTest{
   .....
  }
}

You might guess what is my question now: How to affect SourceSets.java.srcDirs to specific List of sources defined in gradle.properties and of course this depends on flag value? Thank you!!

I have almost found a solution: gradle.properties:

flag=UX
ClassicVersion=['backend','shared','dashboard_web','gwt_common','surveys-backend','widgety']
UXVersion=['backend','shared','dashboard_web_ux','gwt_common_ux','surveys-backend','widgety_ux']

build.gradle:

sourceSets{
  myproject{
    if(!flag.compareTo('UX')){
      java.srcDirs=UXVersion
      }
    else{
      java.srcDirs=ClassicVersion
              }
    output.classesDir=...
    compileClasspath=....
    runtimeClasspath= ...
    }
  unitTest{
  .....
}
}

Well I haven’t found how to cast my Lists defined in gradle.properties to java.srcDirs:

* What went wrong:
A problem occurred evaluating root project 'Myproject.Gradle'.
> Cannot cast object '['backend','shared','dashboard_web_ux','gwt_common_ux','surveys-backend','widgety_ux']' with class 'java.lang.String' to class 'java.lang.Iterable'

any ideas please? Thank you!!

Property values you defined in gradle.properties are not Lists but Strings. You will need to parse them.

Thank you Marcin, Would you please provide some details? because I have never done a simple file parsing using Gradle. Thanks again!!

I think that would be easy to use string instead of Parser, Well here’s the solution: gradle.properties

flag='UX '
UXVersion=backend,shared,dashboard_web_ux,gwt_common_ux,surveys-backend,widgety_ux
ClassicVersion=backend,shared,dashboard_web,gwt_common,surveys-backend,widgety

build.gradle

sourceSets{
 Myproject{
    switch (flag.toString()){
      case 'UX':java.srcDirs=files(UXVersion.split(','))
        break
        default:
        java.srcDirs=java.srcDirs=files(ClassicVersion.split(','))
         break
    }
    }
  unitTest{
....
  }
}

Finally this works fine. But it’s doesn’t means that I still not interested in another solutions. Thank you very much!!