GitLab CI/CD Pipeline for Maven Java Projects: A Complete Guide

 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:

  1. Build: Compile and package the Java project using Maven.
  2. 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

  1. Automation: Builds and deployments are automated, saving time and reducing manual effort.
  2. Artifact Management: Artifacts are safely stored in GitLab’s Package Registry for easy access.
  3. Versioning: Each pipeline run generates a unique version, ensuring traceability and avoiding conflicts.
  4. Branch Control: By restricting the push stage to the main branch, you prevent unnecessary uploads during feature development.

How It Works in Action

  1. 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.
  2. 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

n8n Automation: The Ultimate Guide to Mastering Workflow Automation

  The Ultimate Guide to n8n: A No-Code Workflow Automation Tool Introduction In the modern digital world, automation is the key to efficiency. Whether you're a developer, a marketer, or an entrepreneur, automating repetitive tasks can save time and boost productivity. One of the most powerful automation tools available today is n8n . n8n is an open-source, no-code workflow automation tool that helps you integrate various applications and services with ease. Unlike traditional automation platforms, n8n gives you the flexibility to customize workflows without limitations. This blog explores everything about n8n , from installation to advanced features, helping you leverage its full potential. What is n8n? n8n (pronounced as "n-eight-n") is a workflow automation tool that connects different apps and services. It provides a visual interface to design automation processes without writing extensive code. With over 300 integrations , n8n allows users to conn...
//]]>