NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.08k stars 14.13k forks source link

Gradle has poor handling of Java versions #125819

Closed leo60228 closed 2 years ago

leo60228 commented 3 years ago

Describe the bug The gradle package sets JAVA_HOME to jdk, which is Java 8. This means that compilation and Gradle itself will use that version of Java, which is problematic since both projects and Gradle plugins may be using newer versions of Java.

To Reproduce Steps to reproduce the behavior:

  1. Build a project with Gradle that requires a newer version of Java.

Expected behavior I expect there to be a clear way to handle needed Java versions.

Additional context There are several workarounds here. Newer versions of Java can be configured as Gradle toolchains, but that doesn't help for plugins, and is somewhat ugly (since toolchains can only be configured via dotfiles or command-line arguments). Alternatively, the Gradle package can be overridden, but this is somewhat ugly and non-trivial with the new Nix CLI. However, this problem is complicated by the fact that many projects will expect a Java 8 compiler. A solution could be to run Gradle itself with the latest packaged version of Java and to have support for Gradle toolchains to use multiple compiler versions.

Notify maintainers

@lorenzleutgeb

Metadata Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

 - system: `"x86_64-linux"`
 - host os: `Linux 5.10.37, NixOS, 21.05.20210531.d25ea6a (Okapi)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.4pre20210529_1f39092`
 - channels(root): `"nixos-21.03pre250162.34ad166a830, unstable-21.03pre250162.34ad166a830"`
 - channels(leo60228): `"unstable-21.03pre248170.007126eef72"`
 - nixpkgs: `/nix/store/wxq267m1smpj631y19qpdyn82l8mnj90-source`

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute: gradle gradle_4 gradle_4_10 gradle_5 gradle_6 gradle_7
# a list of nixos modules affected by the problem
module:
lorenzleutgeb commented 3 years ago

See also https://github.com/gradle/gradle/issues/16645 (sadly not much interest/activity over there)

See also https://discourse.nixos.org/t/auto-detecting-java-installations/4677

leo60228 commented 3 years ago

I saw those, but if I understand correctly they only apply to compilation and not the Gradle daemon, so they wouldn't help with plugins requiring newer versions of Java (like https://github.com/FabricMC/fabric-loom).

leo60228 commented 3 years ago

The line in all-packages.nix making Gradle use Java 8 references #89731. However, as far as I can tell, that PR provides no explanation for why keeping Gradle on Java 8 was necessary. I misread the PR description, never mind.

lorenzleutgeb commented 3 years ago

Ah, it seems I misunderstood what you were aiming at. How about overriding the JDK used for your Gradle?

Making this more flexible is what I was trying to do in https://github.com/NixOS/nixpkgs/pull/119444 but that's somewhat stalled. Maybe also worth considering. Happy to take your feedback and/or improvements there.

lorenzleutgeb commented 2 years ago

I believe this was resolved with https://github.com/NixOS/nixpkgs/pull/119444

It is now possible to pass java (the JDK that Gradle itself should use) and javaToolchains (a list of JDKs that should be registered as toolchains). Sane defaults for java are used.

I'll go ahead and close this issue, because I believe it is resolved and it is inactive for >6 months.