Jenkins pipeline mvn: command not found - maven

My test is working fine as Maven project. It fetches code from Git.
I wanted to execute the same in Pipeline so I wrote the below script for pipeline project.
pipeline {
agent any
options {
timestamps()
}
stages{
stage('Just Testing') {
steps {
git "https://github.com/myRepo.git"
script{
sh 'mvn test'
}
step([$class : 'Publisher', reportFilenamePattern : "**/testng-results.xml"])
}
}
}
}
But when I execute I am getting mvn: command not found error Java and Maven path is set correctly in global tool configuration (I am sure because I am able to execute other maven projects, only pipeline is failing.)
Console output:
Running on Jenkins in /Users/abacker/.jenkins/workspace/parallelTestNG
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Just Testing)
[Pipeline] git
00:03:52 No credentials specified
00:03:52 > git rev-parse --is-inside-work-tree # timeout=10
00:03:52 Fetching changes from the remote Git repository
00:03:52 > git config remote.origin.url https://github.com/myRepo.git # timeout=10
00:03:52 Fetching upstream changes from https://github.com/myRepo.git
00:03:52 > git --version # timeout=10
00:03:52 > git fetch --tags --force --progress -- https://github.com/myRepo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
00:03:54 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
00:03:54 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
00:03:54 Checking out Revision e82f48106aa72d0275591926d9cd3226ba0fefd7 (refs/remotes/origin/master)
00:03:54 > git config core.sparsecheckout # timeout=10
00:03:54 > git checkout -f e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
00:03:54 > git branch -a -v --no-abbrev # timeout=10
00:03:54 > git branch -D master # timeout=10
00:03:54 > git checkout -b master e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
00:03:54 Commit message: "First commit"
00:03:54 > git rev-list --no-walk e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
[Pipeline] script
[Pipeline] {
[Pipeline] sh
00:03:54 + mvn test
00:03:54 /Users/abacker/.jenkins/workspace/parallelTestNG#tmp/durable-818e64a8/script.sh: line 1: mvn: command not found
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
How can I fix this issue?

You don't have any tools declared for your maven project.
This would be a good starting point for running mvn goals from Jenkins.
Declarative maven project.

Following pipeline script did the job for me. unlike maven projects created in jenkins you need to add the maven and java as tool for the script.
MAVEN_HOME - configured in "Managing Jenkins" → "Global Tool Configuration"
JAVA_HOME - configured in "Managing Jenkins" → "Global Tool Configuration"
pipeline {
agent any
tools {
maven 'MAVEN_HOME'
jdk 'JAVA_HOME'
}
stages {
stage ('Build') {
steps {
sh 'mvn -B -ntp -Dmaven.test.failure.ignore verify'
}
}
}
}

In the pipeline script you have to give maven path like
def mvnHome = tool name: 'maven-3.8.6', type: 'maven'
sh "${mvnHome}/bin/mvn package"
in maven stage
I hope it will work because it works for me.. thank you

Related

mvn clean package mvn: command not found

sudo: mvn: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
i've tried everything.
i'm running jenkins with username swapnil in terminal
environment variable is file
like if i do echo $PATH i get >>>/mnt/servers/apache-maven-3.8.6/bin
and i am able to build manually on terminal(mvn clean package)
in jenkins global tool configuration i have configured maven
maven Name: apache-maven-3.8.6
MAVEN_HOME = /mnt/servers/apache-maven-3.8.6
i'm getting really frustrated.... please help me

How can I build my maven application succesfully on Jenkins?

I am new to Jenkins and don't have much luck. I am trying to build my spring boot application with maven in it. It builds succesfully until I add commands "clean compile package" in Invoke top-level Maven targets.
The build fails.
Started by user unknown or anonymous
Running as SYSTEM
Building in workspace /var/jenkins_home/jobs/sample/workspace
The recommended git tool is: NONE
No credentials specified
> git rev-parse --resolve-git-dir /var/jenkins_home/jobs/sample/workspace/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/rshncp/stackApp.git # timeout=10
Fetching upstream changes from https://github.com/rshncp/stackApp.git
> git --version # timeout=10
> git --version # 'git version 2.30.2'
> git fetch --tags --force --progress -- https://github.com/rshncp/stackApp.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision 497d77eeef33f9cc19c5390d21cf49e90ac24e05 (refs/remotes/origin/main)
> git config core.sparsecheckout # timeout=10
> git checkout -f 497d77eeef33f9cc19c5390d21cf49e90ac24e05 # timeout=10
Commit message: "first commit"
> git rev-list --no-walk 497d77eeef33f9cc19c5390d21cf49e90ac24e05 # timeout=10
[workspace] $ mvn clean compile package
FATAL: command execution failed
java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
Caused: java.io.IOException: Cannot run program "mvn" (in directory "/var/jenkins_home/jobs/sample/workspace"): error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at hudson.Proc$LocalProc.<init>(Proc.java:252)
at hudson.Proc$LocalProc.<init>(Proc.java:221)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:995)
at hudson.Launcher$ProcStarter.start(Launcher.java:507)
at hudson.Launcher$ProcStarter.join(Launcher.java:518)
at hudson.tasks.Maven.perform(Maven.java:367)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:806)
at hudson.model.Build$BuildExecution.build(Build.java:198)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:514)
at hudson.model.Run.execute(Run.java:1888)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:99)
at hudson.model.Executor.run(Executor.java:431)
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
How can I fix this?
Download Maven as plug-in. You must go to Jenkins Global Tool Configuration, and configure a Maven version with automatic installer (from the web).
Setup in global configuration, maven version. Look second answer of this case Cannot run program "mvn" error=2, No such file or directory

How to debug the following Jenkins error?

I just recently created a new Jenkins pipeline which is reading from Jenkinsfile script in the repository.
I created a branch and then committed this script to the repo. Now I am trying to build this branch on jenkins but I get the following error and I am not sure what I am doing wrong as I basically copied the Jenkinsfile from an existing repo which I can build successfully. This is my first build for this project but its not working. please advice. when it says 'skipped due to earlier failure' what does it mean and how to identify this failure?
Commit message: "Project Foundation"
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
[Pipeline] sh
+ git describe --tags
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
Stage "Build" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
Stage "Test" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Publish Dev)
Stage "Publish Dev" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 128
Finished: FAILURE
Your repository seems to not have tags to be described.
Jenkins reaches to run sh shell script git describe --tags (see the plus sign + in the beginning of the line, which means it was run), but it fails fails with exit code 128, shown in the end of console output:
ERROR: script returned exit code 128
Apparently, git describe seems to exit with that code if there is nothing to describe (tags in your case).
To continue from here you can remove the line, or add ignore the error by adding || true to the command:
sh "git describe --tags || true"

Maven and jenkins 'mvn' is not recognized as an internal or external command, operable program or batch file

I have compiled maven manually in cmd and it is successfull.
However, when i tried to run the jenkins job, it is saying
"'mvn' is not recognized as an internal or external command,
operable program or batch file."
Jenkins console output:
enter code hereStarted by user prakash peram
Building in workspace C:\Program Files (x86)\Jenkins\workspace\atmosphere3
git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
git.exe config remote.origin.url https://github.com/g0t4/jenkins2-course-spring-boot.git # timeout=10
Fetching upstream changes from https://github.com/g0t4/jenkins2-course-spring-boot.git
git.exe --version # timeout=10
git.exe fetch --tags --progress https://github.com/g0t4/jenkins2-course-spring-boot.git +refs/heads/:refs/remotes/origin/
git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 4bde91e33e2860b2aab142028c04eff37b7791f2 (refs/remotes/origin/master)
git.exe config core.sparsecheckout # timeout=10
git.exe checkout -f 4bde91e33e2860b2aab142028c04eff37b7791f2
Commit message: "Adding in jacoco code coverage"
git.exe rev-list --no-walk 4bde91e33e2860b2aab142028c04eff37b7791f2 # timeout=10
[atmosphere3] $ cmd.exe /C "mvn -f spring-boot-samples/spring-boot-sample-atmosphere/pom.xml compile && exit %%ERRORLEVEL%%"
'mvn' is not recognized as an internal or external command,
operable program or batch file.
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
I have set the MVN_HOME and M2_HOME in the environment variables.
Please seek additional info if required.
you have to set up the global tool inside jenkins adding maven and JDK
inside jenkins in the left side panel in dashboard we found general settings, then inside global tools we can configure the JDK and MAVEM environment in the same way as windows, but this time will be made it within localhost jenkins with the path of maven and jdk of program files

Jenkins maven git hub integration mvn.bat' is not recognized as an internal or external command

Hi i am new to this Area i did lot of work on this unable to resolve this
i am integrating my selenium project i stored in git hub and i want to schedule it from localhost jenkins
First i wrote a sample maven project and pushed to git i am configuring jenkins to fetch it from git hub and build on local machine
but i am getting the following error
Started by user anonymous
Building in workspace C:\Program Files (x86)\Jenkins\jobs\GitJenkinsDemo\workspace
> git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git.exe config remote.origin.url https://github.com/chaitanyap97/Gittest.git # timeout=10
Fetching upstream changes from https://github.com/chaitanyap97/Gittest.git
> git.exe --version # timeout=10
> git.exe -c core.askpass=true fetch --tags --progress https://github.com/chaitanyap97/Gittest.git +refs/heads/*:refs/remotes/origin/*
> git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 0ccaf41f5ad97bf665f39b43093f4d94863f824a (refs/remotes/origin/master)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f 0ccaf41f5ad97bf665f39b43093f4d94863f824a
First time build. Skipping changelog.
[workspace] $ cmd.exe /C '"mvn.bat -f GitTestDemo/pom.xml mvn test && exit %%ERRORLEVEL%%"'
'mvn.bat' is not recognized as an internal or external command,
operable program or batch file.
Build step 'Invoke top-level Maven targets' marked build as failure
Finished: FAILURE
Configurations are:
JAVA_HOME:C:\Program Files\Java\jdk1.7.0_79
M2_HOME:C:\Program Files\apache-maven-3.3.3
PATH:C:\Program Files\apache-maven-3.2.3\bin;%JAVA_HOME%\bin;C:\Program Files (x86)\Git\cmd
Or
PATH:%M2_HOME%\bin;%JAVA_HOME%\bin;C:\Program Files (x86)\Git\cmd
Please help,
Thanks.
This is because in Maven 3.3.1, the mvn.bat file is replaced with mvn.cmd. Please refer to Bug 251213 - new maven command line extension for windows change from .bat to .cmd.
So please change mvn.bat to mvn.cmd in your command accordingly.

Resources