Build a Simple Jenkins‑Maven‑Git CI/CD Pipeline for Java Applications
This step‑by‑step guide shows how to set up a three‑server environment with GitLab, Jenkins, and a test machine, configure Maven and SSH Publisher plugins, create a Spring Boot project, and automate code push, build, deployment, and cleanup using Jenkins pipelines and shell scripts.
Prerequisites
Three servers are required:
gitlab99 – 192.168.40.99 – 8C8G – GitLab installed
jenkins98 – 192.168.40.98 – 4C4G – Jenkins, JDK, Maven, Git installed
test97 – 192.168.40.97 – 2C2G – JDK installed (used as the deployment target)
1. GitLab Project Setup
Create a group and a blank project in GitLab:
Navigate to Your work / Groups / New group → Create group → fill the group name and click Create group.
Inside the group, click New project, select Blank project, enter a project name and click Create project.
Generate a personal access token (e.g., glpat‑qx7VU3J219j1D4_92uqJ) via Preferences → Access Tokens and copy it.
2. IDE (IntelliJ IDEA) Project Creation
Create a new Spring Web project and change the default port: server.port=8088 Add a simple controller:
@RestController
@RequestMapping("/")
public class HelloController {
@RequestMapping
public String sayHello() {
return "Hello dev";
}
}Test locally with http://127.0.0.1:8088.
3. Jenkins CI/CD Configuration
3.1 Install Maven Plugin
In Jenkins go to
Dashboard → Manage Jenkins → Manage Plugins → Available, search for "Maven" and install the plugin.
3.2 Create a Jenkins Job
Navigate to Dashboard → New Item, give the job a name (e.g., first), select Freestyle project, and click OK.
3.3 Configure Source Code Management
Under Source Code Management → Git, add the GitLab repository URL and the access token as credentials.
3.4 Configure Build Step
Add a Maven build step with goals clean package. Ensure the Maven installation path is set under
Dashboard → Manage Jenkins → Global Tool Configuration → Maven(e.g., /opt/maven).
3.5 Post‑Build Actions – SSH Publisher
Install the SSH Publisher plugin (Dashboard → Manage Jenkins → Manage Plugins → Available → search "SSH Publisher"). Then configure:
Source files : **/jenkins‑*.jar (searches the workspace /root/.jenkins/workspace/first/target)
Remote directory : /root (or a custom path on the test server)
Enable Remove prefix with value /target to avoid nesting the jar.
3.6 Pre‑Build Steps – Clean Script
Create a shell script clean.sh on the test server ( /root) to stop the previous Java process and delete old jars:
#!/bin/bash
appname=$1
if [ -z $appname ]; then
echo "Application name required!"
exit -1
else
echo "Application name is $appname"
fi
# Remove old jar files
rm -rf $appname/*.jar
# Find running java process for the app
pid=$(ps -ef | grep $appname | grep 'java -jar' | awk '{print $2}')
if [ -z $pid ]; then
echo "$appname is not started"
else
kill -9 $pid
echo "$appname was stopped"
fiMake it executable: chmod 777 clean.sh.
Configure the Jenkins job to run this script in Pre Steps (e.g., ./clean.sh jenkins-study).
3.7 Execute Remote Command to Run the Jar
In Post Steps → Send files or execute commands over SSH , add an Exec command such as:
nohup java -jar /root/jenkins-study/jenkins*.jar >> /root/jenkins-study/log.out 2>&1 &This runs the jar in the background, redirects output to log.out, and ensures the process survives after the SSH session ends.
4. Testing the Pipeline
After configuring the job, click Build Now . Jenkins will:
Run clean.sh to stop any previous instance and delete old jars.
Pull the latest code from GitLab, build the jar with Maven, and archive it.
Transfer the jar to the test server via SSH.
Execute the nohup command to start the application.
Verify on the test server:
# jps
21562 jenkins-study-0.0.1-SNAPSHOT.jar
21610 JpsAccess the running service in a browser at http://192.168.40.97:8088 and you should see the "Hello dev" response.
5. Common Issues & Fixes
Missing Main Manifest : Ensure spring-boot-maven-plugin is not disabled ( skip=false) in pom.xml.
"java" command not found : Add JAVA_HOME and update PATH in /etc/bashrc on the test server.
Port already in use : The clean script stops the previous Java process before starting a new one, preventing the 8088 port conflict.
6. Summary
This guide demonstrates a complete CI/CD workflow using Jenkins, Maven, and GitLab to automatically build, transfer, and run a Spring Boot application on a remote test server, including pre‑build cleanup and post‑build deployment steps.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
