简体   繁体   中英

Maven builds with wrong JDK on jenkins

When I try to run my Jenkins Maven job I get the following error:

[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.

I ran Maven in debug mode and it displayed the right versions:

...
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

...
$ 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.

I call Maven like this:

mvn clean install -X -DskipTests -Dmaven.repo.local=../testrepo -Djava.version=1.7

Am I doing something wrong?

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.

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. Jenkins下的JDK安装

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: 在此输入图像描述

"(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.

By using similar steps you can add multiple maven installations to your Jenkins server.

Try the following steps:

1) Go to Manage Jenkins -> Configure system

2) Add JAVA_HOME and its path in Global Properties -> Environment Variables

3) Add JDK and path in JDK section

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

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.

Thanks everyone for the great answers.

In Jenkins 2.7.3 (and later on maybe) the menu navigation is slightly different:

1) Manage Jenkins

2) Global Tool Configuration

3) JDK section

4) press "JDK installations" button

5) JDK options and paths are there

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.

See LTS Changelog :

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, ie 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.

Compatibility Notes

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM