168

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

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

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

4

14 回答 14

174

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

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

于 2010-03-23T21:15:32.300 回答
96

似乎maven现在在这里给出了一个解决方案:Compiling Sources Using A Different JDK

假设您JAVA_HOME对 JDK7 的看法(它将运行 Maven 进程)

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>

如果您的开发人员只是在他们的 中添加(和自定义)以下行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>
于 2013-12-26T15:38:34.717 回答
46

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

请注意,此用户属性仅在默认fork情况下才有效。truefalse

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

如果该值包含空格,您可能必须双引号。

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

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

于 2016-04-15T12:27:56.290 回答
24

正如你所说的“另外,我不想对所有 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>

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

于 2013-08-24T16:03:24.623 回答
16

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

export JAVA_HOME=/cygdrive/c/pathtothejdk

将 java目录路径添加到环境变量中也永远不会有害binPATH

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

还要添加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>

请参阅Maven Enforcer 插件 - 用法

于 2010-03-24T04:37:18.607 回答
13

我知道它是一个旧线程。但是我在 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>
于 2017-01-12T03:53:49.800 回答
13

如果您已经通过brewin安装了 Java,Mac那么您很可能会在此处找到您的 Java 主目录:

/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

现在下一步是查找Java Homemaven 指向的目录。要找到它,请输入以下命令:
mvn -version

在此处输入图像描述

我们在这里感兴趣的领域是: Java versionruntime

Maven 当前指向Java 13. 另外,你可以在关键的runtime下看到Java Home路径,即:
/usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home

要更改maven的Java版本,我们需要将Java 8home路径添加到JAVA_HOMEenv变量中。

为此,我们需要
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home 在终端中运行命令:。

现在,如果我们检查 maven 版本,我们可以看到它现在指向 Java 8。

在此处输入图像描述

这样做的问题是,如果您在新终端中再次检查 maven 版本,您会发现它指向 Java 13。为避免这种情况,我建议在文件中添加JAVA_HOME变量。~/.profile

这样,每当您的终端加载时,它将默认采用您在 JAVA_HOME 中定义的值。这是您需要在~/.profile文件中添加的行:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

您可以打开一个新终端并检查 Maven 版本(mvn -version),您会发现它这次指向的是 Java 8。

于 2020-05-12T12:38:15.293 回答
7

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

于 2013-08-30T06:14:32.233 回答
3

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

于 2010-03-23T22:06:20.997 回答
3

管理多个 jdk 版本的另一种选择是jEnv

安装后,您可以通过以下方式简单地“本地”更改 java 版本,即针对特定项目目录:

jenv local 1.6

当您启用 mvn 插件时,这也将使 mvn 在本地使用该版本:

jenv enable-plugin maven
于 2019-12-20T11:12:48.390 回答
3

您还可以在主目录的文件中为 Maven 设置 JDK ~/.mavenrc

JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home'

此环境变量将由 mvn 脚本检查并在存在时使用:

  if [ -f "$HOME/.mavenrc" ] ; then
    . "$HOME/.mavenrc"
  fi

https://github.com/CodeFX-org/mvn-java-9/tree/master/mavenrc

于 2020-04-20T17:10:58.180 回答
0

我在 Windows 7 上的 Eclipse 中构建了 Maven 问题。

虽然我观察到 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 正在考虑将 JRE 安装而不是 JDK 作为默认 JVM,因此编译失败。

我在 eclipse.ini 中添加了以下行:

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

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

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

编译错误得到解决。

于 2016-03-09T17:58:02.133 回答
0

对于 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>
于 2017-11-01T13:20:01.353 回答
0

如果没有其他工作,即使您将 JAVA_HOME 设置为正确的路径,请检查是否没有覆盖 JAVA_HOME 路径<user>/.mavenrc

作为进一步的提示,该mvn文件是一个 bash 脚本(在 Linux 上)。因此,如果需要,您可以检查源 [并更改它]。

于 2021-10-20T18:53:53.170 回答