halcyon / asdf-java

A Java plugin for asdf-vm.
MIT License
465 stars 85 forks source link

asdf-maven: JAVA_HOME not found when .tool-versions has multiple versions #97

Closed adamcohen closed 4 years ago

adamcohen commented 4 years ago

First off, apologies for not creating this issue in the asdf-maven repo, however, I was unable to do so because issue tracking is disabled for that repo. This bug is not related to asdf-java but is related to asdf-maven. @halcyon if you can enable issue tracking in https://github.com/halcyon/asdf-maven, I'll move the issue there.

Summary

I'm encountering the same issue described in JAVA_HOME not found from the skotchpine/asdf-maven repo. It looks like halcyon/asdf-maven was forked from the skotchpine/asdf-maven repo in order to fix this exact issue, which it did in Fix missing JAVA_HOME. Unfortunately, another commit was made in Simplify setting JAVA_HOME which reintroduces the bug and prevents JAVA_HOME from being set correctly when the .tool-versions file has multiple versions for a single tool:

$ mvn -version

The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

Steps to reproduce

If the .tool-versions file contains multiple versions for a single tool, for example:

java adopt-openjdk-11.0.7+10 adopt-openjdk-8u252-b09
maven 3.6.3

Then asdf where java used in asdf-maven/bin/exec-env returns the following error:

$ asdf where java
Version not installed

This prevents JAVA_HOME from being set correctly and causes mvn to return the following error:

$ mvn -version

The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

If the .tool-versions file contains a single version for a tool, for example:

java adopt-openjdk-11.0.7+10
maven 3.6.3

Then asdf where java works as expected, and so does mvn -version:

$ asdf where java
/opt/asdf/installs/java/adopt-openjdk-11.0.7+10

$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/asdf/installs/maven/3.6.3
Java version: 11.0.7, vendor: AdoptOpenJDK, runtime: /opt/asdf/installs/java/adopt-openjdk-11.0.7+10
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.19.76-linuxkit", arch: "amd64", family: "unix"

Possible fix

The fix for this bug is to revert Simplify setting JAVA_HOME and just stick with the logic implemented in Fix missing JAVA_HOME

halcyon commented 4 years ago

I've enabled issues on asdf-maven. Have you considered using the method for setting JAVA_HOME provided with asdf-java?

halcyon commented 4 years ago

I think I'll be removing asdf-maven setting of JAVA_HOME.

adamcohen commented 4 years ago

I've enabled issues on asdf-maven. Have you considered using the method for setting JAVA_HOME provided with asdf-java?

with the current maven/bin/exec-env, sourcing set-java-home.bash won't work because the variable set in /bin/exec-env takes precedence. If maven/bin/exec-env is removed, then yes, it would work.

adamcohen commented 4 years ago

Moved the issue to https://github.com/halcyon/asdf-maven/issues/1, closing this one so we can continue the discussion there