Automating Maven Java Project Builds and Deployments to GitLab Using CI/CD Pipelines
In today's fast-paced development environment, automating
build and deployment processes has become essential. With GitLab CI/CD, we can
streamline these tasks and focus more on coding. In this blog post, I’ll
demonstrate how to set up a CI/CD pipeline for a Maven Java project to build
the application and push the resulting JAR file to GitLab’s Package Registry.
Overview of the Pipeline
The pipeline consists of two stages:
- Build:
Compile and package the Java project using Maven.
- Push:
Upload the generated JAR file to GitLab’s Package Registry.
Using GitLab CI/CD’s built-in variables and automation
tools, we can implement this workflow efficiently.
Pipeline Configuration File
Below is the .gitlab-ci.yml configuration for the pipeline:
stages:
- build
- push
# Variables for Docker
and Maven configuration
variables:
REGISTRY: "registry.gitlab.com"
PROJECT_PATH: "Mari-M/maven-java-project-push-to-gitlab" # Manually specify your project path
VERSION: "1.0.$CI_PIPELINE_ID"
# Build stage - Compile
and package the Java project with Maven
build:
stage: build
image: maven:3.8.4-jdk-11 # Use Maven image with OpenJDK 11
script:
- mvn clean package # Run Maven to clean and package the project
artifacts:
paths:
- target/*.jar # Store the JAR file as an artifact for later
stages
expire_in: 1 hour # Set expiration time for the artifact
# Push stage - Push the
JAR file to GitLab Container Registry (or a GitLab generic package registry)
push:
stage: push
image: curlimages/curl:latest # Using a curl image to ensure curl is
available
script:
- echo "Pushing JAR file to GitLab
registry"
- |
curl --header "PRIVATE-TOKEN:
$CI_JOB_TOKEN" \
--upload-file target/*.jar \
"https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/generic/my-java-app/$VERSION/my-java-app-$VERSION.jar"
only:
- main
# Only push when changes are made to the main branch
Step-by-Step Explanation
1. Stages
The pipeline is divided into two stages:
- Build:
Compiles and packages the Java project.
- Push:
Uploads the artifact (JAR file) to the GitLab Package Registry.
2. Variables
We use the following variables:
- REGISTRY:
Points to the GitLab registry (registry.gitlab.com).
- PROJECT_PATH:
Specifies the project path in GitLab.
- VERSION:
Generates a version string dynamically using the pipeline ID ($CI_PIPELINE_ID).
These variables make the pipeline dynamic and reusable
across multiple projects.
3. Build Stage
In the build stage:
- A
Maven Docker image (maven:3.8.4-jdk-11) provides the required environment
to run Maven commands.
- The mvn
clean package command compiles the Java code and packages it into a JAR
file, saved in the target/ directory.
- The
resulting JAR file is stored as an artifact, which is passed to the
next stage.
4. Push Stage
In the push stage:
- A
lightweight curl Docker image is used to push the artifact.
- The curl
command uploads the JAR file to GitLab’s Generic Package Registry using
GitLab’s REST API.
- The
upload URL dynamically uses the project ID ($CI_PROJECT_ID) and version to
ensure each upload is unique.
- The
job executes only when changes are pushed to the main branch.
Benefits of This Pipeline
- Automation:
Builds and deployments are automated, saving time and reducing manual
effort.
- Artifact
Management: Artifacts are safely stored in GitLab’s Package Registry
for easy access.
- Versioning:
Each pipeline run generates a unique version, ensuring traceability and
avoiding conflicts.
- Branch
Control: By restricting the push stage to the main branch, you prevent
unnecessary uploads during feature development.
How It Works in Action
- When
you push changes to the main branch:
- The
pipeline begins by executing the build stage, compiling and
packaging the Maven project.
- Once
the JAR file is ready, the push stage uploads it to GitLab’s
Package Registry.
- The
output of each stage is logged in the GitLab pipeline dashboard, giving
you a clear view of the process.
Conclusion
By leveraging GitLab CI/CD, we can automate the entire
lifecycle of a Maven Java project—from building the application to managing its
artifacts. This pipeline configuration serves as a template for automating
similar workflows for other projects. The flexibility of GitLab’s CI/CD
combined with Maven and Docker makes it an excellent choice for modern
development teams.
What other CI/CD challenges have you faced in your projects?
Share your thoughts and let’s discuss in the comments!
0 Comments