When using the Gradle daemon and the excludeCategories or includeCategories JUnitOptions, dependent jars are locked by the daemon process and you cannot delete them.
I have provided an example project at https://github.com/redcape/gradle-example/tree/junit-error
Please make sure you’re looking at the junit-error branch if you download this project. Note that there is no code in the ‘projectdep’ dependency project, yet its jar still gets locked. Additionally, tests are not even running. It is important that Gradle tries to run a test though meaning that there is source code in the directory or else the condition will not occur.
Run: ./gradlew.bat --daemon clean build
The project will successfully clean and build.
Run it again and it will fail with:
$ ./gradlew.bat --daemon clean check
:projecta:clean
:projectdep:clean FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':projectdep:clean'.
> Unable to delete file: T:\code\show-errors\gradle-example\projectdep\build\libs\projectdep.jar
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Running ./gradlew.bat --stop followed by ./gradlew.bat clean build or ./gradlew.bat --daemon clean build will run fine.
If the daemon is not running, no clean errors will occur.
Commenting out either the project dependency line or the JUnit excludeCategories line and restarting the daemon(if it’s already holding a reference) will make the problem go away.
I tested this against Gradle 2.4 on IBM Java 6 (32-bit) and Oracle Java 8(64-bit) on Windows 7 32-bit and 64-bit respectively. They both have the same result.
I also tested this on the gradle master branch built from source on Oracle Java 8 and had the same issue.