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.
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
Step 2: Parameterized Build
Step 3: Define Pipeline
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
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
Runs
sh 'mvn clean package install -Dmaven.test.skip=true -DarchetypeCatalog=local'.
Copy JAR to Remote Server
Uses Ansible:
sh 'ansible tomcat -m copy -a "src=/root/.jenkins/workspace/pipeline-pond/pond/target/pond.jar dest=/root/upload/"'.
Start Application Script
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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
