Unable to build Java 16 app with Gradle via Docker



I have a standard Springboot application written with Java 16. This compiles and builds just fine in my IDE and via CI/CD etc.

However, attempting to move the building to a docker file and I’m hit with:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Cannot invoke "javax.tools.JavaCompiler.getStandardFileManager(javax.tools.DiagnosticListener, java.util.Locale, java.nio.charset.Charset)" because "this.delegate" is null

I assume this is some under the hood thing but I can’t figure out how to resolve it. I’ve tried different Gradle images with different builds of the JVM (Hotspot, OpenJ9) but all have the same result.

FROM gradle:jre16-hotspot AS build

COPY --chown=gradle:gradle . /home/gradle/srcn

WORKDIR /home/gradle/src

RUN gradle build

Nothing fancy in my Dockerfile either. I’d love to understand why I only get this issue when building via Docker, and not when building in any other manner.

Any tips?

Answer

Since the compiler is available only in JDK, you need to replace the first line in your Dockerfile with:

FROM gradle:jdk16-hotspot AS build


Source: stackoverflow