Operations 12 min read

How to Build a Simple Jenkins CI/CD Pipeline with Maven and Git

This guide walks through setting up a three‑server environment (GitLab, Jenkins, test server), creating a GitLab project, configuring a Java Spring Boot application in IntelliJ, and using Jenkins with Maven, SSH Publisher, and custom pre/post steps to automatically build, package, and deploy a JAR file.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
How to Build a Simple Jenkins CI/CD Pipeline with Maven and Git

Prerequisites

Three servers are required: one with GitLab, one with Jenkins, and a test server. Their IPs and specs are:

gitlab99 – 192.168.40.99 – 8C8G – GitLab

jenkins98 – 192.168.40.98 – 4C4G – Jenkins, JDK, Maven, Git

test97 – 192.168.40.97 – 2C2G – JDK

Installation guides for GitLab (Docker) and Jenkins on CentOS 7 are referenced.

Environment Preparation

1. Create a blank project in GitLab

Create a group via Your work / Groups / New group .

Create a new project inside the group and give it a name.

Generate a personal access token (e.g., glpat‑qx7VU3J219j1D4_92uqJ) from Preferences → Access Tokens .

2. Create a new Spring Boot project in IntelliJ IDEA

Select Spring Web module.

Change the server port 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";
    }
}

Test locally by browsing http://127.0.0.1:8088.

Configure Git in IDEA (Settings → Git → path to Git executable).

Initialize a Git repository (Version Control → Create Git repository).

Add the remote GitLab URL (Git → Manage Remotes) and provide the token.

Commit locally and push to the remote repository.

On GitLab, create a merge request and merge the code.

Jenkins CI/CD Configuration

1. Install Maven plugin

Navigate to Dashboard → Manage Jenkins → Plugin Manager → Available , search for "Maven", and install.

2. Create a Jenkins job

From the dashboard select All → New Item and create a Freestyle project (named "first").

3. Configure source code

In the job configuration set the Git repository URL and credentials (the token created earlier).

4. Configure Maven build

Specify Maven goals (e.g., clean package) and the path to the Maven installation.

5. Install SSH Publisher plugin

Install it via Manage Jenkins → Plugin Manager → Available .

6. Configure the test server in Jenkins

Go to Manage Jenkins → System , add a new SSH server with host, username, and private key.

7. Set up Post‑Build Steps

Use Send files or execute commands over SSH to copy the built JAR to the test server and run it.

# Example of the remote directory configuration
Source files: /root/.jenkins/workspace/first/target/*.jar
Remote directory: ~ (home directory of the SSH user)

Typical command to start the JAR on the test server:

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

The nohup command detaches the process from the terminal, and the redirections send both stdout and stderr to log.out.

8. Handle common errors

If the JAR lacks a main manifest attribute, set skip to false in the spring-boot-maven-plugin configuration.

If the test server cannot find java, add JAVA_HOME and update PATH in /etc/bashrc.

9. Pre‑Build cleanup (Pre Steps)

Create a clean.sh script on the test server to stop any running instance and delete old JAR files before a new build:

#!/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 '{print $2}')
if [ -z $pid ]; then
    echo "$appname is not started"
else
    kill -9 $pid
    echo "$appname was stopped"
fi

Make the script executable ( chmod 777 clean.sh) and add it as a Pre‑Build step in Jenkins.

10. Verify the pipeline

After a build, check the test server with jps to confirm a new PID for the JAR, and browse http://192.168.40.97:8088 to see the running application.

Conclusion

The article demonstrates a complete Jenkins CI/CD workflow: source code push → Maven build → JAR packaging → SSH transfer → execution on a remote test server, including pre‑ and post‑build cleanup to avoid port conflicts and stale processes.

JavaCI/CDAutomationGitmavenshellJenkins
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.