keeganwitt / docker-gradle

Docker images with Gradle
https://hub.docker.com/_/gradle/
Apache License 2.0
140 stars 72 forks source link

Gradle, Docker and JDK 22 issue #289

Open sergey-morenets opened 1 month ago

sergey-morenets commented 1 month ago

Hi

I failed to build my application using Gradle 8.8 and JDK 22. I assume from the documentation that current Docker image supports both JDK 21 and 22 using specific gradle.properties:

org.gradle.java.installations.auto-detect=false
org.gradle.java.installations.auto-download=false
org.gradle.java.installations.fromEnv=JAVA_LTS_HOME,JAVA_CURRENT_HOME

Unfortunately if I try to build my application using this default configuration I receive an error:

error: invalid source release: 22

So I changed this gradle.properties to using only current (JDK22) JDK version:

org.gradle.java.installations.auto-detect=false
org.gradle.java.installations.auto-download=false
org.gradle.java.installations.fromEnv=JAVA_CURRENT_HOME

But the error still happens and I see from the logs that Gradle build still uses JDK 21:

Received JVM installation metadata from '/opt/java/openjdk': {JAVA_HOME=/opt/java/openjdk, JAVA_VERSION=21.0.3, JAVA_VENDOR=Eclipse Adoptium, RUNTIME_NAME=OpenJDK Runtime Environment, RUNTIME_VERSION=21.0.3+9-LTS, VM_NAME=OpenJDK 64-Bit Server VM, VM_VERSION=21.0.3+9-LTS, VM_VENDOR=Eclipse Adoptium, OS_ARCH=amd64}

Here is my Dockerfile:

FROM gradle:8-jdk-21-and-22-alpine 

COPY gradle.properties /home/gradle/.gradle/gradle.properties

COPY . /home/gradle/

RUN  gradle --info clean build 

and build.gradle.kts:

plugins {
    id("java-library") 
}

   java { 
       sourceCompatibility = org.gradle.api.JavaVersion.VERSION_22
       targetCompatibility = org.gradle.api.JavaVersion.VERSION_22
   }   

Please, advise how to build Docker image for Java 22 application.

sergey-morenets commented 1 month ago

I tested different options and the only option to tell Gradle to pick up specific JDK version was using toolchains:

java {
  toolchain {
    languageVersion.set(JavaLanguageVersion.of(22))
  }
}

I guess this code should be explicitly written in the documentation for developers who migrate their Java applications to JDK 22 and haven't used toolchains before. Because previously Gradle Docker image used single JDK and there was no need for using toolchains.

keeganwitt commented 1 month ago

It was required to use toolchains for Java 22 with Gradle 8.7. 8.8 added full Java 22 support, so toolchains aren't needed for that version currently and that image variant isn't really needed (but still can be used). I missed adding Java 22 images once this support was added (sorry about that). Follow the official images PR here for that support: docker-library/official-images#16994.

I've tried to document this here (where I link to Gradle's toolchain documentation). I am very much open to suggestions to improve the documentation.

When Java 23 comes out in a couple months, likely Gradle 8.9 will have partial support for Java 23 once again, and we'll again have to use Java 21 to launch Gradle and toolchains to compile with Java 23 and the combo image will again be relevant.

Hopefully that made sense...

keeganwitt commented 1 month ago

To elaborate on this a little more: the Java Gradle plugin defaults to using the Java version that launched the daemon. Since this image launches with Java 21, that will mean it will default to using 21 unless you specify otherwise using a toolchain.

The intent of the lts-and-current image variant is that Gradle will always be launched with the latest LTS Java supported by Gradle. For now that means launching with 21 until 25 comes out, then when Gradle supports 25 it will be the version used to launch Gradle. And additionally whatever Java is current will be installed alongside the LTS version (which when 25 comes out might be just 25 by itself until 26 comes out).

ArvinB commented 4 weeks ago

@keeganwitt Can these images be built with the s390x platform as well? Using UBI9 requires me to use JDK21 at a minimum.

keeganwitt commented 4 weeks ago

Thank you for calling that out. At one time, I believe the official eclipse-temurin images didn't have ppc64le and s390x architectures for that Java version. But now they do, so I need to update my manifest file.

Here's the PR to do that docker-library/official-images#17060.

keeganwitt commented 4 weeks ago

@sergey-morenets Can we close this issue? Or was there something further you needed?