在我的机器上,我安装了两个Java版本:(1.6和1.7由我手动安装)。我在不同的项目中都需要他们。但对于Maven,我需要1.7,但我的Maven使用1.6 Java版本。

如何将Maven设置为使用1.7?


当前回答

为了避免对项目和环境变量的任何影响,您可以将Maven Compiler Plugin配置为项目的POM,指定源和目标java版本

  <plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
    ...
  </plugins>

其他回答

在windows上

如果您不希望更改系统变量中的JAVA_HOME变量。

编辑mvn.bat文件并添加如下一行

set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre

这可以在@REM ==== START VALIDATION ====之后完成,就像@Jonathan提到的那样

在Mac(和Linux ?)

如果您不想在~/中更改JAVA_HOME变量。bashc或~/.bash_profile

您可以创建一个~/。使用JAVA_HOME工具重新定义您的JAVA_HOME

export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_45`

完整性检查

您可以通过执行以下命令来验证一切是否正常工作。jdk版本应该不同。

mvn - version

then

java - version

在POM中,你可以设置编译器属性,例如1.8:

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

加上我的意见,并明确地提供解决方案。

我的Windows机器上安装了两个JDK——JDK 1.5和JDK 1.6。

我的默认(并设置为windows系统环境变量)JAVA_HOME设置为JDK 1.5。

然而,我有一个maven项目,我需要使用JDK 1.6构建(即,JBehave教程的Etsy.com)。

我在这种情况下的解决方案(工作!),是由@DanielBarbarian建议设置在mvn.bat。

对于一些不熟悉window的批处理文件,我基本上只是在mvn.bat(即%MAVEN_HOME%\bin\mvn.bat)中添加了@REM ==== START VALIDATION ====后的set JAVA_HOME=<path_to_other_jdk>行:

@REM ==== START VALIDATION ====
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45\jre
if not "%JAVA_HOME%" == "" goto OkJHome

我使用的是MAC Mini - M1芯片,在通过brew install maven安装maven后,我注意到它使用的是java 19,但我更想使用我下载到我的机器中的java 17版本。不幸的是,特定的指示重置默认的mvn java版本。以下是你可以遵循的步骤。

MVN——version #这将给出maven的安装位置 Maven home: /opt/homebrew/Cellar/ Maven /3.8.7/libexec 然后进入/opt/homebrew/Cellar/maven/3.8.7/bin文件夹 编辑mvn文件,我使用vi mvn 下面我编辑了jdk的位置,指向我有java 17 jdk的位置

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home}" exec "/opt/homebrew/Cellar/maven/3.8.7/libexec/bin/mvn"  "$@"

编辑完成后保存MVN文件 MVN -v现在会显示它的默认Java版本是17

Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/homebrew/Cellar/maven/3.8.7/libexec
Java version: 17.0.3.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "13.1", arch: "aarch64", family: "mac"

在不改变环境变量的情况下,您可以使用Maven Compiler Plugin基于项目级别管理java版本。

方法1

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

方法2

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>