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

如何将Maven设置为使用1.7?


当前回答

在不改变环境变量的情况下,您可以使用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>

其他回答

Ondrej的回答 完美的工作。它真正解决了问题。工具链文档中详细介绍了其他一些事情

在项目POM中添加maven-toolchains-plugin 配置maven-toolchains-plugin以使用toolchains.xml文件中配置的工具链中的特定JDK。

请看下面的配置示例:

toolchains.xml

<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
  <!-- JDK toolchains -->
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>17</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>11</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

如上所述,这适用于安装了多个java版本的机器。 然后在项目的POM中,maven-toolchains-plugin被配置为使用定义为工具链的JDK版本之一。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-toolchains-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>toolchain</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <toolchains>
      <jdk>
        <version>11</version>
        <vendor>sun</vendor>
      </jdk>
    </toolchains>
  </configuration>
</plugin>

我正在使用Mac,上面的答案对我没有任何帮助。我发现maven从:~/.mavenrc中指定的路径加载自己的JAVA_HOME

我将文件的内容更改为:

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

对于Linux,它看起来像这样:

export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre

在不改变环境变量的情况下,您可以使用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>

您只需要在调用JAVA_HOME之前将您的JAVA版本添加到mvn文件中 例如,我的。bashrc文件中的JAVA版本是JAVA -17 我想用Java-11运行maven:

文件/usr/share/maven/bin/mvn: 我补充说:

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

在脚本中调用JAVA_HOME之前

然后它在Java-11中工作得很好:)

在Linux/Unix上,“mvn”shell脚本中的JAVA_HOME会被中的设置覆盖

$HOME/.mavenrc

在linux中添加JAVA_HOME和MAVEN路径变量