윤현구
윤현구

Reputation: 487

Jenkins maven has other java version. How to solve it?

I'm writing a new Jenkinsfile to work with my pipeline, and I'm trying to build with Maven.

The Jenkinsfile I wrote looks like this:

node {
    stage('Mvn clean install') {
        echo 'Start to : mvn clean install'
        def mvnHome = tool name: 'mvn_3.6.1', type: 'maven'
        sh "${mvnHome}/bin/mvn -version"
        sh "${mvnHome}/bin/mvn clean install -P sonar"
    }
}

The errors that occurred during the build are:

[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ jmsight-management ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 119 source files to /var/lib/jenkins/workspace/jmsight-management-pipeline/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.009 s
[INFO] Finished at: 2019-09-04T17:32:13+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project jmsight-management: Fatal error compiling: invalid target release: 11 -> [Help 1]

The point is that the results of running mvn -version on the server and on Jenkins are different.

Check out my Jenkins console output.

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Mvn clean install)
[Pipeline] echo
Start to : mvn clean install
[Pipeline] tool
[Pipeline] sh
+ /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/mvn_3.6.1/bin/mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T04:00:29+09:00)
Maven home: /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/mvn_3.6.1
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre
Default locale: ko_KR, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

And when I run mvn -version directly on the server.

[jmuser@cicd-vm-dev01 usr]$ /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/mvn_3.6.1/bin/mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T04:00:29+09:00)
Maven home: /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/mvn_3.6.1
Java version: 11.0.4, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-11.0.4.11-0.el7_6.x86_64
Default locale: ko_KR, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

The java version is displaying differently. What am I doing wrong?

Upvotes: 0

Views: 4004

Answers (2)

Tasos Zervos
Tasos Zervos

Reputation: 686

To expand further on the answer by @seenukarthi, this shows how to use it in a scripted pipeline:

node {
  jdk = tool name: 'JDK17'
  env.JAVA_HOME = "${jdk}"

  echo "jdk installation path is: ${jdk}"
  [...]
}

Upvotes: 0

seenukarthi
seenukarthi

Reputation: 8634

To configure Java home for a Jenkins job you need the following

  1. Configure a JDK in Jenkins Global Tools. Go to Manage -Jenkins -> Global Tool Configuration in JDK section add a JDK.

    enter image description here

    Note JAVA_HOME should be a valid JDK not a JRE.

  2. You can set the JAVA_HOME using the following snippet.
    env.JAVA_HOME="${tool 'jdk1.8.0_181'}"
    

This should use the /jdks/jdk1.8.0_181 as JAVA_HOME

Upvotes: 4

Related Questions