Your Jenkinsfile has a simple build step, and the job fails immediately:
+ mvn clean package
/var/lib/jenkins/workspace/my-app@tmp/durable-1a2b3c/script.sh: 1: mvn: not found
On Windows agents the same problem shows as 'mvn' is not recognized as an internal or external command. Maven works when you log in and type mvn -v, so what's different?
What the error means
Jenkins runs every step as the Jenkins service user, on whichever agent the stage runs, with that user's environment. If Maven was installed only for your account, unpacked in your home directory, or on a different agent, the mvn binary isn't on Jenkins' PATH.
Confirm it by printing the environment from the pipeline:
sh 'whoami; hostname; echo $PATH; which mvn || true'
How to fix it, step by step
1. Use Jenkins Tools plus a tools block (recommended)
Go to Manage Jenkins → Tools → Maven installations, add one named Maven-3.9 and tick Install automatically. Do the same for a JDK. Then reference both in the pipeline:
pipeline {
agent any
tools {
maven 'Maven-3.9'
jdk 'JDK-17'
}
stages {
stage('Build') {
steps {
sh 'mvn -v'
sh 'mvn -B clean package'
}
}
}
}
Jenkins downloads Maven on each agent the first time and puts it on the PATH for that build. The names must match the Tools configuration exactly.
2. Run the build in a Maven Docker image
If your agents have Docker and the Docker Pipeline plugin, use the official image so every build gets the same Maven and JDK:
pipeline {
agent {
docker {
image 'maven:3.9-eclipse-temurin-17'
args '-v $HOME/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps { sh 'mvn -B clean package' }
}
}
}
The .m2 volume caches dependencies between builds so they aren't downloaded every time.
3. Use the Maven Wrapper in the project
The Maven Wrapper (mvnw) downloads the right Maven version itself, so the agent only needs a JDK:
# once, on your machine, then commit the files
mvn wrapper:wrapper
# in the Jenkinsfile
sh 'chmod +x mvnw && ./mvnw -B clean package'
4. Add Maven to the PATH yourself
If Maven is already installed on the agent (for example /opt/maven), add it to the pipeline environment:
environment {
PATH = "/opt/maven/bin:${env.PATH}"
}
This works, but it ties the job to how each agent was set up, so it's the least portable option.
Check the agent label too
If the job runs on several agents, Maven may exist on one and not another. Pin builds to prepared agents with a label, for example agent { label 'maven' }.
How to prevent it
- Manage build tools centrally (Jenkins Tools or Docker images), never by hand on agents.
- Keep the Jenkinsfile in the repository so the build definition is versioned with the code.
- Use
mvn -B(batch mode) in CI for cleaner logs, and cache.m2. - Add a quick
mvn -vstep so tool problems fail fast with a clear message.
Jenkins, Maven, Nexus and SonarQube pipelines are core labs in our DevOps training, and running builds in containers is covered in Docker & Kubernetes training.
Frequently asked questions
Why does mvn work in my terminal but not in Jenkins?
Your terminal uses your user's PATH. Jenkins runs as a different user (usually jenkins), often on a different agent machine, with a different PATH where Maven isn't installed.
What is the best way to provide Maven in Jenkins?
For most teams, either the Jenkins Tools configuration with a tools block, or running builds in a Maven Docker image. Both give the same Maven version on every agent.
How do I fix 'JAVA_HOME is not defined correctly' in Jenkins?
Configure a JDK under Manage Jenkins → Tools and add it to the tools block (jdk 'JDK-17'), or use a Maven Docker image that already includes the right JDK.
Learn DevOps & CI/CD hands-on with expert trainers
Fixing problems like this one is part of our DevOps & CI/CD training: live online classes for individuals and on-site corporate training for teams, with real projects and labs.
Training available in Hyderabad, Bangalore, Pune, Mumbai, Delhi, Chennai and online across India.