How to Build a Simple Jenkins‑Maven‑Git CI/CD Pipeline for Java Apps
Learn step‑by‑step how to set up a basic CI/CD workflow using Jenkins, Maven, and Git on three servers—GitLab for source control, Jenkins for building a Spring Boot JAR, and a test server for deployment—covering prerequisites, environment setup, Jenkins configuration, and post‑deployment scripts.
Overview
This guide demonstrates a minimal CI/CD pipeline that builds a Spring Boot application with Maven, stores the source code in GitLab, and deploys the resulting JAR to a remote test server using Jenkins and the SSH Publisher plugin.
Prerequisites
GitLab server (e.g., gitlab99 at 192.168.40.99)
Jenkins server (e.g., jenkins98 at 192.168.40.98)
Test server (e.g., test97 at 192.168.40.97)
Each host should have sufficient CPU/RAM and the required software installed (GitLab, Jenkins + JDK + Maven + Git, and a JDK on the test host).
GitLab Project Setup
Create a new group in GitLab and then a blank project inside that group.
Generate a personal access token (e.g., glpat-xxxx) with read/write repository permissions.
Copy the HTTPS clone URL that includes the token, for example:
https://gitlab99:glpat-xxxx@gitlab99/your-group/your-project.gitIDEA Project Setup
Create a new Spring Web project in IntelliJ IDEA.
Set the server port to 8088 in src/main/resources/application.properties. server.port=8088 Add a simple controller:
@RestController
@RequestMapping("/")
public class HelloController {
@RequestMapping
public String sayHello() {
return "Hello dev";
}
}Verify the application locally at http://127.0.0.1:8088.
Initialize a Git repository (IDEA → Version Control → Create Git repository).
Add the remote GitLab URL (including the token) as origin and push the initial commit.
Jenkins Configuration
Install the Maven Integration plugin via Manage Jenkins → Manage Plugins → Available.
Create a new Pipeline job (Dashboard → New Item → name → Pipeline).
In the job configuration, set the Git repository URL (the token‑embedded HTTPS URL) and enable “Git pull”.
Specify the branch to build (e.g., master).
Configure Maven under Manage Jenkins → Global Tool Configuration → Maven (e.g., installation path /opt/maven).
Save the job and run it. The build should produce jenkins-study-0.0.1-SNAPSHOT.jar in the target/ directory. Verify with:
java -jar target/jenkins-study-0.0.1-SNAPSHOT.jarIf the JAR lacks a main manifest attribute, edit pom.xml to set <skip>false</skip> for the spring-boot-maven-plugin and rebuild.
SSH Publisher Configuration
Install the SSH Publisher plugin via the plugin manager.
Add the test server under Manage Jenkins → System → SSH remote hosts (host 192.168.40.97, user root, authentication using the GitLab token).
In the Pipeline job, add a Post‑Build step “Send files or execute commands over SSH”. Configure:
Source files: **/jenkin*.jar (matches the built JAR in target/).
Remote directory: ~/ (root’s home directory).
Remove prefix: /target to avoid creating an extra target folder on the remote host.
Add an Exec command to start the application on the test server:
nohup java -jar /root/jenkins-study/jenkins*.jar >> /root/jenkins-study/log.out 2>&1 &Explanation: nohup runs the process detached from the terminal. >> log.out appends stdout to a log file. 2>&1 redirects stderr to the same log. & runs the process in the background.
Pre‑Build Cleanup Script
Create a script /root/clean.sh on the test server to remove stale JARs and kill any running instance before a new deployment:
#!/bin/bash
appname=$1
if [ -z "$appname" ]; then
echo "Application name is required"
exit 1
fi
# Remove old JARs
rm -rf $appname/*.jar
# Find PID of running java -jar process
pid=$(ps -ef | grep "$appname" | grep 'java -jar' | awk '{print $2}')
if [ -z "$pid" ]; then
echo "$appname is not running"
else
kill -9 $pid
echo "$appname stopped"
fiMake it executable ( chmod +x clean.sh) and add it as a Pre‑Build step in Jenkins (e.g., Execute shell with /root/clean.sh jenkins-study).
Testing the Pipeline
After configuring the pre‑ and post‑steps, trigger a build. Verify on the test server:
The JAR is copied to /root/jenkins-study/.
The cleanup script stops any previous instance and removes old JAR files.
The new instance starts; jps shows a new PID for jenkins-study-0.0.1-SNAPSHOT.jar.
# Before rebuild
[root@test97 ~]# jps
20807 jenkins-study-0.0.1-SNAPSHOT.jar
21445 Jps
# After rebuild
[root@test97 jenkins-study]# jps
21562 jenkins-study-0.0.1-SNAPSHOT.jar
21610 JpsAccess the application at http://192.168.40.97:8088 to confirm it returns the expected response.
Additional Notes
If the remote host cannot find java, ensure JAVA_HOME and PATH are exported in /etc/bashrc (e.g., export JAVA_HOME=/usr/local/jdk/jdk-17 and export PATH=$PATH:$JAVA_HOME/bin).
The cleanup script uses grep 'java -jar' to avoid matching the grep process itself.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
