Maven not found in Jenkins
Asked Answered
S

12

14

I am running my Maven/Spring project in Jenkins (just testing it out, first time) using the shell script option with:

mvn spring-boot:run

I get the build error: /Users/Shared/Jenkins/tmp/jenkins8087926087546049217.sh: line 2: mvn: command not found

How can I fix this? Its a Spring-boot app. It works fine when I run mvn spring-boot:run via command line.

Sollows answered 19/8, 2017 at 23:6 Comment(1)
What is the output of which mvn in a Terminal.app session?Bifarious
P
4

Try this, Navigate to Manage Jenkins and click on Global Tool Configuration, In Maven section click on the 'Add Maven' button and provide the maven installation path in MAVEN_HOME and save configurations.

Maven config in Jenkins

Precontract answered 20/8, 2017 at 0:5 Comment(5)
There is no "Maven section," can you provide some direction?Sollows
@Charo, take a look at the snapshot I have shared in my answer for the reference.Precontract
wow, I don't even have this in my Jenkins config system... Any idea?Sollows
Refer these, tutorialspoint.com/jenkins/jenkins_maven_setup.htm or itcuties.com/tools/… see if it helpsPrecontract
Click on "Global Tool Configuration". The link is your-jenkins-host/configureToolsAlpenstock
B
10

"Manage Jenkins"->"Global Tool Configuration" enter image description here

if still not work, add the following to "Execute shell"

export MAVEN_HOME=/opt/maven
export PATH=$PATH:$MAVEN_HOME/bin
mvn --version
mvn clean package

enter image description here

Birdhouse answered 1/7, 2019 at 10:18 Comment(0)
P
4

Try this, Navigate to Manage Jenkins and click on Global Tool Configuration, In Maven section click on the 'Add Maven' button and provide the maven installation path in MAVEN_HOME and save configurations.

Maven config in Jenkins

Precontract answered 20/8, 2017 at 0:5 Comment(5)
There is no "Maven section," can you provide some direction?Sollows
@Charo, take a look at the snapshot I have shared in my answer for the reference.Precontract
wow, I don't even have this in my Jenkins config system... Any idea?Sollows
Refer these, tutorialspoint.com/jenkins/jenkins_maven_setup.htm or itcuties.com/tools/… see if it helpsPrecontract
Click on "Global Tool Configuration". The link is your-jenkins-host/configureToolsAlpenstock
G
3

I had a similar problem on windows. Maven worked fine from console, but failed when called as cmd command from Jenkins.

The reason is that Jenkins is installed and run as a ‚system user‘ service. This means the process can‘t see the ENV variables and PATH of your user (user level). Setting up maven in PATH and ENV variables on system level makes the trick.

P.S.: @Charo the maven config moved from Configure System to Global Tool Configuratin in newer Jenkins versions

Glee answered 15/9, 2017 at 14:46 Comment(1)
Example here: #45923816Tarrel
T
2
export MAVEN_HOME=/opt/maven
export PATH=$PATH:$MAVEN_HOME/bin
mvn --version
mvn clean package

and then mvn install

Tshombe answered 8/4, 2020 at 2:15 Comment(0)
S
2

I also met the problem and solved it. I think you may be using a "FreeStyle Project" instead of a "Maven Project" when you creating a new project via "New Item" button.

In FreeStyle Project, Jenkins may not read environment variables in Linux server and the Maven installations in "Global Tool Configuration", so I configured environment variables repeatedly in "Configure System". Then it worked.

Swellhead answered 21/9, 2020 at 14:23 Comment(0)
M
1

makesure you maven config is true. you can use cmd and run:

mvn -version

if no error, try

mvn spring-boot:run
Maurene answered 20/8, 2017 at 10:23 Comment(0)
D
1

Maybe this is an stupid answer but I forgot to put maven as tool in the Jenkinsfile.

pipeline {
    agent any
    tools {
        maven 'Maven 3.8.6'
        jdk 'Java 17.0.4.1'
    }
    stages {

    }
}
Derrick answered 20/9, 2022 at 12:47 Comment(0)
P
0

I had faced the similar issue where I have put the Name As maven-3.3.9

And Placed the MAVEN_HOME LIKE : E:\Softwares\apache-maven-3.3.9\bin

Pe answered 14/3, 2018 at 8:17 Comment(0)
V
0

I have faced this issue in my ubuntu 16.04 machine and the actual reason is the maven bin directory path is not set $PATH variable.

How to check?

  1. you can append below lines in execute shell prompt of Jenkins GUI.

    
    echo $M2_HOME
    echo $PATH
    mvn clean install
    

    When you will build again then you will see M2_HOME or maven bin path is not set in $PATH variable.

  2. Or you can log in in to Jenkins user from the root and run following commands.

    
    su - Jenkins
    echo $PATH
    

Solution::


 1. logged in to root user.
 2. then this command cd /etc/profile.d/
 3. vim maven.sh then add below lines
    export M2_HOME=/usr/local/maven
    export PATH=$PATH:$M2_HOME/bin
 4. run this command chmod +x maven.sh
 5. then login to jenkins user (su - jenkins) then see again maven path is set or not via (echo $PATH) command.
 6. If the path is set then restart Jenkins service 
    service jenkins restart
    and re-login in Jenkin GUI (http://localhost:8080/)
 7. Now rebuild your job then you will get success :-)
Villeinage answered 15/5, 2020 at 15:18 Comment(0)
L
0

You can also get the mvn not found error if your agent is a docker image without maven in it

Louis answered 2/12, 2022 at 7:11 Comment(0)
S
0
  1. set Java JDK in jenkins manage
  2. set Maven in jenkins maange

in Command execute set

set path=C:\Program Files\apache-maven-3.8.7\bin

your path from environment in windows

cd C:\Users\X\IdeaProjects\ "nameofprocject"

just copy from Intellij/Eclipse

mvn -Dtest=MainPageTests test -> goal from Maven to run

Sprage answered 22/1, 2023 at 2:27 Comment(0)
C
0

2024 Solution

Install Pipeline Maven Integration Plugin and use Maven in your Jenkinsfile like this:

pipeline {
  agent any
  stages {
    stage("Build") {
      steps {
        git url: 'https://github.com/cyrille-leclerc/multi-module-maven-project'
        withMaven {
          sh "mvn clean verify"
        } // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe reports and FindBugs reports
      }
    }
  }
}

Source: https://plugins.jenkins.io/pipeline-maven

Cowcatcher answered 4/6 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.