Operations 5 min read

Automate Java Project Build and Deployment with Jenkins Pipeline and Ansible

This guide walks through creating a Jenkins pipeline that pulls Java code, builds a JAR, transfers it to a remote server via Ansible, and starts the application using SSH, illustrating a complete CI/CD workflow for backend services.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Automate Java Project Build and Deployment with Jenkins Pipeline and Ansible

Overview

The purpose is to use Jenkins to select a branch, build a JAR package, send it to a backend server, and start the Java project via a server script.

Step 1: Choose Build Retention

image-20220520220952169
image-20220520220952169

Step 2: Parameterized Build

image-20220520221113350
image-20220520221113350

Step 3: Define Pipeline

image-20220520221204093
image-20220520221204093

The pipeline syntax follows the declarative model. Example snippet:

pipeline {
    agent any
    stages {
        stage('拉取代码') {
            steps {
                // Add content
            }
        }
        // Additional stages omitted for brevity
    }
}

Pipeline Stages

Pull Code

image-20220520223344764
image-20220520223344764

Uses

checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: '100b584e-7f67-466f-8b93-9b9038e117a0', url: 'https://git.ibimfish.cn/Suzhou/pond.git']]])

to fetch the specified tag.

Compile and Package

image-20220520224454228
image-20220520224454228

Runs

sh 'mvn clean package install -Dmaven.test.skip=true -DarchetypeCatalog=local'

.

Copy JAR to Remote Server

image-20220520224626184
image-20220520224626184

Uses Ansible:

sh 'ansible tomcat -m copy -a "src=/root/.jenkins/workspace/pipeline-pond/pond/target/pond.jar dest=/root/upload/"'

.

Start Application Script

image-20220520235827109
image-20220520235827109

Executes via sshPublisher with commands:

sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.2.204', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /pipeline-pond
source /etc/profile
bash up.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '', usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

Final Pipeline Script

pipeline {
    agent any
    stages {
        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: '100b584e-7f67-466f-8b93-9b9038e117a0', url: 'https://git.ibimfish.cn/Suzhou/pond.git']]])
            }
        }
        // stage('切换分支') { ... } // Skipped because tag is used
        stage('编译打包') {
            steps {
                sh 'mvn clean package install -Dmaven.test.skip=true -DarchetypeCatalog=local'
            }
        }
        stage('拷贝jar包') {
            steps {
                sh 'ansible tomcat -m copy -a "src=/root/.jenkins/workspace/pipeline-pond/pond/target/pond.jar dest=/root/upload/"'
            }
        }
        stage('启动脚本') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.2.204', transfers: [sshTransfer(execCommand: '''cd /pipeline-pond
source /etc/profile
bash up.sh''')])])
            }
        }
    }
}

Result

Successful execution screenshots show the pipeline completing and the application starting on the remote server.

image-20220521001016649
image-20220521001016649
JavaCI/CDdeploymentPipelineJenkinsAnsibleSSH
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.