Building and Deploying a Web Application to Tomcat Server Using Jenkins ๐Ÿš€

Ankit RajAnkit Raj
4 min read

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:

  1. Download Jenkins WAR File:

    • Go to the Jenkins official website.

    • Download the Jenkins WAR file (itโ€™s a self-contained file).

  2. 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:

  1. Download Tomcat:

  2. 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" /> -->
      
  3. Start Tomcat:

     sudo startTomacat
    
  4. Access Tomcat:

    • Open a browser and visit http://localhost:8080 to see if Tomcat is running.

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

  1. 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).

  2. 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

  1. 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.

  2. 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.

  3. Save the Job:

    • Click Save to create the job.
  4. 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! ๐Ÿš€

GITHUB

0
Subscribe to my newsletter

Read articles from Ankit Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ankit Raj
Ankit Raj