How to write a Jenkinsfile to build a Maven project and deploy to Apache Tomcat webserver

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 03-Jenkinsfile-deploy-to-tomcat

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'
      }
    }
    stage ('Deploy') {
      steps {
        script {
          deploy adapters: [tomcat9(credentialsId: 'tomcat_credential', path: '', url: 'http://dayal-test.letspractice.tk:8081')], contextPath: '/pipeline', onFailure: false, war: 'webapp/target/*.war' 
        }
      }
    }
  }
}

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

In the stages block we have created two stages Build and Deploy.

In the Build stage we are executing 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

In the Deploy stage we are using the Deploy to container plugin to deploy the hello-world.war file to tomcat webserver.

Parameters passed to Deploy to container plugin definition.

tomcat

Previous Topic

[Part-1] Jenkinsfile to build a maven project

Next Topic