jenkins

'mvn: not found' error even after installing maven plugin


I have configured jenkins plugin 'Pipeline Maven Integration Plugin', and has following in my jenkinsfile:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    echo 'Building the project...'
                    sh 'mvn clean install'
                }
            }
        }

        stage('Test') {
            steps {
                script {
                    echo 'Running tests...'
                    sh 'mvn test'
                }
            }
        }

        stage('Deploy') {
            steps {
                script {
                    echo 'Deploy step (for demo purposes)...'
                }
            }
        }
    }
}

But build fails with error:

mvn: not found

Why so?


Solution

  • It looks like the machine Jenkins runs on doesn't have Maven installed. Please install it and that has to fix your issue.

    Instruction is available on the official Maven website here

    Or you can run the next commands

    $ wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
    $ tar -xvf apache-maven-3.9.9-bin.tar.gz
    $ mv apache-maven-3.6.3 /opt/
    

    Set M2_HOME variable

    M2_HOME='/opt/apache-maven-3.6.3'
    PATH="$M2_HOME/bin:$PATH"
    export PATH
    

    And check your maven version

    mvn -version