[英]Using alternative maven and java version in vscode integrated terminal
I want to set the maven version and java version in vscode integrated terminal to versions other than those specified in system environment variables.我想将vscode集成终端中的maven版本和java版本设置为系统环境变量中指定的版本以外的版本。 I am using the "Maven for Java" extension in vscode.我在 vscode 中使用“Maven for Java”扩展。
I don't want to change my system environment variables / restart everytime I need to switch between my primary and secondary programming setting.每次我需要在主要和次要编程设置之间切换时,我都不想更改我的系统环境变量/重新启动。
So far I have specified the "java.configuration.runtimes"
and several other things in settings.json.到目前为止,我已经在 settings.json 中指定了"java.configuration.runtimes"
和其他一些内容。 The language server recognizes this setting - all fine.语言服务器识别此设置 - 一切正常。 However, running java -version
and mvn -version
in the integrated terminal still shows the java version of my system environment and also has troubles with maven:但是,在集成终端中运行java -version
和mvn -version
仍然显示我的系统环境的 java 版本,并且 maven 也有问题:
java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
mvn -version
Unrecognized option: --add-opens
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
I got the mvn command to run once with some setting combination I can't remember.我用一些我不记得的设置组合让 mvn 命令运行一次。 It was using the new java version but still the old mvn version interestingly.它使用的是新的 java 版本,但有趣的是仍然是旧的 mvn 版本。
Here is my settings.json这是我的设置。json
{
"java.configuration.runtimes": [
{
"name": "JavaSE-11",
"path": "C:\\Program Files\\Java\\jdk-11.0.7+10_hotspot",
"default": true
}
],
"java.home": "C:\\Program Files\\Java\\jdk-11.0.7+10_hotspot",
"terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"terminal.integrated.fontFamily": "Cascadia Mono PL",
"maven.executable.path": "C:\\maven-3.6.3\\bin\\mvn",
"maven.terminal.useJavaHome": true,
"maven.terminal.customEnv": [
{
"environmentVariable": "JAVA_HOME",
"value": "C:\\Program Files\\Java\\jdk-11.0.7+10_hotspot"
},
{
"environmentVariable": "M2_HOME",
"value": "C:\\maven-3.6.3\\"
},
{
"environmentVariable": "M2",
"value": "C:\\maven-3.6.3\\bin"
},
]
}
As the documentation of the Maven Extension says ( https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven ) "the value from maven.terminal.customEnv will take precedence [over the specification of java.home and useJavaHome]". As the documentation of the Maven Extension says ( https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven ) "the value from maven.terminal.customEnv will take precedence [over the specification of java.home and使用JavaHome]”。 Nevertheless, I tried choosing between one or the other but it doesn't make a difference.尽管如此,我尝试在其中一个或另一个之间进行选择,但这并没有什么不同。
When you run java --version
in integrated Terminal in VS Code, it will search JAVA_HOME set in Environment Variables, so it's still jdk1.8 .当你在VS Code的集成终端中运行java --version
时,它会搜索环境变量中设置的JAVA_HOME ,所以它仍然是jdk1.8 。
But when you run the file like helloworld.java
, the execution scripts shown in Terminal should include C:\Program Files\Java\jdk-11.0.7+10_hotspot\bin\java.exe
, which means java extension tools indeed uses jdk11 to build and compile projects. But when you run the file like helloworld.java
, the execution scripts shown in Terminal should include C:\Program Files\Java\jdk-11.0.7+10_hotspot\bin\java.exe
, which means java extension tools indeed uses jdk11 to构建和编译项目。
About maven, you still need to add it to PATH, then mvn --version
should work in Terminal:关于 maven,您仍然需要将其添加到 PATH,然后mvn --version
应该可以在终端中使用:
If you set "maven.terminal.useJavaHome": true,
, it will use JAVA_HOME in Environment Variables which is JDK1.8, you can specify jdk version with the following two ways:如果设置"maven.terminal.useJavaHome": true,
,它将使用环境变量中的JAVA_HOME ,即 JDK1.8,您可以通过以下两种方式指定 jdk 版本:
When you create a maven project, you can choose jdk version manually;创建maven工程时,可以手动选择jdk版本;
In Maven projects' pom.xml , don't forget to rebuild the project:在 Maven 项目的pom.xml中,不要忘记重建项目:
<properties> <java.version>11</java.version> </properties>
More information view Apache Maven Compiler Plugin – Setting the -source and -target of the Java Compiler .更多信息查看Apache Maven 编译器插件 – 设置 Java 编译器的 -source 和 -target 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.