PROJECT 3

Automated Java Build Pipeline with Jenkins

Set up Java development environments, install Jenkins CI servers, configure GitHub Webhook integrations, and script multi-stage build pipelines.

Environment
Ubuntu / Jenkins Daemon
Difficulty
Intermediate
Course Module
Chapters 4–5: CI & Jenkins
Deliverables
Jenkinsfile & Webhook Proof
1. System Architecture & Workflow

The diagram below highlights the integration flow between Java runtimes, Maven build lifecycles, and a locally running Jenkins CI controller orchestration service.

CI/CD Execution Topology GitHub Server git-repo.git Triggers Webhook on push event Local Linux VM (Port 2222 / 8080) Jenkins Master Pipeline Runner Pipeline Job Executes: Jenkinsfile Java Build Env OpenJDK 17 (Runtime) Maven (mvn package) Webhook (8080)
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1

Install Java Runtime Development Kits, Maven, and Gradle

Prepare the compiler packages required to build Java source code archives on the Linux environment.

$ sudo apt update && sudo apt install -y openjdk-17-jdk maven gradle
This command updates the repository index files and installs the OpenJDK 17 JDK package, along with the Maven and Gradle build automation systems.
STEP 2

Configure Permanent Environment Paths

Locate the Java installation binary paths and write environment variables inside the configuration profiles.

$ readlink -f $(which java)
This command resolves symbolic links to return the actual filesystem path of the default java execution binary, identifying the JDK directory.
$ echo "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" >> ~/.bashrc
This command adds the global JAVA_HOME environment variable pointing to the resolved OpenJDK folder path inside the user's .bashrc profile.
$ echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bashrc && source ~/.bashrc
This command adds the Java bin folder to the execution PATH variable, enabling you to run Java commands from any directory, and reloads the shell settings.
STEP 3

Generate and Compile a Maven Application Template

Bootstrap a standard Java application project structure using the Maven archetype generator and compile it.

$ mvn archetype:generate -DgroupId=com.devops.app -DartifactId=hello-world -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command initializes a standard Maven project layout with structured source folders and a baseline POM.xml config file.
$ cd hello-world && mvn package
This command moves the terminal context into the project root directory and compiles the source code, runs tests, and packages it into a JAR file.
STEP 4

Install and Configure a Local Jenkins CI Server

Register the official Jenkins GPG keys, add the stable package repositories, install the service, and display initialization credentials.

$ sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
This command downloads Jenkins' cryptographic GPG keys, enabling apt to verify the authenticity of downloaded packages.
$ 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
This command writes the official Jenkins package repository source entry into apt's configuration directory, making the packages available for installation.
$ sudo apt update && sudo apt install -y jenkins
This command updates the repository indexes to include Jenkins and installs the server application binary daemon.
$ sudo systemctl start jenkins && sudo systemctl enable jenkins
This command starts the Jenkins background process and configures the service to launch automatically during system startup.
$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword
This command prints the temporary administrator password generated by the installer, which is required to unlock the Jenkins dashboard at http://localhost:8080.
3. Operational Pipeline Architecture

The diagram below traces the webhook-triggered continuous integration pipeline, starting from code commits through compile tests, to jar artifact outputs.

1. Git Push Developer commits code to GitHub git push origin 2. Webhook Event GitHub notifies Jenkins controller POST /github-webhook 3. Build Trigger Jenkins executes the Jenkinsfile mvn clean test 4. Packaging Generate and archive executable JAR file target/*.jar
4. Part 2: Complete Deliverable Assets & Production Templates

To automate the continuous integration process, we will write a declarative Jenkinsfile. Below is a step-by-step breakdown of how this pipeline is built, followed by the final consolidated script template.

Step-by-Step Script Construction

Step 1

Define the Pipeline Block and Agent

Setup the main container block and specify where the build commands will execute.

pipeline { agent any }
The pipeline block wraps the entire CI structure, while agent any allows Jenkins to schedule and run the stages on any available executor agent.
Step 2

Configure Build Environment Tools

Declare the JDK and Maven tools needed to compile Java files.

tools { maven 'M3' jdk 'JDK17' }
This loads specific compiler pathways configured in the Jenkins tools directory, setting up Maven (M3) and Java (JDK17).
Step 3

Implement Clean and Compile Stages

Write build stages to clear out previous compilation assets and check the compilation of Java source files.

stages { stage('Clean Workspace') { steps { sh 'mvn clean' } } stage('Compile Source') { steps { sh 'mvn compile' } } }
This defines the initial steps of the pipeline: cleaning previous build outputs and compiling the current source code using standard Maven commands.
Step 4

Implement Unit Testing Stage with Reports

Execute JUnit test suites and collect outputs for dashboard graphing.

stage('Unit Testing') { steps { sh 'mvn test' } post { always { junit '**/target/surefire-reports/*.xml' } } }
This step runs the tests. The post action block runs always (even on failures), collecting XML test logs to graph test results on the Jenkins dashboard.
Step 5

Package and Archive Artifacts

Compile the code into an executable JAR file and copy it to the Jenkins server for archiving.

stage('Package Archive') { steps { sh 'mvn package -DskipTests' } post { success { archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true } } }
This step packages the application into a JAR file, skipping tests (since they were verified in the previous stage), and archives the output jar files if the stage completes successfully.

Combined Automated Script

Save the consolidated blocks below as a file named Jenkinsfile in the root folder of your project repository, commit it, and push it to trigger the build:

pipeline { agent any tools { maven 'M3' // Must match the Maven identifier configured in Jenkins Global Tool Configuration jdk 'JDK17' // Must match the JDK identifier configured in Jenkins Global Tool Configuration } stages { stage('Clean Workspace') { steps { echo 'Cleaning workspace directories...' sh 'mvn clean' } } stage('Compile Source') { steps { echo 'Compiling Java source files...' sh 'mvn compile' } } stage('Unit Testing') { steps { echo 'Running unit tests...' sh 'mvn test' } post { always { junit '**/target/surefire-reports/*.xml' } } } stage('Package Archive') { steps { echo 'Packaging source libraries into executable binary JAR...' sh 'mvn package -DskipTests' } post { success { archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true } } } } }
5. Deliverables Summary

Verify that the following configurations and outputs exist inside your Jenkins workspace.

Created Files / Templates

  • /home/devops/hello-world/POM.xml - Maven project object model build file.
  • /home/devops/hello-world/Jenkinsfile - Declarative multi-stage pipeline script.
  • /etc/apt/sources.list.d/jenkins.list - Jenkins repository list file.

Verification Artifacts / Execution Proof

  • Screenshot of Jenkins pipeline dashboard showing green status blocks for all build stages.
  • Archived artifact binary (e.g. hello-world-1.0-SNAPSHOT.jar) hosted on Jenkins controller.
  • Webhook trigger logs on GitHub confirming delivery status code 200 OK to Jenkins endpoint.
6. Closing Explanation: Why We Did This & What It Accomplishes

Architectural Intent & Operational Impact

Why We Did This

What This Accomplishes