Jenkins Pipeline to deploy Java application | Git, Maven, Nexus & Tomcat

Jenkins Pipeline to deploy Java application | Git, Maven, Nexus & Tomcat

Hey techies, in Prev blog we learned how to deploy applications using Jenkins free style job. In this blog i am going to write a pipeline to deploy the java-based application.
But i request you to complete my previous blogs to understand this concept better.

https://devops-cicd.hashnode.dev/deploy-a-java-application-using-jenkins
https://devops-cicd.hashnode.dev/jenkins-integrated-with-nexus

my pipeline contains 4 stages [code, build & Unit Test, Artifact, deploy]

Tools we used for each stage:

  • code: GIT

  • Build: Maven

  • Unit Test: Maven

  • Artifact: Nexus

  • Deploy: Tomcat

STEP BY STEP PROCESS TO DEPLOY THE APPLICATION:

STEP-1: Launch 3 Instances

  • Jenkins

  • Nexus

  • Tomcat

STEP-2: refer the below scripts to install Jenkins, Tomcat and Nexus on respective servers.

Jenkins Script:

#STEP-1: INSTALLING GIT
yum install git -y

#STEP-2: GETTING THE REPO (jenkins.io --> download -- > redhat)
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

#STEP-3: DOWNLOAD JAVA17 AND JENKINS
yum install java-17-amazon-corretto -y
yum install jenkins -y

#STEP-4: RESTARTING JENKINS (when we download service it will on stopped state)
systemctl start jenkins.service
systemctl status jenkins.service

Tomcat Script:

yum install java-17-amazon-corretto -y
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.98/bin/apache-tomcat-9.0.98.tar.gz
tar -zxvf apache-tomcat-9.0.98.tar.gz
sed -i '56  a\<role rolename="manager-gui"/>' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '57  a\<role rolename="manager-script"/>' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '58  a\<user username="tomcat" password="admin@123" roles="manager-gui, manager-script"/>' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '59  a\</tomcat-users>' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '56d' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '21d' apache-tomcat-9.0.98/webapps/manager/META-INF/context.xml
sed -i '22d'  apache-tomcat-9.0.98/webapps/manager/META-INF/context.xml
sh apache-tomcat-9.0.98/bin/startup.sh

Nexus Script:

yum install java-17-amazon-corretto -y
mkdir /app
cd /app
wget -O nexus.tar.gz https://download.sonatype.com/nexus/3/latest-unix.tar.gz
tar -zxvf nexus.tar.gz
mv nexus-* nexus
useradd nexus
chown -R nexus:nexus *
vim /app/nexus/bin/nexus.rc
sed -i '1s/.*/run_as_user="nexus"/' /app/nexus/bin/nexus.rc
./nexus/bin/nexus start
./nexus/bin/nexus status

STEP-3: Install the following plugins on Jenkins dashborad

  • Nexus Artifact Uploader

  • Deploy to Container

STEP-4: Create a Pipeline job and write the below Jenkinsfile

pipeline {
    agent any
    tools {
        maven "mymaven"
    }

    stages {
        stage('Code') {
            steps {
                git 'https://github.com/devops0014/one.git'
            }
        }
        stage ("Build") {
            steps {
                sh 'mvn clean package'
            }
        }
        stage ("Artifact") {
            steps {
                nexusArtifactUploader artifacts: [[artifactId: 'myweb', classifier: '', file: 'target/myweb-8.6.9.war', type: 'war']], credentialsId: 'nexus', groupId: 'in.javahome', nexusUrl: '54.227.157.176:8081', nexusVersion: 'nexus3', protocol: 'http', repository: 'myrepo', version: '8.6.9'
            }
        }
        stage ("Deploy") {
            steps {
                deploy adapters: [tomcat9(credentialsId: 'tomcat', path: '', url: 'http://54.156.87.218:8080')], contextPath: 'myapp', war: 'target/*.war'
            }
        }
    }
}

NOTE: use pipeline syntax for getting the scrips for each stage.

If you love stories that inspire learning, growth, and productivity, consider subscribing for more! If this article added value to your journey, your support would mean the world to me — only if it’s within your means. Let’s stay connected on LinkedIn too. Thank you for reading 💕