VOOZH about

URL: https://dev.to/sanjayghosh/understanding-jenkins-cicd-using-a-tiny-java-project-a-beginner-friendly-walkthrough-6co

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


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

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

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

Verify Java Installation

java -version

If Java is not installed:

sudo apt install openjdk-17-jdk

Verify Java again:

java -version

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

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

Update Package Information Again

sudo apt update

Install Jenkins

sudo apt install jenkins

Start Jenkins Service

sudo systemctl start jenkins

Enable Jenkins at Startup

sudo systemctl enable jenkins

Verify Jenkins Status

sudo systemctl status jenkins

Open Jenkins in Browser

http://localhost:8080

During the first login, Jenkins requests an administrator password.

Retrieve it using:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password and complete the Jenkins setup wizard.


Project Structure

The project is intentionally very small.

calculator-app/
│
├── src/
│ └── Main.java
│
└── Jenkinsfile

Java Application

Create:

src/Main.java
public class Main {

 public static void main(String[] args) {

 int a = 10;
 int b = 20;

 int sum = a + b;

 System.out.println("Sum = " + sum);
 }
}

When executed:

Sum = 30

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


Jenkins Pipeline

Create:

Jenkinsfile
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!'
 }
 }
}

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...'

This stage simulates environment preparation.

In larger projects, this could include:

  • Loading credentials
  • Installing dependencies
  • Setting environment variables

Compile

sh 'javac src/Main.java'

Compiles the Java source code.

If compilation fails, the pipeline stops immediately.


Run Program

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

Executes the application and redirects the output to:

output.txt

Contents:

Sum = 30

Archive Output

archiveArtifacts artifacts: 'output.txt'

Stores the generated file inside Jenkins for later access.


Deploy Simulation

echo 'Deploying application...'

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
  1. Enter:
calculator-pipeline
  1. Select:
Pipeline
  1. Click OK

  2. Under:

Definition

Choose:

Pipeline Script
  1. Paste the Jenkinsfile content.

  2. Save.

  3. Click:

Build Now

CI/CD Flow Visualization

 Java Source Code
 │
 ▼
 Jenkins Pipeline Starts
 │
 ▼
 Compile
 │
 ▼
 Execute
 │
 ▼
 Generate Artifact
 │
 ▼
 Archive Artifact
 │
 ▼
 Post Actions

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

My initial build failed because Jenkins could not locate:

src/Main.java

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'
}

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!

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


Generated Artifact

The pipeline generated:

output.txt

Contents:

Sum = 30

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'

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

Likewise:

sh 'docker build .'

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.