简体   繁体   English

在Windows 10上为Maven设置JAVA_HOME

[英]Setting JAVA_HOME for Maven on Windows 10

I recently upgraded to Windows 10. I'm trying to create a new maven project in eclipse, but it doesn't seem to be picking up my JAVA_HOME setting. 我最近升级到Windows10。我试图在eclipse中创建一个新的maven项目,但是它似乎并没有占用我的JAVA_HOME设置。

Here is the JDK installation directory: 这是JDK安装目录:

And here is my JAVA_HOME environment variable setting: 这是我的JAVA_HOME环境变量设置:

If I run mvn -version in a command prompt, maven does seem to get the correct value for JAVA_HOME: 如果我在命令提示符下运行mvn -version ,则maven似乎确实获得了JAVA_HOME的正确值:

I then try to create a new maven project in eclipse, using a barebones pom.xml: 然后,我尝试使用准系统pom.xml在Eclipse中创建一个新的Maven项目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>StaticVoidGames</groupId>
    <artifactId>DatabaseTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>DatabaseTest</name>
</project>

But the project's JRE is 1.5 for some reason: 但是由于某些原因,该项目的JRE为1.5:

For what it's worth, my eclipse workspace default JRE is the same as my JAVA_HOME setting. 值得的是,我的Eclipse工作区默认JRE与我的JAVA_HOME设置相同。

I've tried manually changing the build path to use the correct jdk, but it simply reverts. 我尝试过手动更改构建路径以使用正确的jdk,但它只是还原。 I've tried cleaning the project, doing a maven update, restarting eclipse, restarting my computer, and creating a new project. 我尝试清理项目,进行maven更新,重新启动Eclipse,重新启动计算机以及创建新项目。 Maven still chooses Java 1.5, which isn't even installed on my machine. Maven仍选择Java 1.5,而Java 1.5尚未安装在我的计算机上。

All of my googling says that maven should pick up the JAVA_HOME setting to figure out which Java to use, but that doesn't seem to be the case here. 我所有的谷歌搜索都说maven应该选择JAVA_HOME设置来确定要使用的Java,但是在这里似乎并非如此。 Is there another setting that I'm missing somewhere? 我在某处缺少其他设置吗?

As per screenshoot, Maven is correctly picking up your JAVA_HOME, however what you see in Eclipse is not the version of Java installed (1.5) but the version Maven will use by default to compile . 根据屏幕截图,Maven正确地选择了JAVA_HOME,但是您在Eclipse中看到的不是安装的Java版本(1.5),而是Maven默认将用于编译的版本。
In your case, Maven will use JDK 1.8 and compile settings source and target to 1.5 (version by default). 在您的情况下,Maven将使用JDK 1.8并将sourcetarget设置编译为1.5(默认为版本)。

To set Maven to the same level as the Java version installed, you need to configure the Maven Compiler Plugin. 要将Maven设置为与所安装的Java版本相同的级别,需要配置Maven编译器插件。 Once done that and after an Eclipse update (right click on the project, Maven, Update Project), Eclipse will update its view accordingly. 完成此操作并在Eclipse更新(右键单击项目,Maven,更新项目)之后,Eclipse将相应地更新其视图。

You can add to your POM the following Maven Compiler Plugin configuration : 您可以将以下Maven编译器插件配置添加到POM中:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Alternatively, you can use well-known Maven properties to achieve the same by only settings: 另外,您可以使用众所周知的Maven属性仅通过设置即可达到相同的目的:

<properties>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM