Operations 12 min read

How to Build a Simple Jenkins‑Maven‑Git CI/CD Pipeline from Scratch

This step‑by‑step guide shows how to set up a three‑server environment with GitLab, Jenkins, and a test machine, configure Git repositories, create a Spring Boot project in IntelliJ, install Maven and SSH Publisher plugins in Jenkins, and automate building, packaging, and deploying a JAR file via SSH while handling cleanup and port conflicts.

Java Web Project
Java Web Project
Java Web Project
How to Build a Simple Jenkins‑Maven‑Git CI/CD Pipeline from Scratch

1. Prerequisites

Three servers are required: one running GitLab (192.168.40.99, 8C8G), one running Jenkins (192.168.40.98, 4C4G with JDK, Maven, Git installed), and a test server (192.168.40.97, 2C2G with JDK only). The GitLab installation link is provided in the original article.

2. Environment Setup

2.1 Create a GitLab group

Navigate to Your work / Groups / New group and click Create group.

2.2 Create a new project

Click New project, then select Create blank project, enter a project name, and click Create project.

2.3 Generate a personal access token

Open the user avatar menu, go to Preferences or Settings, select Access Tokens under User Settings, and copy the generated token (e.g., glpat‑qx7VU3J219j1D4_92uqJ).

2.4 Create a new Spring Boot project in IntelliJ IDEA

Choose Spring Web as the starter.

Change the server port in src/main/resources/application.properties to server.port=8088.

Add a simple controller:

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

Test locally by opening a browser at 127.0.0.1:8088.

Configure Git in IDEA: Settings → Git → set the path to the local Git executable.

Create a Git repository for the project (Version Control → Create Git repository).

Add the remote GitLab URL (Manage Remotes → + → Define Remote) and push the code.

Merge the code on GitLab via a merge request.

3. Jenkins CI/CD Configuration

3.1 Install Maven plugin

Navigate to

Dashboard → Manage Jenkins → Plugin Manager → Available

, search for "Maven", and install it.

3.2 Create a Jenkins job

From the dashboard, select All → New Item, give the job a name (e.g., first), and choose Freestyle project.

3.3 Configure Git source

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

3.4 Configure branch

Select the appropriate branch to build.

3.5 Configure Maven

Set the Maven installation path under Manage Jenkins → Global Tool Configuration → Maven (e.g., /opt/maven). The root POM remains jenkins‑study and does not need changes.

3.6 Test Jenkins build

Run the build and verify that Jenkins compiles the project, producing a JAR in /root/.jenkins/workspace/first/target. Use the console output to confirm the JAR was created.

3.7 Install SSH Publisher plugin

Install the plugin via

Dashboard → Manage Jenkins → Plugin Manager → Available → SSH Publisher

.

3.8 Configure test server in Jenkins

Navigate to Dashboard → Manage Jenkins → System, add a new SSH server with the test machine’s IP, credentials, and test the connection.

3.9 Configure Post‑Build Steps (Send files or execute commands over SSH)

Source files: **/jenkin*.jar (wildcard matches the built JAR).

Remote directory: ~ (home directory of the SSH user, e.g., /root for root).

Remove prefix: /target to place the JAR directly in the home directory.

3.10 Execute the JAR on the test server

Use the following command in the Post‑Build step:

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

Explanation of the command: nohup detaches the process from the terminal. >> appends stdout to log.out. 2>&1 redirects stderr to the same log file. & runs the process in the background.

3.11 Add a Pre‑Build cleanup script

Create clean.sh on the test server ( /root) with the following content (make it executable with chmod 777 clean.sh):

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

Configure this script as a Pre‑Build step in Jenkins (Pre Steps → Execute shell → ./clean.sh jenkins-study).

3.12 Verify deployment

After rebuilding, check the test server with jps to see a new PID for jenkins‑study‑0.0.1‑SNAPSHOT.jar and confirm the application is reachable at 192.168.40.97:8088.

4. Conclusion

The article demonstrates a complete Jenkins‑Maven‑Git CI/CD pipeline: source code push, automated Maven build, JAR packaging, SSH transfer, and remote execution with cleanup. Readers are encouraged to consider further improvements such as handling multiple environments, adding automated tests, or using Docker containers for reproducible builds.

CI/CDAutomationGitmavenLinuxshellJenkins
Java Web Project
Written by

Java Web Project

Focused on Java backend technologies, trending internet tech, and the latest industry developments. The platform serves over 200,000 Java developers, inviting you to learn and exchange ideas together. Check the menu for Java learning resources.

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.