java Maven 执行器未检测到 jdk 1.7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15711547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:36:23  来源:igfitidea点击:

Maven enforcer doesn't detect jdk 1.7

mavenjava

提问by Yosi

I am using maven enforcer plugin to enforce only jdk 1.7 (I am using java.nio.file). For some reason, maven enforcer plugin can't detect jdk 1.7.

我正在使用 maven 执行器插件来仅执行 jdk 1.7(我使用的是 java.nio.file)。出于某种原因,maven 执行器插件无法检测 jdk 1.7。

λ ~/ java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
λ ~/ javac -version
javac 1.7.0_13
λ ~/ mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 15:51:28+0200)
Maven home: /usr/local/Cellar/maven/3.0.5/libexec
Java version: 1.7.0_13, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.7.5", arch: "x86_64", family: "mac"

This is the code I have in my pom.xml -

这是我在 pom.xml 中的代码 -

   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <executions>
        <execution>
          <id>enforce-versions</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireJavaVersion>
                <version>1.7</version>
              </requireJavaVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>

And this is the error I get -

这是我得到的错误 -

[INFO] --- maven-enforcer-plugin:1.2:enforce (enforce-versions) @ com.microsoft.gittf.core ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireJavaVersion failed with message:
Detected JDK Version: 1.6.0-43 is not in the allowed range 1.7.

EDIT

编辑

mvn enforcer:display-info

mvn执行者:显示信息

shows version 1.7 and not 1.6... why the enforce detects java version 1.6?

显示 1.7 版而不是 1.6 版...为什么强制检测到 Java 1.6 版?

回答by wibobm

Add the following (or create this file if it is missing) ~/.mavenrc

添加以下内容(如果缺少则创建此文件) ~/.mavenrc

export JAVA_HOME=/Library/Java/JavaVirtualMachines/{your-jdk-here}/Contents/Home

The .mavenrcis only picked up by maven so other Java programs won't be affected by this change.

.mavenrc仅由行家所以其他Java程序不会受到这一变化的影响回升。

回答by Vikram Palakurthi

The version decleration is fixed. What you gave is a exact version of java. Always remember if we are not sure about exact version we should use give the range. Change the version to [1.7,)

版本声明是固定的。你给的是一个确切版本的java。永远记住,如果我们不确定应该使用的确切版本,请给出范围。将版本更改为 [1.7,)

          <requireJavaVersion>
            <version>[1.7,)</version>
          </requireJavaVersion>

This means any version equal or greater than 1.7 is acceptable.

这意味着任何等于或大于 1.7 的版本都是可以接受的。

回答by Steve Cohen

You don't say what OS you're running under but the command "java ...." will find the first executable file called java (or java.exe on windows) on the path, this may or may not be the one that you want to use with Maven.

你没有说你在什么操作系统下运行,但命令“java ....”会在路径上找到第一个名为 java(或 windows 上的 java.exe)的可执行文件,这可能是也可能不是你想与 Maven 一起使用。

There is a system under linux called "alternatives" that maps a specific installation of java to a location that is almost certain (unless you mess with your path) to be first on your path. The purpose of this system is to allow you to keep many versions of programs such as java on your system, and have one be the default you will use unless you specify something different.

linux 下有一个名为“alternatives”的系统,它将特定的 java 安装映射到一个几乎可以肯定的位置(除非你弄乱了你的路径)在你的路径上是第一个。该系统的目的是允许您在您的系统上保留许多版本的程序,例如 java,并且除非您指定不同的内容,否则您将使用一个作为默认版本。

However this has nothingto do with Maven, which requires a JDK installation to run correctly. There are many executables in a java JDK installation that maven relies on, the javac compiler, the jar executable for building jars, just to name the most obvious ones. Using the alternatives system you would have to map alternatives for each of these and you might easily miss a few. That is why Maven relies instead on the JAVA_HOME variable.

然而,这与 Maven无关,它需要安装 JDK 才能正确运行。maven 依赖的 java JDK 安装中有许多可执行文件,javac 编译器,用于构建 jar 的 jar 可执行文件,仅举出最明显的那些。使用备选方案系统,您必须为每一个选择方案,您可能很容易错过一些。这就是 Maven 依赖于 JAVA_HOME 变量的原因。

On a linux system one handy place to set the JAVA_HOME variable is the file ~/.mavenrc. Maven reads this file if it exists.

在 Linux 系统上,设置 JAVA_HOME 变量的一个方便的地方是文件 ~/.mavenrc。如果存在,Maven 会读取该文件。