This repository contains a minimal Java application designed to demonstrate a complete CI/CD workflow using Jenkins, Maven, Docker, and Docker Compose.
This project is intended for beginners who want to learn:
- How to create a simple Java application
- How to build, compile, and test it using Maven
- How to containerize it with Docker
- How to run it using Docker Compose
- How to automate the entire workflow using a Jenkins pipeline
The application itself is a simple Java "Hello World" program with a basic JUnit test.
java-jenkins-docker/
│── src/
│ ├── main/java/com/example/App.java # Main Java Application
│ └── test/java/com/example/AppTest.java # Unit Tests (JUnit)
│── pom.xml # Maven Build File
│── Dockerfile # Docker Image Definition
│── docker-compose.yml # Docker Compose Configuration
│── Jenkinsfile # Jenkins Pipeline Script
│── README.md # Project Documentation
We use Maven to compile the Java source code:
mvn clean packagemvn cleanremoves any previous build artifacts.mvn packagecompiles the code and packages it into a JAR file.- Output JAR:
target/java-jenkins-docker-1.0-SNAPSHOT.jar
The project includes a basic JUnit test:
mvn testThis runs all tests inside the src/test/java directory.
Maven automatically compiles the Java source files during the package phase:
mvn compileThis ensures all Java files are translated into .class files in the target/classes directory.
After building the project, run the application locally:
java -jar target/java-jenkins-docker-1.0-SNAPSHOT.jarExpected Output:
Hello from Java Application for Jenkins CI/CD!
docker build -t java-jenkins-docker:latest .docker run --rm java-jenkins-docker:latestIf you want to run the app using Docker Compose:
docker-compose up --builddocker-compose downThe provided Jenkinsfile automates the process:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Docker Build') {
steps {
sh 'docker build -t java-jenkins-docker:latest .'
}
}
stage('Docker Run') {
steps {
sh 'docker run --rm java-jenkins-docker:latest'
}
}
}
}
Pipeline Stages:
- Build – Compiles and packages the Java code
- Test – Runs JUnit tests
- Docker Build & Run – Builds and runs the Docker image
You need to have the following installed:
- Java 17+
- Apache Maven
- Docker
- Docker Compose
- Jenkins (optional for CI/CD testing)
This project is released under the MIT License.