java Maven 在 jenkins 上使用错误的 JDK 构建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36622142/
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
Maven builds with wrong JDK on jenkins
提问by fa94
When I try to run my Jenkins Maven job I get the following error:
当我尝试运行我的 Jenkins Maven 作业时,我收到以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project myProject: Fatal error compiling: invalid target release: 1.8 -> [Help 1]
This indicates that Maven uses Java 1.8 but it is supposed to use Java 1.7.
这表明 Maven 使用 Java 1.8,但它应该使用 Java 1.7。
I ran Maven in debug mode and it displayed the right versions:
我在调试模式下运行 Maven,它显示了正确的版本:
...
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven305
Java version: 1.7.0_71, vendor: Oracle Corporation
Java home: /var/lib/jenkins/tools/hudson.model.JDK/JDK_7u71/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.8.0-44-generic", arch: "amd64", family: "unix"
...
The Jenkins server runs with Java 1.8
Jenkins 服务器使用 Java 1.8 运行
...
$ java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
...
$ javac -version
javac 1.8.0_74
...
I tried running the job with an empty Maven repo and telling Maven exactly which version to use but with no success.
我尝试使用空的 Maven 存储库运行该作业,并准确地告诉 Maven 使用哪个版本,但没有成功。
I call Maven like this:
我这样称呼 Maven:
mvn clean install -X -DskipTests -Dmaven.repo.local=../testrepo -Djava.version=1.7
Am I doing something wrong?
难道我做错了什么?
采纳答案by fa94
I finally found out what the problem was.
我终于发现了问题所在。
I use another module in my build which I download from a nexus server. Someone made a mistake and uploaded a version to the nexus which is out of sync with the one under version control. The version on nexus has Java 1.8 specified but the one under version control has Java 1.7 so I did not notice it earlier.
我在我的构建中使用了另一个模块,我从 nexus 服务器下载了它。有人犯了一个错误,上传了一个与版本控制下的版本不同步的版本。nexus 上的版本指定了 Java 1.8,但受版本控制的版本是 Java 1.7,所以我之前没有注意到。
Thanks everyone for the great answers.
感谢大家的精彩回答。
回答by Talijanac
Proper solution is to add needed JDK to Jenkins global configuration and then to specifically choose needed JDK version/installation under the project build configuration.
正确的解决方案是将需要的JDK添加到Jenkins全局配置中,然后在项目构建配置下专门选择需要的JDK版本/安装。
Start by copying needed JDKs to your build machine. Then open Jenkins configuration and scroll to "JDK" section [Jenkins -> Manage Jenkins -> Configure System]. Add desired JDKs to Jenkins. Following picture is example of Jenkins configured with two different JDKs.
首先将所需的 JDK 复制到您的构建机器。然后打开 Jenkins 配置并滚动到“JDK”部分 [Jenkins -> 管理 Jenkins -> 配置系统]。将所需的 JDK 添加到 Jenkins。下图是配置了两个不同 JDK 的 Jenkins 示例。
Now under project configuration [YourProject -> Configure], a new option will be available. You can select desired JDK to build project with as in the picture:
现在在项目配置 [YourProject -> Configure] 下,将提供一个新选项。您可以选择所需的JDK来构建项目,如图:
"(Default)" JVM in picture refers to the JDK under which Jenkins run. Easy fix would be to change this JDK to the version needed for your project (either by changing Jenkins start script or using JAVA_HOME). This will work. However once you have a need to build two or more conflicting projects, you are gonna need to configure multiple JDKs anyway.
图中的“(默认)”JVM 是指运行 Jenkins 的 JDK。简单的解决方法是将此 JDK 更改为您的项目所需的版本(通过更改 Jenkins 启动脚本或使用 JAVA_HOME)。这将起作用。但是,一旦您需要构建两个或更多相互冲突的项目,无论如何您都需要配置多个 JDK。
By using similar steps you can add multiple maven installations to your Jenkins server.
通过使用类似的步骤,您可以将多个 maven 安装添加到您的 Jenkins 服务器。
回答by deeveeABC
Try the following steps:
尝试以下步骤:
1) Go to Manage Jenkins -> Configure system
1) 转到管理 Jenkins -> 配置系统
2) Add JAVA_HOME and its path in Global Properties -> Environment Variables
2)在全局属性->环境变量中添加JAVA_HOME及其路径
3) Add JDK and path in JDK section
3)在JDK部分添加JDK和路径
If not, try following this post as a lot of solutions are offered here: Maven uses an incorrect Java version when it is executed by Jenkins
如果没有,请尝试关注这篇文章,因为这里提供了很多解决方案:Maven 在 Jenkins 执行时使用了不正确的 Java 版本
回答by joro
In Jenkins 2.7.3 (and later on maybe) the menu navigation is slightly different:
在 Jenkins 2.7.3(也许以后)菜单导航略有不同:
1) Manage Jenkins
1) 管理詹金斯
2) Global Tool Configuration
2) 全局工具配置
3) JDK section
3)JDK部分
4) press "JDK installations" button
4)按“JDK安装”按钮
5) JDK options and paths are there
5) JDK 选项和路径在那里
回答by Pedro
From Jenkins 2.60.1 (June 2017) LTS on, maven jobs require JDK 1.8 or up. Only maven jobs require this, all the rest (for example freestyle ones) can fix the jdk version that fit their needs best.
从 Jenkins 2.60.1(2017 年 6 月)LTS 开始,maven 作业需要 JDK 1.8 或更高版本。只有 Maven 工作需要这个,所有其他工作(例如自由式工作)可以修复最适合他们需求的 jdk 版本。
See LTS Changelog:
请参阅LTS 更新日志:
2.60.1 is the first Jenkins LTS release that requires Java 8 to run. If you're using the Maven Project type, please note that it needs to use a JDK capable of running Jenkins, i.e. JDK 8 or up. If you configure an older JDK in a Maven Project, Jenkins will attempt to find a newer JDK and use that automatically.
2.60.1 是第一个需要 Java 8 才能运行的 Jenkins LTS 版本。如果您使用的是 Maven 项目类型,请注意它需要使用能够运行 Jenkins 的 JDK,即 JDK 8 或更高版本。如果您在 Maven 项目中配置较旧的 JDK,Jenkins 将尝试查找较新的 JDK 并自动使用它。
Using the Maven project type with Java 7
Users with jobs configured with the "Maven project" type may not be able to use Java 7 for their Maven jobs. The correct behavior is not guaranteed so proceed at your own risk. The Maven Project uses Jenkins Remoting to establish "interceptors" within the Maven executable. Because of this, Maven uses Remoting and other Jenkins core classes, and this behavior may break an update.
在 Java 7 中使用 Maven 项目类型
使用“Maven 项目”类型配置作业的用户可能无法将 Java 7 用于他们的 Maven 作业。不能保证正确的行为,因此请自行承担风险。Maven 项目使用 Jenkins Remoting 在 Maven 可执行文件中建立“拦截器”。因此,Maven 使用 Remoting 和其他 Jenkins 核心类,这种行为可能会破坏更新。