Developer's Diary
Software development, with Terry Ebdon
10-Jul-2020 Gradle fixed

Clean Gradle

Yesterday's Gradle behaviour was confusing. Today I started from scratch. I created an empty folder and created a Groovy app using gradle init. I then ran it with gradle run. The code compiled and I got the expected "Hello world." message. I then tried gradle test. This failed with a compilation error.

C:\z>gradle test
> Task :compileTestGroovy
startup failed:
C:z\src\test\groovy\z\AppTest.groovy: 11: unable to resolve class App
@ line 11, column 19.
def app = new App()
^
1 error
> Task :compileTestGroovy FAILED
FAILURE: Build failed with an exception.

Gradle compiled and ran the template app but will not run the template Spock test. Now it's refusing to re-run the app. It's as if running the test breaks Gradle.

C:\z>gradle run
> Task :run FAILED
Error: Could not find or load main class z.App
Caused by: java.lang.ClassNotFoundException: z.App
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command 'java.exe'' finished with non-zero exit value 1

Up-to-date

Running gradle --console=plain clean build adds some information.

C:\z>gradle --console=plain clean build
> Task :clean UP-TO-DATE
> Task :compileJava NO-SOURCE
> Task :compileGroovy UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar
> Task :startScripts
> Task :distTar
> Task :distZip
> Task :assemble
> Task :compileTestJava NO-SOURCE

Note that it thinks the clean and compileGroovy tasks are up-to-date. The results are cached, deleting the build folder makes no difference. I then deleted everything in the folder, re-ran gradle init and re-ran the gradle --console=plain clean build command. (Yes, clean is redundant.) That works.

I can see that the class files have all built:

C:\z>dir build\classes\*.class /s /b
C:\z\build\classes\groovy\main\z\App.class
C:\z\build\classes\groovy\test\z\AppTest.class
C:\z>

I ran the test, it succeeded. I ran the app, with gradle --console=plain run. It fails:

> Task :run FAILED
Error: Could not find or load main class z.App
Caused by: java.lang.ClassNotFoundException: z.App
FAILURE: Build failed with an exception.

At this point the test works but the app fails, claiming the app class doesn't exist. This is contradictory. Looking for the classes shows something odd:

C:\z>dir build\classes\*.class /s /b
The system cannot find the file specified.
C:\z>

Missing classes

Where are the compiled classes? I tried deleting the build folder and re-running the test, but Gradle claims the compile tasks are up-to-date. Gradle is caching the build results. If I force it to re-run the tasks then everything starts working again:

C:\z>gradle --console=plain --rerun-tasks run
> Task :compileJava NO-SOURCE
> Task :compileGroovy
> Task :processResources NO-SOURCE
> Task :classes
> Task :run
Hello world.
BUILD SUCCESSFUL in 4s
2 actionable tasks: 2 executed
C:\z>

This is weird, could it be a bug in Gradle 6.5? I'll experiment with a different version.

Failing configuration

C:\z>gradle --version
------------------------------------------------------------
Gradle 6.5
------------------------------------------------------------
Build time: 2020-06-02 20:46:21 UTC
Revision: a27f41e4ae5e8a41ab9b19f8dd6d86d7b384dad4
Kotlin: 1.3.72
Groovy: 2.5.11
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 11.0.2 (Oracle Corporation 11.0.2+9)
OS: Windows 10 10.0 amd64
C:\z>
Note
I'm deliberately using an older JVM, this is a work-around for a previous Grails failure.

Problem solved

A new Gradle version was released 10 days ago, on June 30th. This does not fix the issue. Versions 6.5 and 6.5.1 both exhibit the flawed behaviour. Rolling back to Gradle version 5.2.1 fixes it. I'm now wondering if my previous Grails build issues had the same cause. V5.2.1 dates from February 2019. Gradle is a very active product, this roll-back puts me nearly two dozen versions behind. It was picked purely as I've previously used it, and had it unzipped and ready to go. When I get a chance I'll try rolling it forward until it stops working, then check the change logs and bug list.

Working configuration

C:\z2>gradle --version
------------------------------------------------------------
Gradle 5.2.1
------------------------------------------------------------
Build time: 2019-02-08 19:00:10 UTC
Revision: f02764e074c32ee8851a4e1877dd1fea8ffb7183
Kotlin DSL: 1.1.3
Kotlin: 1.3.20
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 11.0.2 (Oracle Corporation 11.0.2+9)
OS: Windows 10 10.0 amd64
C:\z2>

09-JUL-2020 👈 Top of page 👉 11-JUL-2020

© 2020 Terry Ebdon.

Find me coding on GitHub, networking on LinkedIn, answering questions on Stack Exchange and hanging out on twitter.