How to write a Jenkinsfile to build a Maven project

Prerequisites

References

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

Fork this project hello-world and update the required feilds 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
  tools {
    maven 'maven-3.6.3' 
  }
  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

Previous Topic

How to write a Jenkinsfile

Next Topic

[Part-2] Jenkinsfile to build and deploy to Tomcat