Gradle: can't targetExclude files in the buildDir
See original GitHub issueI have a project where some Java source files are generated by a task; they’re generated into build/generated-sources/
and added to a source set to later be compiled (like any other source files). I don’t want those generated source files to be checked, but setting targetExclude("build/generated-sources/**/*.java")
doesn’t work.
This is using Gradle 5.4.1 and Spotless 3.23.0 on Linux.
The way java()
works, target
is set to include allJava
sources for all source sets, irrespective of their location (contrary to setting the target to an Ant-style pattern, which will exclude files in the buildDir
).
The problem is that targetExclude()
works the same as target()
and will also exclude the buildDir
, so targetExclude("build/generated-sources/**/*.java")
will result in an empty FileTree
, and my generated sources will be checked by Spotless.
I found a workaround by using a FileTree
, which won’t go through that code that excludes the buildDir
:
targetExclude(fileTree("$buildDir/generated-sources") { include("**/*.java") })
But the special exclusions should only be applied to the target
, not targetExclude
.
To reproduce, no need for a task generating source code: put some malformed Java source file somewhere in the buildDir
(e.g. $buildDir/generated-sources/test/Foo.java
) and include it in a source set:
sourceSets {
test {
java {
srcDir("$buildDir/generated-sources/test")
}
}
}
spotless {
java {
targetExclude("build/generated-sources/**/*.java")
googleJavaFormat()
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top GitHub Comments
I stepped through the build in a debugger and found that the problem lies in SpotlessTaskImpl.getOutputFile(). The call to FormatExtension.relativize returns null, which causes the exception to be thrown. relativize returns null because the targetExclude is not a child of the subproject directory in a multi-module build. So targetExclude can’t be used to exclude generated sources from a multi-module build, because the build output is at the root directory, not the subproject directory.
I tried to use the OP’s original non-working solution, thinking that #457 would make it work, but no luck.
targetExclude("build/*generated-sources/**/*.java")
results in 'Execution failed for task ‘:helium-data-model:spotlessJava’.I’m using gradle spotlees plugin 6.4.2
This is a multi-module gradle build, where spotless is configured in the root build file.