Java 指定 JDK 供 Maven 使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2503658/
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-08-13 08:33:43  来源:igfitidea点击:

Specify JDK for Maven to use

maven-2settingsjavapom.xml

提问by DanInDC

I am trying to build a Hudson plugin I've modified and it requires jdk1.6. This is fine, but I don't see how I can tell maven where the different jdk is. I've found few mentions on the internet but they don't seem to apply to me. Some suggest adding some config to .m2/settings.xmlbut I don't have a settings.xml. Plus, I don't want to use 1.6 for all maven builds.

我正在尝试构建一个我修改过的 Hudson 插件,它需要 jdk1.6。这很好,但我不知道如何告诉 Maven 不同的 jdk 在哪里。我在互联网上发现很少提及,但它们似乎不适用于我。有人建议添加一些配置,.m2/settings.xml但我没有settings.xml. 另外,我不想将 1.6 用于所有 maven 构建。

One kink is I am using mvnin cygwin, if that matters at all. It appears I should be able to make the specification in the project pom file, but the existing pom is pretty bare.

一个问题是我mvn在 cygwin 中使用,如果这很重要的话。看来我应该能够在项目 pom 文件中制定规范,但现有的 pom 非常简单。

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

所以底线是,有没有办法为 maven 的单个调用指定 jdk?

采纳答案by Pascal Thivent

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

所以底线是,有没有办法为 maven 的单个调用指定 jdk?

Temporarily change the value of your JAVA_HOMEenvironment variable.

临时更改JAVA_HOME环境变量的值。

回答by Thorbj?rn Ravn Andersen

Hudson also allows you to define several Java runtimes, and let you invoke Maven with one of these. Have a closer look on the configuration page.

Hudson 还允许您定义多个 Java 运行时,并让您使用其中之一调用 Maven。仔细查看配置页面。

回答by feniix

I say you setup the JAVA_HOMEenvironment variable like Pascal is saying: In Cygwin if you use bash as your shell should be:

我说你JAVA_HOME像 Pascal 所说的那样设置环境变量:在 Cygwin 中,如果你使用 bash 作为你的 shell 应该是:

export JAVA_HOME=/cygdrive/c/pathtothejdk

It never harms to also prependthe java bindirectory path to the PATHenvironment variable with:

它从来没有危害也预先准备了javabin的目录路径PATH与环境变量:

export PATH=${JAVA_HOME}/bin:${PATH}

Also add maven-enforce-pluginto make sure the right JDK is used. This is a good practice for your pom.

还要添加maven-enforce-plugin以确保使用正确的 JDK。这对你的 pom 来说是一个很好的做法。

<build>
 <plugins>
   <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.6</version>
              </requireJavaVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Please, see Maven Enforcer plugin – Usage.

请参阅Maven Enforcer 插件 – 用法

回答by DevCoder

As u said "Plus, I don't want to use 1.6 for all maven builds."....So better I will say modify your pom file and specify which jdk version to use.

正如你所说的“另外,我不想为所有 maven 构建使用 1.6。”....所以我会说修改你的 pom 文件并指定要使用的 jdk 版本更好。

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

It will ensure that your particular project uses that version of jdk.

它将确保您的特定项目使用该版本的 jdk。

回答by Lin Gao

Maven uses variable $JAVACMD as the final java command, set it to where the java executable is will switch maven to different JDK.

Maven 使用变量 $JAVACMD 作为最终的 java 命令,将其设置为 java 可执行文件所在的位置会将 maven 切换到不同的 JDK。

回答by Cerber

Seems that maven now gives a solution here : Compiling Sources Using A Different JDK

似乎 maven 现在在这里提供了一个解决方案:使用不同的 JDK 编译源代码

Let's say your JAVA_HOMEpoints to JDK7 (which will run maven processes)

假设您JAVA_HOME指向 JDK7(它将运行 maven 进程)

Your pom.xmlcould be :

pom.xml可能是:

<build>
    <plugins>
        <!-- we want JDK 1.6 source and binary compatiblility -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- ... -->
        <!-- we want sources to be processed by a specific 1.6 javac -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable>${JAVA_1_6_HOME}/bin/javac</executable>
              <compilerVersion>1.3</compilerVersion>
            </configuration>
        </plugin>
    </plugins>
</build>

If your developpers just add (and customize) the following lines in their settings.xml, your pom will be platform independant :

如果您的开发人员只是在他们的 中添加(并自定义)以下几行settings.xml,您的 pom 将与平台无关:

<settings>
  [...]
  <profiles>
    [...]
    <profile>
      <id>compiler</id>
        <properties>
          <JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
          <JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>
        </properties>
    </profile>
  </profiles>
  [...]
  <activeProfiles>
    <activeProfile>compiler</activeProfile>
  </activeProfiles>
</settings>

回答by aprodan

I had build problem with maven within Eclipse on Windows 7.

我在 Windows 7 上的 Eclipse 中使用 maven 构建问题。

Though I observed mvn build was running just fine from command line.

虽然我观察到 mvn build 从命令行运行得很好。

mvn -T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml   > output.log

Eclipse was considering as default JVM a JRE installation instead of JDK so it was failing on compilation.

Eclipse 正在考虑将 JRE 安装而不是 JDK 作为默认 JVM,因此编译失败。

I added to eclipse.ini following line:

我添加到 eclipse.ini 以下行:

-vm
C:\Program Files (x86)\Java\jdk1.8.0_25\bin

Also when starting from eclipse I used in "Goals" section following list:

此外,当我从 Eclipse 开始时,我在“目标”部分使用了以下列表:

-T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml

Compilation error got solved.

编译错误得到解决。

回答by Jin Kwon

compile:compilehas a user propertythat allows you to specify a path to the javac.

compile:compile一个用户属性,允许您指定javac.

Note that this user property only works when forkis truewhich is falseby default.

需要注意的是,当该用户属性只能forktrue这是false默认。

$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/the/javac compile

You might have to double quote the value if it contains spaces.

如果值包含空格,您可能需要用双引号括起来。

> mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable="C:\...\javac" compile

See also Maven custom properties precedence.

另请参阅Maven 自定义属性优先级

回答by Parth Joshi

I know its an old thread. But I was having some issues with something similar to this in Maven for Java 8 compiler source. I figured this out with a quick fix mentioned in this articlethought I can put it here and maybe can help others:

我知道它是一个旧线程。但是我在 Maven for Java 8 编译器源代码中遇到了一些与此类似的问题。我通过本文中提到的快速修复解决了这个问题,认为我可以把它放在这里,也许可以帮助其他人:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

回答by Jobanpreet Singh

For Java 9 :

对于 Java 9:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>