Skip to content

Build Maven Project

Prerequisites

  • maven plugin should be installed in Jenkins. By default maven plugin will be installed in Jenkins

  • Configure specific version of maven in Jenkins Global Tool Configuration

I have a sample hello-world maven project in github hello-world

Fork this project hello-world and update the required fields in the Jenkinsfile 02-Jenkinsfile-maven-build

Maven is a build tool used to compile, test and package the application developed using Java programming language.

Jenkinsfile

pipeline {
  agent any
  stages {
    stage ('Build') {
      steps {
        sh 'mvn clean package'
      }
    }
  }
}

In the tools block we have used maven definition to refer the maven installation maven-3.6.3 configured in Jenkins Global tool configuration.

We have created one stage called Build, here we are executing the mvn clean package command to compile and package the java application.

It will compile the java code and generate the package in targets folder.

jenkins

References

Important Tips

Tip

Tool Auto-Installation: The tools { maven '...' } block is powerful. It ensures that the specified version of Maven is installed and available in the path before any steps run, so you don't have to manually configuration paths on agents.

Note

Workspace Cleaning: Ideally, you should often start with a clean workspace. The mvn clean command handles this at the build tool level, but Jenkins also has deleteDir() (usually in post { always { ... } }) to clean the agent's workspace.

Quick Quiz

#

Which block in a declarative pipeline is used to auto-install and configure tools like Maven?

#

What Maven command is commonly used to clean the target directory and package the application?

#

In a declarative pipeline, inside which block are shell commands (like sh) executed?

📬 DevopsPilot Weekly — Learn DevOps, Cloud & Gen AI the simple way.
👉 Subscribe here