Operations 12 min read

How to Build a Simple Jenkins‑Maven‑Git CI/CD Pipeline for Java Apps

This step‑by‑step guide shows how to set up a three‑server environment with GitLab, Jenkins, and a test machine, create a GitLab project and token, configure Maven and Jenkins, and automate building, deploying, and running a Spring Boot JAR using SSH and cleanup scripts.

Architect
Architect
Architect
How to Build a Simple Jenkins‑Maven‑Git CI/CD Pipeline for Java Apps

Prerequisites

Three servers are required: a GitLab server (192.168.40.99, 8C8G, gitlab), a Jenkins server (192.168.40.98, 4C4G, jenkins/jdk/maven/git) and a test server (192.168.40.97, 2C2G, jdk).

1. GitLab Project Setup

Create a group in GitLab → Your work / Groups / New group.

Create a new blank project → New project → enter project name and click Create project.

Generate a personal access token (Preferences → Access Tokens) and copy the token value.

2. IDE Project Setup

In IntelliJ IDEA create a new Spring Web project.

Change the server port in application.properties to server.port=8088.

Add a simple @RestController that returns Hello dev.

@RestController
@RequestMapping("/")
public class HelloController {
    @RequestMapping
    public String sayHello() {
        return "Hello dev";
    }
}

3. Git Configuration in IDEA

Open Settings > Git and set the path to the local Git executable.

Initialize a Git repository, add files and commit.

Add the remote GitLab URL and push the code.

4. Jenkins CI/CD Configuration

Install the Maven plugin (Dashboard → Manage Jenkins → Plugin Manager → Available → search "maven").

Create a new Jenkins job (Dashboard → New Item → give it a name, e.g., first).

Configure the job:

Source Code Management: Git → set repository URL and credentials (use the GitLab token).

Build Triggers: enable as needed.

Build Environment: add Maven build step with goal clean package.

Configure Maven tool location (Dashboard → Manage Jenkins → Global Tool Configuration → Maven → set installation path).

5. Post‑Build Actions – Deploy JAR

Install the SSH Publisher plugin.

Add the test server under Manage Jenkins > Configure System > SSH remote hosts (host, credentials, remote directory).

In the job configuration, add a Post-build ActionSend files or execute commands over SSH.

Source files: **/target/*.jar.

Remove prefix: /target (so the JAR is placed directly in the remote directory).

Exec command:

nohup java -jar /root/jenkins-study/jenkins*.jar >> /root/jenkins-study/log.out 2>&1 &

.

6. Pre‑Build Cleanup Script

Create clean.sh on the test server ( /root) with the following content:

#!/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/${appname}*.jar
# Find running PID
pid=$(ps -ef | grep $1 | grep 'java -jar' | awk '{print $2}')
if [ -z $pid ]; then
    echo "$appname is not started"
else
    kill -9 $pid
    echo "$appname was stopped"
fi

Make it executable ( chmod 777 clean.sh) and add it as a Pre Steps command in the Jenkins job (e.g., sh ./clean.sh jenkins-study).

7. Verify Deployment

After a build, the JAR is transferred to /root on the test server.

Run jps to see the new process PID.

Access the application in a browser at http://192.168.40.97:8088 to confirm it returns the expected response.

Conclusion

The guide demonstrates a complete Jenkins CI/CD pipeline that pulls code from GitLab, builds a Spring Boot JAR with Maven, cleans up previous deployments, and deploys the new JAR to a test server using SSH, providing a foundation for further automation and scaling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaci/cdmavenJenkins
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

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.