How to change maven java version

Hello,

I want to work on a Java Spring project, so I created a new project and chose the backend template for Java. The template uses Java 17 as default Java version, but I want to use Java 21.

So what I did is, I changed the pkgs.zulu17 to pkgs.zulu21 and also in the project.toml file I changed the value from 17 to 21. Of course I also changed the java.version in the pom.xml file to 21.

Now when I run mvn clean install I am getting this error message:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project x: Fatal error compiling: error: release version 21 not supported -> [Help 1]

And when I run mvn -v I can see, that maven is using Java version 19.0.2, so it is obvious why it is not working

Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)
Maven home: /nix/store/sc5x4nm0rfwabs4wfdrn47fv8p1f1fwd-apache-maven-3.9.5/maven
Java version: 19.0.2, vendor: N/A, runtime: /nix/store/ikqzs5w9py247w4kvr6im2c2pbpdphnk-openjdk-19.0.2+7/lib/openjdk
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "6.1.85+", arch: "amd64", family: "unix"

But I really don’t know how I can change it, that maven is using Java version 21.
Can someone help me, please?
Thanks in advance!

In regard to my problem, I created this uservoice:

1 Like

Hi @ms-cosieu

To get java 21 it looks like you’ll need to switch your channel to "unstable" and just use pkgs.zulu

NOTE It’s not recommended to use “unstable” channel as it will become incompatible. Important to keep your eye on the NixOS versioning, especially if any issues arise

1 Like

This worked! Thank you very much :slight_smile:

1 Like