sourceSets: how can I include -> exclude -> and include again in sourceSets in that order

I need to filter some classes based on package name: /tests/, but I have also a buisness function that contains ‘.test.’ in its package name. With Maven, I put an explicit inclusion AFTER the exclusion, and that put the required ‘.test.’ package in my sourceSet or equivalent.

In my gradle.build I do the following but with not the Maven behaviour:

sourceSets {

main {

java {

srcDir “${project.genRoot}/src”

include “com/toto/**/*.java”

exclude “**/tests/*”

include “com/toto/core/tests/extensionjunittest/*.java”

}

}

All the content of ‘com/toto/core/tests/extensionjunittest/*.java’ is filtered and not present in the compilation result.

How can I make the same function in Gradle? thanks all!

Excludes win over includes. The order in which you declare them doesn’t matter.

The first include seems to be using ‘//’ instead of ‘/*’. The exclude uses an absolute path, which will probably exclude nothing. To achieve your goal, you may have to use programmatic includes/excludes. E.g.:

exclude { FileTreeElement elem -> /* code that inspects elem.path (a String) and returns true or false */ }

PS: Please use HTML code tags for all code snippets.

1 Like

thanks all, and sorry for the HTML missing.

For the newbies, I put the solution:

exclude { FileTreeElement elem -> ((elem.path.contains('/tests/') || elem.path.contains('/Tests/') || elem.path.contains('/testsimpl/')) && !elem.path.contains('com/toto/core/tests/'))}

I hope that will helps!

2 Likes