Search code examples
javagradle

gradle clean fails with "GradleException: Failed to create Jar file"


I'm seeing a very strange problem with gradle.

When I run any command (see clean below), gradle fails with a concurrent exception.

This is happening since my latest changes. Rolling back to a previous commit works fine.

The changes are too numerous to describe here but in summary I've restructured the build a bit, based on another project which is working. And when I say based on, I mean I have simply copied build.gradle, settings.gradle and gradle.properties from that other project. None of the changes relate to jackson core.

$ ./gradlew clean

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'myservice'.
> java.util.concurrent.ExecutionException: org.gradle.api.GradleException: Failed to create Jar file C:\Users\me\.gradle\caches\jars-9\b868ab677410c01cf9b5a0e29a035e73\jackson-core-2.15.2.jar.
    
BUILD FAILED in 2s

I tried removing the lock file but that failed:

$ rm -rf ~/.gradle/caches/jars-9/*lock
rm: cannot remove '/c/Users/me/.gradle/caches/jars-9/jars-9.lock': Device or resource busy

So I ran ./gradlew --stop which stopped 2 daemons, and I was able to remove the lock file. But the clean still fails with the same error.

I've tried cleaning the .gradle directory in the project, and I've also tried checking out a new copy of the project. Same result.

So although it seems like a cache error, I can fix it by rolling back to a previous commit (without touching the cache) and if I clear the cache, that doesn't fix it.

What's going wrong and how do I fix it?


Solution

  • The answer was to do with a bug in gradle. Versions prior to 7.6.2 (?) failed to handle multi-release jar files (ie with multiple binaries built with different Java versions in them).

    The jackson dependency was such a jar file. This explains why the previous commit worked consistently, and this one failed consistently. The project from which I copied the config uses gradle 8.1, which is why it worked there.

    Solution: upgrade gradle.