Maven pipeline fails to find JDK - jenkins

I have a problem running a simple Java Maven pipeline. The used maven command fails using the expected JDK for an unknown reason.
freshly installed Jenkins running from the docker image jenkinsci/blueocean:latest
configured Oracle JDK 9 and Maven 3.5.4 at the Jenkins Tools settings pane
Jenkinsfile content:
pipeline {
agent any
tools {
jdk 'java9'
maven 'Maven3.5'
}
stages {
stage ('Initialize') {
steps {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
echo "JAVA_HOME = ${JAVA_HOME}"
'''
}
}
stage ('Build') {
steps {
sh 'java -version'
sh 'mvn --version'
}
}
}
}
Output of the "Initialize" stage script
PATH = /var/jenkins_home/tools/hudson.model.JDK/java9/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5/bin:/var/jenkins_home/tools/hudson.model.JDK/java9/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
M2_HOME = /var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5
JAVA_HOME = /var/jenkins_home/tools/hudson.model.JDK/java9
This looks valid to me. M2_HOME and JAVA_HOME point to the expected path of the Jenkins tools.
Output of the "Build" "java -version" script
+ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (IcedTea 3.10.0) (Alpine 8.191.12-r0)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
This seems to be the installed Java of the Jenkins docker host system
Failure output of the "Build" "mvn -version" script:
mvn --version
/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5/bin/mvn: exec: line 191: /var/jenkins_home/tools/hudson.model.JDK/java9/bin/java: not found
script returned exit code 127
If I bash into the Jenkins docker container, I find the java bin at the mentioned path.
Where is my mistake? Why can't Maven find the Java?
Is it a permission problem? Jenkins docker container uses a docker volume:
-v jenkins-data:/var/jenkins_home.
Best,
Lars

Not sure tools + docker in Jenkins Pipeline is working so good.
See: https://issues.jenkins-ci.org/browse/JENKINS-36159
Only way so far I've managed to get this to work was by mounting the docker node's tools directory into the docker slave.
e.g.
agent { docker
args '-v $HOME/tools:/var/lib/jenkins/tools'
stage { step {
sh "${MAVEN_HOME}/bin/mvn -version"
EDIT: Just found this too: https://issues.jenkins-ci.org/browse/JENKINS-48050

You don't need both tools. It is enough with Maven tool. It includes Java. Your pipeline should be:
pipeline {
agent any
tools {
maven 'Maven3.5'
}
stages {
stage ('Initialize') {
steps {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
echo "JAVA_HOME = ${JAVA_HOME}"
'''
}
}
stage ('Build') {
steps {
sh 'java -version'
sh 'mvn --version'
}
}
}
}
And you can delete Java Tool in Jenkins configuration tool.

This problem occurs when you're using an alpine based docker image for jenkins and your jdk is glibc based. You can solve this in two ways:
Use a jdk that is musl based. You can find them on adoptium by filtering out the OS as Alpine Linux.
Use a non-alpine based docker image.

Related

unable to override env PATH variable in Jenkins

In Jenkins Server, there are two global environment variables defined. It's in Manage Jenkins -> Configure System -> Global Properties -> Environment variables
Name: MAVEN_HOME
Value: /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven3.5.2
Name: PATH+EXTRA
$PATH:/usr/local/bin:$MAVEN_HOME/bin
I see that PATH+EXTRA will add the MAVEN PATH to the PATH environment variable. This is how my existing Server set up is. Now I need to update Jenkins with Maven 3.8.2, so I downloaded Maven 3.8.2 in the server using Manage Jenkins -> Global Tool Configuration -> Maven Installations. Now I am trying to override the global MAVEN_HOME and PATH to point to MAVEN_3.8.2 path.
In the Jenkins pipeline script
def maven_version = 'maven_3.8.2'
pipeline {
agent any
stages {
stage ('build') {
steps {
withEnv(["PATH+MAVEN=${tool maven_version}/bin"]) {
echo "PATH is: $PATH"
echo env.PATH
echo env.MAVEN_HOME
sh 'env'
sh 'mvn --version'
}
}
}
}
}
Results:
echo "PATH is: $PATH" =>
/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
echo env.PATH => /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/opt/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
echo env.MAVEN_HOME =>
/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2
sh 'env' => prints all the environment variables. Noticed following:
MAVEN_HOME=/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2
PATH=$PATH:/usr/local/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Why is the PATH is being appending with Maven 3.5.2 in the front of the path. How can I let PATH point to Maven 3.8.2?
sh 'mvn --version' => Apache Maven 3.5.2
Maven home: /var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.2
How do I get the mvn --version result with maven3.8.2?
Note: I also tried with free style project, and used following commands to override the values but the mvn --version is always printing 3.5.2. Any idea if it is a bug with Jenkins unable to override the path or is there any way to do it?
export MAVEN_HOME=/var/home/tools/hudson.tasks.Maven_MavenInstallation/maven_3.8.2
export PATH=$PATH:$MAVEN_HOME/bin
The format you used to modify the PATH variable uses concatenation that prepends the new value to the existing one. It means that
PATH+MAVEN=${tool maven_version}/bin
is an equivalent of:
PATH=${tool maven_version}/bin:$PATH
You can solve this issue by overriding the PATH variable explicitly and putting the new path at the end of the variable. Try to test the pipeline like this one:
def maven_version = 'maven_3.8.2'
pipeline {
agent any
stages {
stage ('build') {
steps {
withEnv(["PATH=${tool maven_version}/bin:$PATH"]) {
echo "PATH is: $PATH"
echo env.PATH
echo env.MAVEN_HOME
sh 'env'
sh 'mvn --version'
}
}
}
}
}

'.' is not recognized as an internal or external command for Gradle command in Jenkins when gradlew script is executed

I try to run SonarQube analysis for a Gradle project in a Jenkins Pipeline using the following code:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "./gradlew sonarqube"
}
}
}
The Gradle plugin is installed in Jenkins but I am getting the following error:
05:15:05 D:\*\*\*\*\*\*>./gradlew sonarqube
05:15:05 '.' is not recognized as an internal or external command,
Two things are incorrect in your code. On Windows machines you have to:
use backslashes instead of slashes in paths (./command → .\command)
execute script written for Windows (gradlew is a Unix script, gradlew.bat is a Windows script)
This code should work:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat '.\\gradlew.bat sonarqube'
}
}
}
Gradle Wtapper by default is provided with two script gardlew and gradlew.bat. If your project doesn't have the gradlew.bat file, execute on your Unix machine ./gradlew wrapper. The missing file will be generated.
Btw. You don't need the Jenkins Gradle plugin, when you use Gradlew Wrapper. The plugin is required when you want to provide Gradle installations for jobs, example:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "${tool(name: 'toolId', type: 'gradle')}\\bin\\gradle.bat sonarqube"
}
}
}
toolId must much the identifiers used in the Jenkins Global Tool Configuration, examples: gradle-6.X, gradle-6.8.3 etc.

Pipeline Script Jenkins

Here is my pipeline jenkins
pipeline {
agent any
stages{
stage('clone and clean repo'){
steps {
bat "git clone https://github.com/developper-root/my-app"
bat "mvn clean -f my-app"
}
}
I have this error:
'mvn' is not recognized as an internal command
or external, an executable program or a batch file.
But, this command works
C:\Users\ASUS>mvn --version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T12:57:37+01:00)
Maven home: C:\Users\ASUS\Desktop\Esprit\Ressources JEE\Semaine 1\Outils - JEE\Maven\apache-maven-
3.3.3\bin\..
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_60\jre
Default locale: fr_FR, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"
Kind regards.
Okay two things here.
When using -f flag you specify the pom.xml file so your command should be
mvn -f pom.xml clean package
Secondly if you're executing multiple line in bat file use below syntax
stage ('Build repo') {
steps {
bat '''
cd folder to pom.xml
mvn clean package
'''
}
}
It seems that MVN_HOME environment variable is not properly set. Are you running the script on Jenkins master directly? If so check if you have MVN_HOME set in Windows. If you are running this script on some slave, check if maven is installed on that machine.

JAVA_HOME not found Maven Docker image Jenkins pipeline

I am receiving a message that JAVA_HOME can't be found or is pointed to a JRE when running the following in a Jenkins Declarative Pipeline.
pipeline {
agent none
stages {
stage('Build and Package') {
agent {
docker { image 'maven:3.5.2-jdk-8' }
}
steps {
sh 'mvn clean verify'
}
}
}
}
See Installing Apache Maven:
Ensure JAVA_HOME environment variable is set and points to your JDK installation

SonarQube Scanner fails in a Jenkins pipeline due to command not found

I'd like to run SonarQube Scanner from a Jenkins pipeline and I followed the documentation.
Regarding the error, it seems that the scanner is present but some commands are not found. My jenkins instance runs in a docker.
Jenkins version : 2.46.1
SonarQube Scanner : 2.6.1
+ /var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner
/var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner: line 56: which: command not found
/var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner: line 66: exec: : not found
In the sonar-scanner script, there is this block
if [ -n "$JAVA_HOME" ]
then
java_cmd="$JAVA_HOME/bin/java"
else
java_cmd="$(which java)"
fi
And given that my JAVA_HOME was unset, the script called which and the command is not installed inside my container.
As a workaround, I set the env variable JAVA_HOME.
Make sure the PATH is complete, or check if resetting it is enough
def sonarqubeScannerHome = tool name: 'SonarQubeScanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withEnv(["PATH=/usr/bin: ..."]) {
// Your call to Sonar
sh "${sonarqubeScannerHome}/bin/sonar-scanner -e -Dsonar.host.url=..."
}
I used the setup from "Execute SonarQube Scanner within Jenkins 2 Pipeline", but with Sonar 2.5, there is an official support of Jenkins pipeline:
def scannerHome = tool 'SonarQube Scanner 2.8';
withEnv(["PATH=/usr/bin: ..."]) {
withSonarQubeEnv('My SonarQube Server') {
sh "${scannerHome}/bin/sonar-scanner"
}
}

Resources