DEV Community

Cover image for Automated Deployment of Java Applications to Apache Tomcat using Jenkins.
Nikhil Raj A
Nikhil Raj A

Posted on • Originally published at Medium

Automated Deployment of Java Applications to Apache Tomcat using Jenkins.

Introduction

This project aims to automate the deployment of Java applications to an Apache Tomcat server using Jenkins. Instead of manually building and copying .war files, we use a CI/CD pipeline where Jenkins pulls code from a Git repository, builds it using Maven, and deploys it to Tomcat automatically. This streamlines the development process, reduces human error, and enables faster, more reliable application delivery, following modern DevOps practices.

workflow of the project

Why This Project?

Manual deployment of Java applications is time-consuming and error-prone. This project solves that by introducing automation using Jenkins, which ensures that every time code is updated, it is built, tested, and deployed automatically to a Tomcat server. It saves developer time, reduces bugs, and supports faster and more reliable software delivery — all of which are essential in modern DevOps and agile environments.

Prerequsities :

  • Tomcat Server
  • Jenkins Server
  • A java Project

References

Step — 1 : Install Git into your Jenkins server

  1. install git and check the git version .
# Become a root 
sudo su -
# Install git.
yum install git -y
# Check Version of git.
git --version
Enter fullscreen mode Exit fullscreen mode
  1. Open Jenkins in web-browser, click on manage jenkins then select Global Tool Configuration.

captionless image

  1. Then under Global tool configuration , add git in Git Installations

captionless image

  1. In git installations , click Add git then select git.

captionless imagecaptionless image

  1. Enter the Git name and path , later click apply and save

captionless image

  1. Now we have successfully integrated Git with Jenkins . Then click on Global Tool Configuration , under that go to Maven installtions . After giving the details click on apply and save .

captionless image

Now Maven is also successfully integrated with jenkins .

Steps — 2 : Install a Plugin called ‘Deploy to container ‘

  1. Go to manage jenkins > plugins > Available plugins > Deploy to container .

captionless image

  1. Click on the restart jenkins after installations , after the installations the jenkins will restart and you need to enter the username and password .

captionless image

step — 3 : Create a Global Credentials

  1. Go to Credentials > Select system > Select Global Credentials then click on Add Credentials .

captionless image

  1. After clicking on add credentials , Enter the username and password

captionless image

  1. Once your done giviing the details , then you can see the credentails you have created .

captionless image

Step — 4: Create a Jenkins Job

  1. Head to Dashboard > New Item , then provide a name for the jenkins job and select freestyle project as project type .

captionless image

  1. Enter the github repo and also change the branch name from master to main , because there is no master branch .

captionless image

  1. Under Build steps > invoke top level maven targets , then provide maven command ‘ clean package’ . And there is no need to mention mvn as we give in the terminal , it automatically takes as mvn and then runs the command .

captionless image

  1. In Post-build Actions click Add Post-build Actions and Select Deploy war/ear to container.

captionless image

  1. Enter the details as following then click on apply and save.

captionless image

Now Click on Build Now

captionless image

The Build is Successfull .

captionless image

Output of the project

Web application deployed

Why to use Github Webhook ?

Using a GitHub webhook with Jenkins allows you to automatically trigger a build and deploy your Java web application whenever someone pushes code to your GitHub repository.

Step — 5 : Automate Build and Deploy using Github webhook

  1. Go to Github Account > Settings > Webhook , then create a webhook.
  • Payload URL is public ip if jenkins server.
http://<public ip address>:8080/github-webhook/
Enter fullscreen mode Exit fullscreen mode
  • Set content type as application/JSON .
  • Select Event Trigger as just the push event .e

captionless image

  1. Then go to jenkins dashboard > tomcat-jenkins > configure > Triggers, there you need to enable the Github hook trigger for GITscm polling.

captionless image

  1. Whenever there is a commit / changes made in the java web — application , then the webhook will automatically create build . Later click on Commit .

captionless image

  1. After the Changes made when you click on commit , a build will be created in jenkins dashboard automatically where it builds and deploys the web application with (.war ) file.

captionless image

  1. The changes made in github repository will be automatically changed in the web — application without any manual intervention . Webhook trigger whenever there is a commit in that repository .

Final output of web application

Conclusion

This mini project successfully demonstrates a CI/CD pipeline where a Java web application is automatically built and deployed to Apache Tomcat using Jenkins. By integrating GitHub Webhooks, the pipeline is triggered instantly upon every code push, ensuring that the latest changes are continuously tested and deployed without manual intervention.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.