Gradle (starting from 3.4) cleans build/classes/test and build/resources/test

Steps to reproduce:

  1. Build project
  2. Delete .gradle and src dirs
  3. Run test task

Expected result:
Tests are executed as they’re compiled in build dir.

Actual result:
Gradle cleans build/classes/test and build/resources/test and skips tests with NO-SOURCE status.

Worked fine with 3.2.1 and earlier.

This reads a lot like a bug report. But, from my point of view, it could also be interpreted as “Thanks for fixing this!”

Obviously, if you delete all of the sources on which a build is based, then you are essentially asking that the tests not be run. So, Gradle is doing the right thing to tell you that the tests are skipped with NO-SOURCE status.

Why were you counting on the previous behavior? What problem are you trying to solve?

Thanks for response but I think I did not clearly explain all the details.
First of all the problem I’m solving is migration to newer version of gradle.

  1. In step 1 I’ve built the project, so all compiled files are in build dir.

  2. I do not delete build dir, only .gradle dir and src. I do not need src anymore as I’m not going to build the project once more.

  3. I execute test task which does not depend on clean and build tasks, so based on what logic do I get my classes removed before any task execution? (I assume it’s because of gradle caching or incremental build features as I delete .gradle dir, but here we’re talking about API not internal behavior like caching policies and in my opinion cleaning of build directory is a part of API)

    All projects evaluated.
    Cleaned up directory '…/build/classes/test’
    Cleaned up directory '…/build/resources/test’
    Selected primary task ‘test’ from project :

Also please note if I do not delete .gradle dir then tests again work perfectly fine without src dir. So the issue is not in no src -> NO-SOURCES and it’s not a fix of wrong previous behavior (might be but then it’s incomplete fix and should also be reported), but it looks like some internal change in gradle’s file management which causes removal of classes and resources.

There are two things going on.

  1. Removing .gradle or upgrading Gradle deletes class directories since 3.4. This is to prevent scenarios where source files are removed and a Gradle upgrade happens at the same time. This would orphan .class files that would only be removed when running clean and made it hard to reliably cache the output of Java compilation.
  2. When all sources are removed from a task (like JavaCompile), we do not clean-up the orphaned outputs of the task. This is going to be fixed in 4.2.

It’s a bug/incomplete fix that you can delete all of your source files and still run tests in an older version of Gradle.

1 Like