Create sourceSet which overrides from another sourceSet

Is there a way to create a sourceSet from another that overrides some files?


Given a source tree above, the core logic is in the src/main/java dir. In some cases we need to build the core but replace files from the src/legacy/java folder. Is there a way to create a new sourceSet which is src/main/java but with all the files of src/legacy/java overriding?

ie getting a sourceSet like:

|--com/company/ (from src/legacy/java)

We can’t model this with source sets at the moment. You could include all the files from one in the other but as far as the source set is concerned they would be unique files. You have to deal with this when you create the final output, which in this case is most likely a JAR. You could define a separate JAR task like so:

task legacyJar(type: Jar) {
    classifier = 'legacy'

    from sourceSets.legacy.output
    from sourceSets.main.output
    duplicatesStrategy = 'exclude'

Great! This will work out as well… thanks!