Building and Deploying a Web Application to Tomcat Server Using Jenkins ๐
In today's tutorial, we'll walk through the process of building and deploying a web application to a Tomcat server using Jenkins. ๐ฅ๏ธ Let's dive into each step in detail:
1. What is Tomcat Server? ๐
Apache Tomcat is an open-source web server and servlet container that executes Java Servlets and renders JavaServer Pages (JSP). It's widely used to run Java-based web applications and provides a platform for deploying Java web apps.
Tomcat supports technologies like Java Servlets, JSP, and WebSockets, making it perfect for running Java-based web applications. ๐
2. Installing Jenkins Using a WAR File ๐ฆ
Jenkins is an open-source automation tool used for continuous integration and continuous delivery (CI/CD). We can install Jenkins using a WAR file, which is a simple and straightforward way to get started.
Here are the steps to install Jenkins using the WAR file:
Download Jenkins WAR File:
Go to the Jenkins official website.
Download the Jenkins WAR file (itโs a self-contained file).
Run Jenkins Using the Command Line:
Open a Command Prompt (Windows) or Terminal (macOS/Linux).
Navigate to the folder where the Jenkins WAR file is downloaded.
Execute the following command to start Jenkins:
wget <jenkins file address> sudo apt install maven java -jar jenkins.war --httpPort=8082
Jenkins will start running on
http://localhost:8082
.
3. Installing Tomcat Server Using Command Line ๐ฅ๏ธ
Now that Jenkins is up and running, it's time to install Apache Tomcat. We will use command-line instructions for the installation.
On Linux or macOS:
Download Tomcat:
Go to the Apache Tomcat download page.
Download the latest version of Tomcat 9.x (choose the tar.gz file).
Install Tomcat:
Extract the tar file to your desired directory:
cd /opt sudo wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz sudo tar -xvf apache-tomcat-9.0.65.tar.gz
Go to the location and comment the line, for example:
cd /opt/apache-tomcat-9.0.65/conf sudo vi tomcat-users.xml ---add-below-line at the end (2nd-last line)---- <user username="admin" password="admin1234" roles="admin-gui, manager-gui, manager-script"/> sudo ln -s /opt/apache-tomcat-9.0.65/bin/startup.sh /usr/bin/startTomcat sudo ln -s /opt/apache-tomcat-9.0.65/bin/shutdown.sh /usr/bin/stopTomcat sudo vi /opt/apache-tomcat-9.0.65/webapps/manager/META-INF/context.xml comment: <!-- Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> --> sudo vi /opt/apache-tomcat-9.0.65/webapps/host-manager/META-INF/context.xml comment: <!-- Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
Start Tomcat:
sudo startTomacat
Access Tomcat:
- Open a browser and visit
http://localhost:8080
to see if Tomcat is running.
- Open a browser and visit
4. Accessing Jenkins UI and Creating a Job Pipeline ๐ง
Once Jenkins and Tomcat are up and running, it's time to integrate them. Weโll now set up a Jenkins job to automate the deployment of our web application to the Tomcat server.
Access Jenkins UI
Open Jenkins:
Visit
http://localhost:8082
in your browser.If itโs your first time, Jenkins will ask for a security unlock key (found in the terminal output after you ran
jenkins.war
).
Login to Jenkins:
- Once logged in, you'll land on the Jenkins dashboard, where you can start creating jobs.
Create a Job Pipeline to Deploy to Tomcat
Create a New Job:
On the Jenkins dashboard, click on New Item.
Choose Pipeline and give it a name (e.g., "Tomcat-Deployment").
Click OK.
Configure the Job Pipeline:
In the job configuration, scroll to the Pipeline section.
In the Pipeline script, define the stages of the pipeline using Jenkinsfile syntax. Hereโs an example of a pipeline that will build and deploy your app to Tomcat:
pipeline { agent any stages { stage('Git checkout') { steps { git branch:'main' ,url:'Ank911007/mavaen-tomcat-sample.git' } } stage('Compile') { steps { sh 'mvn compile' } } stage('Test') { steps { sh 'mvn test' } } stage('Package') { steps { sh 'mvn package' } } stage('Deploy') { steps { deploy adapters: [tomact9(credentialsId:<tomcat-cred added in jenkin> ,path:'',url:<http://localhost:8080>)],contextPath:'hello-world',onFailure:false, war:'target/*.war' } } } }
This script assumes you have a WAR file ready and it will be transferred to the Tomcat server for deployment.
Save the Job:
- Click Save to create the job.
Run the Pipeline:
On the job page, click Build Now to trigger the pipeline.
Jenkins will build and deploy the web application to Tomcat.
Conclusion ๐
Congratulations! Youโve successfully created a Jenkins pipeline that builds and deploys a web application to a Tomcat server. This process streamlines your deployment workflow and ensures consistency in your builds and deployments.
Happy coding, and may your deployments always be smooth! ๐
Subscribe to my newsletter
Read articles from Ankit Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by