如何让maven和jenkins使用特定的jdk?

Ran*_*ery 3 java maven jenkins

我的机器上有jdk 7和8,JAVA_HOME指向java 7,Jenkins使用java 8。我创建了一个maven项目,我试图使用java 8在jenkins上构建它,但出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project maven-demo: Compilation failure -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
Run Code Online (Sandbox Code Playgroud)

在 pom.xml 中我添加了以下内容:

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
Run Code Online (Sandbox Code Playgroud)

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
           <source>1.8</source>
           <target>1.8</target>
          <executable>path/to/jdk/bin/javac</executable>
        </configuration>
      </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

但使用上述两种方法中的任何一种方法我仍然遇到相同的错误。

我怎样才能更新它以使maven使用java 8?

小智 7

您必须在全局工具配置下使用不同 jdks 的名称和 JAVA_HOME 配置 Jenkins。http://path-to-your-jenkins:8080/configureTools/。然后,您可以在项目配置中选择 JDK http://path-to-your-jenkins:8080/job/yourJob/configure。在“常规”选项卡中的 JDK 字段中。

  • 谢谢,我也遇到了这个问题,这对我很有帮助。我一击:在全局工具配置中添加至少两个不同的java版本,然后常规选项卡将显示jdk选择项 (2认同)