Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to use specific Java SDK to run Maven #992

Closed
FERViborg opened this issue Aug 9, 2023 · 6 comments
Closed

Option to use specific Java SDK to run Maven #992

FERViborg opened this issue Aug 9, 2023 · 6 comments
Labels
feature-request New feature or request jdk

Comments

@FERViborg
Copy link

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
Copy link
Author

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
Copy link

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

@testforstephen testforstephen added feature-request New feature or request jdk labels Oct 30, 2023
@cypher256
Copy link

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.

  • Gradle
    (*a) settings.json (Gradle execution runtime)

    "java.import.gradle.java.home": "C:\\Program Files\\java\\jdk-21.0.1"

    (*b) build.gradle (Project version: compileJava.options.release)

    def javaVersion = 17
    java.sourceCompatibility = javaVersion // Legacy option for VS Code dependencies and Run/Debug
    compileJava.options.release = javaVersion // JEP 247: API validation

  • Maven
    (*a) settings.json (Maven execution runtime)

    "maven.terminal.customEnv": [
      {
        "environmentVariable": "JAVA_HOME",
        "value": "C:\\Program Files\\java\\jdk-21.0.1"
      }
    ]

    (*b) pom.xml (Project version: maven.compiler.release)

    <properties>
        <!-- <maven.compiler.source>17</maven.compiler.source> -->
        <!-- <maven.compiler.target>17</maven.compiler.target> -->
        <maven.compiler.release>17</maven.compiler.release><!-- JEP 247: API validation -->
    </properties>

(*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
Copy link
Author

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
Copy link

vlix-vli commented Mar 6, 2024

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
Copy link

"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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request jdk
Projects
None yet
Development

No branches or pull requests

6 participants