'Gradle's compileJava fails to write .class files if cache is enabled and the resources output dir is the same as the classes output dir

Applies to gradle 7.0 and earlier.

Every second clean/build cycle, the .class files are not written to $buildDir/classes.

The problem appears only when:

  • The project has a very few number of java files (1-2 files) and some resource files
  • sourceSets.main.java.outputDir == sourcesSets.main.java.output.resourceDir
  • cache is enabled for the compileJava task

The behavior is this:

  1. clean -> build: works, .class files are written in $buildDir/classes
  2. Repeat step 1: fails, .class files are NOT written
  3. Repeat 1: works ...
  4. etc...

Given these conditions, the problem does not occur if either of the following is true:

  1. Disabling cache globally or for the compileJava task
  2. The resources output directory is different than the classesDir
  3. The are no resources
  4. The project being build is larger (some timing issue obviously).

The problem happens randomly to other small sub-projects at different machines and at random times. I have searched for days and have not found any similar issue mentioned elsewhere so any hint will be very helpful.

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source