DEV Community

Cover image for Understanding Jenkins CI/CD Using a Tiny Java Project (A Beginner-Friendly Walkthrough)
Sanjay Ghosh
Sanjay Ghosh

Posted on

Understanding Jenkins CI/CD Using a Tiny Java Project (A Beginner-Friendly Walkthrough)

Understanding Jenkins CI/CD Using a Tiny Java Project

Most Jenkins tutorials immediately jump into Docker, Kubernetes, Maven, cloud deployments, and enterprise-scale architectures.

For someone learning CI/CD for the first time, that can become overwhelming very quickly.

In this article, we'll build a very lightweight but real Jenkins pipeline using a simple Java application. The goal is to understand how Jenkins works behind the scenes before introducing more advanced DevOps tools.

This example demonstrates:

  • Pipeline stages
  • Compilation
  • Program execution
  • Artifact generation
  • Artifact archiving
  • Jenkins workspace concepts
  • Console output debugging

without introducing unnecessary complexity.


Why This Example?

Whether you're building:

  • A Java application
  • A Spring Boot service
  • A Docker image
  • A Kubernetes deployment

the fundamental Jenkins workflow remains largely the same:

Source Code
    ↓
Build
    ↓
Test
    ↓
Package
    ↓
Deploy
Enter fullscreen mode Exit fullscreen mode

Understanding this flow first makes learning advanced CI/CD concepts much easier.


Figure 1: Simplified Jenkins CI/CD flow used throughout this tutorial.

Environment

The following environment was used:

Ubuntu 24.04 LTS
OpenJDK 17
Git
Jenkins
Enter fullscreen mode Exit fullscreen mode

Jenkins Installation

Before creating Jenkins pipelines, Jenkins must be installed on the system.

Since Jenkins runs on Java, Java must be installed first.

Update Package Information

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Verify Java Installation

java -version
Enter fullscreen mode Exit fullscreen mode

If Java is not installed:

sudo apt install openjdk-17-jdk
Enter fullscreen mode Exit fullscreen mode

Verify Java again:

java -version
Enter fullscreen mode Exit fullscreen mode

Add Jenkins Repository Key

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
Enter fullscreen mode Exit fullscreen mode

Add Jenkins Repository

echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
Enter fullscreen mode Exit fullscreen mode

Update Package Information Again

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Jenkins

sudo apt install jenkins
Enter fullscreen mode Exit fullscreen mode

Start Jenkins Service

sudo systemctl start jenkins
Enter fullscreen mode Exit fullscreen mode

Enable Jenkins at Startup

sudo systemctl enable jenkins
Enter fullscreen mode Exit fullscreen mode

Verify Jenkins Status

sudo systemctl status jenkins
Enter fullscreen mode Exit fullscreen mode

Open Jenkins in Browser

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

During the first login, Jenkins requests an administrator password.

Retrieve it using:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode

Copy the password and complete the Jenkins setup wizard.


Project Structure

The project is intentionally very small.

calculator-app/
│
├── src/
│   └── Main.java
│
└── Jenkinsfile
Enter fullscreen mode Exit fullscreen mode

Java Application

Create:

src/Main.java
Enter fullscreen mode Exit fullscreen mode
public class Main {

    public static void main(String[] args) {

        int a = 10;
        int b = 20;

        int sum = a + b;

        System.out.println("Sum = " + sum);
    }
}
Enter fullscreen mode Exit fullscreen mode

When executed:

Sum = 30
Enter fullscreen mode Exit fullscreen mode

Although the application is tiny, it exercises the same Jenkins concepts used in larger CI/CD pipelines.


Jenkins Pipeline

Create:

Jenkinsfile
Enter fullscreen mode Exit fullscreen mode
pipeline {

    agent any

    stages {

        stage('Preparation') {
            steps {
                echo 'Preparing build environment...'
            }
        }

        stage('Compile') {
            steps {

                dir('/mnt/c/MyDomain/jenkin-work/apps/calculator-app') {

                    echo 'Compiling Java program...'

                    sh 'javac src/Main.java'
                }
            }
        }

        stage('Run Program') {
            steps {

                dir('/mnt/c/MyDomain/jenkin-work/apps/calculator-app') {

                    echo 'Running Java program...'

                    sh 'java -cp src Main > output.txt'
                }
            }
        }

        stage('Archive Output') {
            steps {

                dir('/mnt/c/MyDomain/jenkin-work/apps/calculator-app') {

                    archiveArtifacts artifacts: 'output.txt'
                }
            }
        }

        stage('Deploy Simulation') {
            steps {
                echo 'Deploying application...'
            }
        }
    }

    post {

        success {
            echo 'Pipeline executed successfully!'
        }

        failure {
            echo 'Pipeline failed!'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Note

For simplicity, this tutorial uses a hardcoded project path:

dir('/mnt/c/MyDomain/jenkin-work/apps/calculator-app')

This was intentionally done to demonstrate an important Jenkins workspace concept and keep the example lightweight.

In real-world CI/CD pipelines, Jenkins typically checks out source code directly from a Git repository into its workspace.

A more typical approach would look like:

stage('Checkout') {
    steps {
        git 'https://github.com/your-username/calculator-app.git'
    }
}

stage('Compile') {
    steps {
        sh 'javac src/Main.java'
    }
}

The purpose of this article is to focus on understanding pipeline execution, workspaces, artifacts, and build flow before introducing Git integration.


Understanding Each Stage

Preparation

echo 'Preparing build environment...'
Enter fullscreen mode Exit fullscreen mode

This stage simulates environment preparation.

In larger projects, this could include:

  • Loading credentials
  • Installing dependencies
  • Setting environment variables

Compile

sh 'javac src/Main.java'
Enter fullscreen mode Exit fullscreen mode

Compiles the Java source code.

If compilation fails, the pipeline stops immediately.


Run Program

sh 'java -cp src Main > output.txt'
Enter fullscreen mode Exit fullscreen mode

Executes the application and redirects the output to:

output.txt
Enter fullscreen mode Exit fullscreen mode

Contents:

Sum = 30
Enter fullscreen mode Exit fullscreen mode

Archive Output

archiveArtifacts artifacts: 'output.txt'
Enter fullscreen mode Exit fullscreen mode

Stores the generated file inside Jenkins for later access.


Deploy Simulation

echo 'Deploying application...'
Enter fullscreen mode Exit fullscreen mode

In a production pipeline, this stage could:

  • Deploy Docker containers
  • Copy files to servers
  • Deploy applications to Kubernetes
  • Restart services

Creating the Jenkins Pipeline Job

After logging into Jenkins:

  1. Click:
New Item
Enter fullscreen mode Exit fullscreen mode
  1. Enter:
calculator-pipeline
Enter fullscreen mode Exit fullscreen mode
  1. Select:
Pipeline
Enter fullscreen mode Exit fullscreen mode
  1. Click OK

  2. Under:

Definition
Enter fullscreen mode Exit fullscreen mode

Choose:

Pipeline Script
Enter fullscreen mode Exit fullscreen mode
  1. Paste the Jenkinsfile content.

  2. Save.

  3. Click:

Build Now
Enter fullscreen mode Exit fullscreen mode

CI/CD Flow Visualization

          Java Source Code
                  │
                  ▼
      Jenkins Pipeline Starts
                  │
                  ▼
             Compile
                  │
                  ▼
             Execute
                  │
                  ▼
        Generate Artifact
                  │
                  ▼
        Archive Artifact
                  │
                  ▼
          Post Actions
Enter fullscreen mode Exit fullscreen mode

Although this example is small, enterprise pipelines follow the same basic pattern.


Understanding Jenkins Workspaces

One of the most important Jenkins concepts is the workspace.

When Jenkins executes a build, it typically runs inside:

/var/lib/jenkins/workspace/calculator-pipeline
Enter fullscreen mode Exit fullscreen mode

My initial build failed because Jenkins could not locate:

src/Main.java
Enter fullscreen mode Exit fullscreen mode

The source code existed outside the Jenkins workspace.

The solution was:

dir('/mnt/c/MyDomain/jenkin-work/apps/calculator-app') {

    sh 'javac src/Main.java'
}
Enter fullscreen mode Exit fullscreen mode

This temporarily changes the working directory before executing commands.

Understanding workspaces will save significant debugging time when building larger pipelines.


Console Output

A successful build produced output similar to:

Preparing build environment...
Compiling Java program...
Running Java program...
Pipeline executed successfully!
Enter fullscreen mode Exit fullscreen mode

The Jenkins Console Output page is often the first place you'll inspect when troubleshooting build failures.


Generated Artifact

The pipeline generated:

output.txt
Enter fullscreen mode Exit fullscreen mode

Contents:

Sum = 30
Enter fullscreen mode Exit fullscreen mode

Real-world pipelines commonly generate:

  • JAR files
  • WAR files
  • Test reports
  • Coverage reports
  • Docker images
  • Deployment packages

The concept remains identical.


The Most Important Lesson

💡 Key Takeaway

Jenkins does not compile Java, build Docker images, or deploy applications itself.

Jenkins is an automation server that orchestrates external tools and commands.

Understanding this distinction is one of the most important concepts in DevOps and CI/CD.

When Jenkins executes:

sh 'javac src/Main.java'
Enter fullscreen mode Exit fullscreen mode

it is simply asking the operating system to run the Java compiler.

Likewise:

sh 'docker build .'
Enter fullscreen mode Exit fullscreen mode

asks Docker to build an image.

Jenkins orchestrates tools—it does not replace them.

Understanding this distinction is fundamental to learning DevOps and CI/CD.


Future Enhancements

Once comfortable with this example, you can gradually introduce:

  • Git integration
  • Automatic build triggers
  • Maven builds
  • Unit testing
  • Docker integration
  • Tomcat deployment
  • Kubernetes deployment
  • Full CI/CD automation

The core Jenkins concepts remain exactly the same.


Final Thoughts

Many engineers first encounter Jenkins through large enterprise pipelines that can feel intimidating.

Starting with a tiny project makes the learning process much easier.

A simple Java application is enough to understand:

  • Pipeline stages
  • Build execution
  • Artifacts
  • Workspaces
  • Automation flow

Once these fundamentals become clear, moving toward Docker, Kubernetes, and enterprise CI/CD becomes much more natural.

Sometimes the best way to understand a large system is to begin with the smallest possible working example.

If you're new to Jenkins, try building this example yourself before moving on to Maven, Docker, or Kubernetes. The concepts learned here will transfer directly to larger CI/CD pipelines.

Top comments (0)