Gradle java plugin - filtering with multiple source directories

(Ahsan Rabbani) #1

I have a multiproject setup with a common build.gradle file in the root directory which has the following srcDir for main java resources: sourceSets.main.resources.srcDir “configurations/common”

In my subproject build.gradle I want to keep the above srcDir intact and add another srcDir but only include certain files from it. So I added: sourceSets {

main {

resources {

srcDir “src”

include “*.xml”


} }

This however overwrites the srcDir value from the root build.gradle and I only get files from “src”.

If I do the following in the subproject build.gradle, the root srcDir is kept intact but the subproject’s srcDir is not filtered in anyway. sourceSets.main.resources.srcDir “src”

There must be a way to do this? Also, ideally I would like to accomplish this without having to respecify the srcDir from the root build.gradle.

Any help would be appreciated. Thanks

(Peter Niederwieser) #2

Works just fine for me. Chances are there’s a problem somewhere in your build script(s). Does the common ‘build.gradle’ configure the resources dir for ‘subprojects {}’?

(Ahsan Rabbani) #3

Thanks for the reply. To answer your question, the common build.gradle does configure the resources directory for subprojects. However, I played around with it some more and realized that the srcDir is not actually getting overwritten as I previously suspected. The problem is that the include in the subproject’s build.gradle is being applied to the srcDir in the root build.gradle. I verified this by putting an xml file in the configurations/common directory and sure enough, the dummy xml file was in the build/resources/main directory. So is there anyway to make the include only apply to the subproject’s srcDir?

(Peter Niederwieser) #4

That’s expected, because includes/excludes apply to all source dirs in the source directory set (e.g. sourceSets.main.resources).

(Ahsan Rabbani) #5

Ok, that makes sense. I must have overlooked that fact if it was in the docs. Thanks for clearing it up.