Build a Simple Jenkins CI/CD Pipeline with Maven and Git
This guide walks you through setting up a basic CI/CD workflow using Jenkins, Maven, and Git on three servers—GitLab, Jenkins, and a test machine—covering prerequisites, project creation, pipeline configuration, Maven builds, SSH publishing, and cleanup scripts to achieve automated deployment.
1. Prerequisites
Three servers are required:
GitLab server: IP 192.168.40.99, 8C8G, software: gitlab
Jenkins server: IP 192.168.40.98, 4C4G, software: jenkins, jdk, maven, git
Test server: IP 192.168.40.97, 2C2G, software: jdk
Install the listed software on each machine.
2. Environment Preparation
2.1 Create a blank project in GitLab
Navigate to Your work / Groups / New group and create a group.
Inside the group click Create group.
Create a new project by clicking New project and choose a blank project.
Enter a project name and click Create project.
Generate a personal access token (e.g., glpat-xxxx) in Preferences → Access Tokens and copy it.
2.2 Create a new project in IDEA
Open IDEA → Settings → Git and set the path to the local Git executable.
Create a Git repository for the project.
3. Jenkins CI/CD
3.1 Install Maven plugin
In Jenkins go to
Dashboard → Manage Jenkins → Plugin Management → Available Plugins, search for "maven" and install it.
3.2 Create a Jenkins job
Click Dashboard → All → New Item, give the job a name (e.g., first) and select a Freestyle project.
3.3 Configure Git
In the job configuration set the Git repository URL (using the token) and add the credentials.
3.4 Configure branch
Specify the branch to build (e.g., */main).
3.5 Configure Maven
Set Maven installation path at http://192.168.40.98:8080/configureTools/. Keep the Root POM as jenkins-study/pom.xml.
3.6 Test Jenkins build
Save the configuration and click Build Now. The console output shows the JAR file generated in /root/.jenkins/workspace/first/target/. Run it with: java -jar jenkins-study-0.0.1-SNAPSHOT.jar If you see the error "no main manifest attribute", set skip to false in the spring-boot-maven-plugin configuration.
3.7 Install SSH Publisher plugin
Install the "SSH Publisher" plugin via Manage Jenkins → Plugin Management → Available Plugins.
3.8 Configure test server in Jenkins
Go to Manage Jenkins → System and add a new SSH server with the test machine’s IP, username, and private key. Test the connection.
3.9 Configure Post‑Build Steps
In Post Steps → Send files or execute commands over SSH set:
Source files : **/jenkin*.jar (any JAR in the workspace)
Remote directory : ~ (or /root for root user)
Optionally enable "Remove prefix" to avoid creating a /target sub‑directory on the remote host.
3.10 Configure Pre‑Build Steps (cleanup script)
Create a clean.sh script on the test server:
#!/bin/bash
appname=$1
if [ -z $appname ]; then
echo "Application name cannot be empty!"
exit -1
else
echo "Application name is $1"
fi
# Remove old JARs
rm -rf $appname/*.jar
# Find running PID
pid=$(ps -ef | grep $1 | grep 'java -jar' | awk '{printf $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) and add it to Jenkins Pre Steps as sh clean.sh jenkins-study.
3.11 Run the pipeline
Build the job. The pre‑step script stops any previous instance and removes old JARs, then the Maven build creates a new JAR, and the post‑step publishes it to the test server. Verify the process with jps and access the application at http://192.168.40.97:8088.
4. Summary
The tutorial demonstrates a complete Jenkins CI/CD pipeline for a simple Spring Boot application, including source control, build, artifact publishing, and cleanup. Consider further improvements such as parallel stages, automated testing, and containerization.
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.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.
