Build and Deploy Maven/Java Project on Apache Tomcat with Jenkins CI CD Pipeline

Introduction 

Apache Maven is a popular build automation and project management tool primarily used for Java projects. It provides a consistent and efficient way to manage project dependencies, build processes, and project documentation. Maven uses a declarative XML file called pom.xml (Project Object Model) to define the project structure, dependencies, plugins, and other project-related configurations.


In this post, We will show you how to create CI CD pipeline to build, test and deploy the JAVA project to the Apache tomcat server using Jenkins automation server.


Step 1: Installing Maven Plugin In Jenkins

We need o login Jenkins portal and click on Manage Jenkins button To install the Maven plugin in Jenkins, you can follow these steps:



Here we should get the Plugins section click on it.



Now we need to click on Available plugins button left menu options.



In this page we need to search maven and after that we need to install Maven Integration plugin.



Click on Install without restart or you can also choose Download now and install after restart.



After successful installation we should get success message for Maven Integration plugin.


Step 2: Configure Maven in Jenkins

Once installed maven integration plugins we need to configure the maven installation in tools section, Go to the Dashboard > Manage Jenkins > Tools and search the Maven sub section like this.



Here we need to define the name which we can put maven along with version and we need to also select the install automatically maven version from the drop down menu and in future we can also add and update the maven version here as per our need.



Once we put the maven name and version we are goo to click on Apply and Save button.


Step 3: Configure GitHub Creds with Jenkins

By following given link we can Creating GitHub’s personal access token for Jenkin step by step.



Now you should have GitHub account username and token, Now we need to configure in Jenkins’s credentials option like this.


Go to the Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)



Save your username and token from GitHub account in order clone the repository, Here our GitHub Creds ID will be github.



After create the Jenkins creds with GitHub account using token, We should get status like this.



Step 4: Installing SSH Agent Plugin

Now we need to install the SSH Agent plugin in order o copy the war file from Jenkins workspace to tomcat’s webapps path.


Go to the Dashboard > Manage Jenkins > Plugins like before, Search in Available plugins “SSH Agent”



Once you get this, You need to select and click on Install without restart button.



After installation, We should get success message like this.



Create SSH’s creds using Jenkins credential option, SSH Agent compatible with PEM (Private SSH key ) based authentication.


Go to the Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)



and create the SSH creds using PEM file like this, We will this creds to transfer the war file to tomcat server in later Jenkins pipeline.



After creating the creds we should get out put like this.



Step 5: Creating CI CD Pipeline

We are good to create a Jenkins CI CD pipeline with any name like BuildandDeploy for this we need to click on Create a job from Jenkins dashboard.



Here we need to define the job name like BuildandDeploy.



and then we need to select the type, In my case i am going to use the pipeline.



Now go to the end the of page, You should get blank pipeline script editor like this.



Here we need to copy and paste the following pipeline script that having Git clone, Maven build, Maven test and SCP command to copy the war file after the build process to tomcat server.


pipeline { agent any tools { maven 'maven3.9.2' } stages { stage('Checkout') { steps { git credentialsId: 'github', url: 'https://github.com/aftab70/maven.git' } } stage('Build') { steps { sh 'mvn package' } } stage('Test') { steps { sh 'mvn test' } } stage('Deploy') { steps { sshagent(credentials: ['tomcat']) { sh 'scp -oStrictHostKeyChecking=no target/*.war USERNAME@IPADDRESS:TYPE_PATH' } } } } }


We need to replace the GitHub id, SSH agent creds id along with tomcat server IP address and webapps path and all good, We are good to apply and save this job to run it.



After save this job we need to click on Build now button from left side menu after this we should get our job up and running state like this.



Here we can see that our all the stage got completed now.



Open the Apache tomcat server IP with port number and add the project name to deployment, Like in my case I am having profile page like this.



Conclusion

We have done successful created CI CD pipeline to build and deploy Maven project to the tomcat server, Still you are having any issue, So please leave a comment below.




Aftab Ali

My expertise is in ethical hacking, penetration testing, network security, monitoring and more.

Post a Comment

Previous Post Next Post