Gradle Exclude File From Sourceset Not Working
Solution 1:
I finally figured out the way to include a single file, without modifying the srcDir
path:
main.java {
srcDir "../../java/src"
setIncludes(new HashSet(['com/company/product/pkg1/libraryoneonly/*.java']))
}
The path '**/*.java'
is automatically included as an include, adding any more includes, like a direct include to the source file does nothing, since it is already included path anyway.
The setIncludes()
call replaces any existing includes, including the '**/*.java'
and sets the provided path[s] as the only include path.
Reading over the docs and adding the suggested printf statements after the main.java helped.
main.java.getIncludes().each { println"Added include: $it" }
main.java.sourceFiles.each { println"File in source set: " + it }
Thanks to all who have helped out.
Solution 2:
Gradle'e SourceDirectorySet seems to be missing features. docs has a very telling note:
TODO - configure includes/excludes for individual source dirs, and sync up with CopySpec TODO - allow add FileTree
The lack of features on par with CopySpec means there isn't a logical way to do what you're asking. The include
pattern first adds a list of files to the sourceSet and then you can exclude
a few files from that set. Except,
- The
main.java
sourceset implicitly adds**\*.java
- There is no exclude everything that is not
means you are SOL.
You can declare a separate main.somethingElse
source set and use include just that one file, but I wasn't able to figure out how to add the new sourceset to the compile path. The hack suggested here, using compiler args doesn't work anymore, java complains about invalid flags.
The only workaround I can think of is to use a path that excludes that files you do not want in main.java.srcDir
for library1
main.java {
srcDir '../../java/src/com/company/product/pkg1/libraryoneonly'
}
Solution 3:
Your code seems to be correct to me, it's just that FileCollections like sourceFiles are lazily evaluated, i.e. your println
statement is not executed until the contents of the source set are actually being requested.
Adding something like
main.java.sourceFiles.each { println"File in source set: " + it }
after the main.java {...}
closure should actually make the println
work.
Post a Comment for "Gradle Exclude File From Sourceset Not Working"