question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gradle: can't targetExclude files in the buildDir

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
niels1voocommented, Apr 6, 2022

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.

0reactions
niels1voocommented, Apr 6, 2022

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’.

Spotless error! All target files must be within the project dir. project dir: /Users/niels/repo/sx/helium/helium-main/helium-data-model target: /Users/niels/repo/sx/helium/helium-main/build/helium-data-model/generated/sources/xjc/java/com/soundexchange/helium/distribution/iso20022/NameAndAddress3.java’

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change Gradle's working directory when compiling a Groovy ...
Yes, there is. You can put the following line in your build script: buildDir = 'your_directory'. Or in gradle.properties file (no quotes around ......
Read more >
diffplug/spotless - Gitter
Hey, @nedtwigg I want to invite you to join the Gradle Slack channel, ... follow the steps there to "include" Spotless into their...
Read more >
build.gradle · Hyperledger/besu - Gitee.com
An enterprise-grade Java-based, Apache 2.0 licensed Ethereum client https://wiki.hyperledger.org/display/besu.
Read more >
The Distribution Plugin - Gradle User Manual
The files will be created at $buildDir/distributions/${project.name}-${project.version}.«ext» . You can run gradle installDist to assemble the uncompressed ...
Read more >
FormatExtension (plugin-gradle 5.6.1 API) - Javadoc.io
The files to be formatted = (target - targetExclude). ... then gradle can never mark your files as up-to-date , because it can't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found