How can I build my maven application succesfully on Jenkins? - maven

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

Related

Jenkins pipeline mvn: command not found

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

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

gitflow-maven-plugin: Remote branch 'origin/master' is ahead of the local branch 'master'

I use maven to create releases using gitflow-maven-plugin. My projects builds fine, unless I create a release calling the following maven command
mvn -B gitflow:release
This fails with the following error
[ERROR] Failed to execute goal com.amashchenko.maven.plugin:gitflow-maven-plugin:1.9.0:release (default-cli) on project test:
release: Remote branch 'origin/master' is ahead of the local branch 'master'.
Execute git pull. -> [Help 1]
There are no changes on the master so there should be no such error, especially also cause the plugin does a fetch beforehand as shown in the log
16:03:21 [INFO] Fetching remote branch 'origin master'.
16:03:21 [INFO] Comparing local branch 'master' with remote 'origin/master'
Any clue what could cause that?
When I downloaded the workspace zip archive to inspect the cause of this problem, git showed that that master branch was indeed behind the remote master.
In the Jenkinsfile I only did
git checkout develop && git pull --rebase
I fixed the problem by pulling the master branch, too
git checkout master && git pull --rebase
git checkout develop && git pull --rebase
Probably a git fetch also would have sufficed

Error while bulding maven project - TeamCity

Everytime I try to build a Maven project hosted on a gitlab server with TeamCity, I get an error from the buildAgent. Both TeamCity and GitLab are on the same server.
It seems that the agent can't connect to gitlab but it does, because when I have to specify the pom.xml path in the build configuration section, I can navigate through the repository folders(http://prntscr.com/i8pyjf).
I have already tried different solutions:
I added both the root ssh and the ssh key of the ser that is running teamcity, both of them seem to work cause the test connection was succesful(http://prntscr.com/i8q0jf);
I added teamcity.git.use.native.ssh true in the buildAgent conf and the error log changed a little (the error is still the same but I get less lines)
I added teamcity.git.use.native.ssh true as a configuration parameter -> http://prntscr.com/i8pnqp (not sure if I did it right)
I tried adding the runAs plugin to force the user agent to use root
TeamCity version -> TeamCity Professional 2017.2.2 (build 50909)
Here's the error log:
[22:03:51] The build is removed from the queue to be prepared for the start
[22:03:51] Collecting changes in 1 VCS root
[22:03:51] [Collecting changes in 1 VCS root] VCS Root details
[22:03:51] [VCS Root details] "git#151.80.136.106:Developer/BanlogBridge.git" {instance id=11, parent internal id=1, parent id=Ban_Git15180136106DeveloperBanlogBridgeGit, description: "git#151.80.136.106:Developer/BanlogBridge.git#master"}
[22:03:51] [Collecting changes in 1 VCS root] Compute revision for 'git#151.80.136.106:Developer/BanlogBridge.git'
[22:03:51] [Compute revision for 'git#151.80.136.106:Developer/BanlogBridge.git'] Upper limit revision: 0d41a12c788798152c7da9327ef63b759f7d5b4c
[22:03:51] [Compute revision for 'git#151.80.136.106:Developer/BanlogBridge.git'] Cannot find modification with revision 0d41a12c788798152c7da9327ef63b759f7d5b4c
[22:03:51] [Compute revision for 'git#151.80.136.106:Developer/BanlogBridge.git'] No modification from VCS root is attached to build configuration, use upper limit revision
[22:03:51] [Compute revision for 'git#151.80.136.106:Developer/BanlogBridge.git'] Computed revision: 0d41a12c788798152c7da9327ef63b759f7d5b4c
[22:03:51] Starting the build on the agent Default Agent
[22:03:52] Clearing temporary directory: /usr/local/teamcity/buildAgent/temp/buildTmp
[22:03:52] Publishing internal artifacts
[22:03:52] [Publishing internal artifacts] Publishing 1 file using [ArtifactsCachePublisher]
[22:03:52] [Publishing internal artifacts] Publishing 1 file using [WebPublisher]
[22:03:52] Using vcs information from agent file: db80d9c7294729d5.xml
[22:03:52] Checkout directory: /usr/local/teamcity/buildAgent/work/db80d9c7294729d5
[22:03:52] Updating sources: auto checkout (on agent)
[22:03:52] [Updating sources] Will use agent side checkout
[22:03:52] [Updating sources] VCS Root: git#151.80.136.106:Developer/BanlogBridge.git
[22:03:52] [VCS Root: git#151.80.136.106:Developer/BanlogBridge.git] revision: 0d41a12c788798152c7da9327ef63b759f7d5b4c
[22:03:52] [VCS Root: git#151.80.136.106:Developer/BanlogBridge.git] Git version: 2.7.4.0
[22:03:52] [VCS Root: git#151.80.136.106:Developer/BanlogBridge.git] Will use native ssh (teamcity.git.use.native.ssh=true)
[22:03:52] [VCS Root: git#151.80.136.106:Developer/BanlogBridge.git] Update checkout directory (/usr/local/teamcity/buildAgent/work/db80d9c7294729d5)
[22:03:52] [Update checkout directory (/usr/local/teamcity/buildAgent/work/db80d9c7294729d5)] /usr/bin/git config core.sparseCheckout true
[22:03:52] [Update checkout directory (/usr/local/teamcity/buildAgent/work/db80d9c7294729d5)] /usr/bin/git show-ref
[22:03:52] [Update checkout directory (/usr/local/teamcity/buildAgent/work/db80d9c7294729d5)] /usr/bin/git show-ref refs/remotes/origin/master
[22:03:52] [Update checkout directory (/usr/local/teamcity/buildAgent/work/db80d9c7294729d5)] Commit '0d41a12c788798152c7da9327ef63b759f7d5b4c' is not found in local clone. Running 'git fetch'...
[22:03:52] [Update checkout directory (/usr/local/teamcity/buildAgent/work/db80d9c7294729d5)] /usr/bin/git fetch --progress origin +refs/heads/master:refs/remotes/origin/master
[22:03:52] [Updating sources] Failed to perform checkout on agent: '/usr/bin/git fetch --progress origin +refs/heads/master:refs/remotes/origin/master' command failed.
exit code: 128
stderr: Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[22:03:52] Publishing internal artifacts
[22:03:52] [Publishing internal artifacts] Publishing 1 file using [ArtifactsCachePublisher]
[22:03:52] [Publishing internal artifacts] Publishing 1 file using [WebPublisher]
[22:03:52] Build failed to start. Artifacts will not be published for this build
[22:03:52] Build finished
Thank you for your help!
It happens because known hosts database on the TeamCity agent machine doesn't contain the fingerprint of the Gitlab server. You can add it by running some command via ssh, e.g. git ls-remote <your repository url>, and agree to add the fingerprint if it's correct.
Actually TeamCity checks out code with known hosts database check disabled. Please check if you have anything related to Gitlab in the .ssh/config on the TeamCity agent machine.

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