Deploy Application To Tomcat Using Jenkins :

Deploy Application To Tomcat Using Jenkins :

Step1 : Install Tomcat

sudo su
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

Step 2 : Navigate to Tomcat's configuration directory

cd /opt/apache-tomcat-9.0.65/conf

Step 3 : Edit the tomcat-users.xml file and add the following line at the end (2nd-last line)

<user username="admin" password="admin1234" roles="admin-gui, manager-gui, manager-script"/>

Step 4 : Create symbolic links for Tomcat startup and shutdown scripts

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

Step 5 :Edit the context.xml files for the manager and host-manager web applications to comment out a section

sudo vi /opt/apache-tomcat-9.0.65/webapps/manager/META-INF/context.xml

Step 6 : Comment out the following section

<!-- Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

Step 7 :Similarly, edit the context.xml file for the host-manager :

sudo vi /opt/apache-tomcat-9.0.65/webapps/host-manager/META-INF/context.xml

Step 8 : Comment out the same section as above

<!-- Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" -->

Step 8 : To stop and start Tomcat, use the following commands

sudo stopTomcat
sudo startTomcat

Remember to replace the username and password in the tomcat-users.xml file with your preferred values for added security.

Step 9: Install Jenkins & Configure Pipeline

pipeline {
    agent any

    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'main', url: 'https://github.com/your_repo.git'
            }
        }

        stage('Compile') {
            steps {
                sh "mvn compile"
            }
        }

        stage('Tests') {
            steps {
                sh "mvn test"
            }
        }

        stage('Package') {
            steps {
                sh "mvn clean package"
            }
        }

        stage('Deploy') {
            steps {
                deploy adapters: [
                    tomcat9(
                        credentialsId: 'tomcat-credential',
                        path: '',
                        url: 'http://13.233.144.57:8080/'
                    )
                ],
                contextPath: 'Planview',
                onFailure: false,
                war: 'target/*.war'
            }
        }
    }
}

This script defines a Jenkins Declarative Pipeline with several stages. Each stage corresponds to a specific step in your build and deployment process. The script checks out code from a Git repository, compiles it, runs tests, packages the application, and finally deploys it to an Apache Tomcat server.

Make sure to have the necessary Jenkins plugins installed for Git integration, Maven, and the Tomcat deployment adapter (tomcat9).

Remember to adjust any configuration values such as Git repository URL, Tomcat server URL, context path, and credentials as needed for your setup.