microsoft / vscode-maven

VSCode extension "Maven for Java"
https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven
Other
179 stars 88 forks source link

Option to use specific Java SDK to run Maven #992

Closed FERViborg closed 5 months ago

FERViborg commented 11 months ago

Type: Feature Request

Hello,

Sometimes in the development process it is needed to have 2 different versions of Java Maven projects. Further more, sometimes it is crucial to build the application running Maven with the specific Java.

  1. I would like to have an option to list available Java SDKs like I can in "Extension Pack for Java" using "java.configuration.runtimes".
  2. I would like VSCode to use maven with the specific Java SDK if available based on what it is read from pom.xml file.

Currently, there is a way to set up maven path (maven.executable.path) and to set its Java path:

    "maven.terminal.customEnv": [
        {
            "environmentVariable": "JAVA_HOME",
            "value" : "C:\\Java\\jdk-20.0.1"
        }
    ],

However, I'm in a situation where I really need to run maven build for one project with one Java version and another project where I need to run maven with another Java version. If I want to do that, I constantly need to change the value for JAVA_HOME. I would like to avoid that.

Extension version: 0.42.0 VS Code version: Code 1.81.0 (6445d93c81ebe42c4cbd7a60712e0b17d9463e97, 2023-08-02T12:37:13.485Z) OS version: Windows_NT x64 10.0.19045 Modes:

FERViborg commented 11 months ago

I'm also aware that the spring boot applications can have mvnw.cmd file, and in that file I can add SET JAVA_HOME=C:\Java\jdk-20.0.1 so that the build works for each project independently.

However, if a project would change Java, it would be necessary not only to change the pom.xml, but also mvnw.cmd and mvnw.

Not only that, all developer that use Windows would have to have Java placed in the same folder and all the developers that use Linux would have to have Java placed in the same directory which makes this approach unusable.

Tarrowren commented 9 months ago

Is it possible to automatically get the jdk version used by the project and then utilize the existing java.configuration.runtimes?

cypher256 commented 6 months ago

For your information, since Java 9, even if you specify the latest JDK in customEnv, specifying release instead of source or target will ensure that you are built strictly with that version of the Java API.


(a) The settings.json can be configured by project (workspace). (b) The java.configuration.runtimes that best matches this version will be used.

source: Java Extension Pack Auto Config > Specify Project Java Version

FERViborg commented 5 months ago

I went back and checked which versions of Java I was using and I found that I've been using Java 20 to run maven:

...
{
    "java.configuration.runtimes": [
        {
            "name": "JavaSE-1.7",
            "path": "C:\\Java\\java_1.7.1_64\\",
        },
        {
            "name": "JavaSE-1.8",
            "path": "C:\\Java\\jdk8u372-b07"
        },
        {
            "name": "JavaSE-17",
            "path": "C:\\Java\\jdk-17.0.7+7"
        },
        {
            "name": "JavaSE-20",
            "path": "C:\\Java\\jdk-20.0.1"
        }
    ],
    "maven.executable.path": "C:\\Programs\\Maven\\3.9.1\\bin\\mvn.cmd",
    "maven.terminal.customEnv": [
        {
            "environmentVariable": "JAVA_HOME",
            //"value": "C:\\Java\\jdk-17.0.7+7"
            "value": "C:\\Java\\jdk-20.0.1"
            //"value": "C:\\Java\\jdk8u372-b07"
        }
    ],
...

and Java 7 for the application image.

That combination is not compatible:

[ERROR] Source option 7 is no longer supported. Use 8 or later.
[ERROR] Target option 7 is no longer supported. Use 8 or later.

I've found that this is the extreme edge case because only about 0.28% (according to my google search) is using Java 7.

Further more, even if the VSC would first read pom.xml to determine that Java 7 should be used for maven, then the maven would complain that the Java 7 is not supported (because Java 8 is minimal version for latest maven).

I need to use Java 8 to build the Java 7 applications until I can migrate to Java 8, and Java 20+ for modern applications so I guess I'll need to change the value of maven.terminal.customEnv for the time being.

Not sure if there are any more edge cases like this. If so, that other person can open a new one.

Thank you all for your inputs.

vlix-vli commented 4 months ago

I'm confused about the maven.terminal.customEnv setting, when I set my user profile with

JAVA_HOME=$ENVIRONMENTS_ROOT/Jdk/jdk-21.0.2.jdk/Contents/Home
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

and set maven.terminal.customEnv with

"maven.terminal.customEnv": [
        {
            "environmentVariable": "JAVA_HOME",
            "value": "/Users/Environments/Jdk/zulu-8.jdk/Contents/Home"
        }
    ]

mvn commond will use the user profile setting:

-> % echo $JAVA_HOME
/Users/Environments/Jdk/jdk-21.0.2.jdk/Contents/Home

when I remove JAVA_HOME in user profile, I will get the maven.terminal.customEnv setting:

-> % echo $JAVA_HOME
/Users/Environments/Jdk/zulu-8.jdk/Contents/Home

if there is a setting make maven.terminal.customEnv overwrite user profile, I will not care about JAVA_HOME in user profile .

thejoyfulcoder commented 4 months ago

"maven.terminal.customEnv": [ { "environmentVariable": "JAVA_HOME", "value": "/Users/Environments/Jdk/zulu-8.jdk/Contents/Home" } ]

If this property is set in vscode's workspace setttings, then any maven project in that workspace will use this jdk and not the one set in your user environment variables. However, if this property is not set then maven will use your env vars path for java home