How to Build and Deploy Java Applications Using GitLab CI/CD and Gradle

Building and Deploying a Java Application with Gradle in a GitLab CI/CD Pipeline

In this guide, we’ll walk through setting up a complete GitLab CI/CD pipeline to build a Java application using Gradle, create a JAR file, and push it to the GitLab Package Registry. Along the way, we’ll also explain how to set up your local GitLab repository to upload files and manage your project efficiently.


Prerequisites

Before diving in, ensure you have the following:

  1. GitLab Repository: A repository created in GitLab.
  2. Java Development Kit (JDK): Installed on your local system for local Gradle builds.
  3. Gradle: Installed globally or as a wrapper in your project.
  4. Git CLI: Installed and configured with access to your GitLab repository.
  5. Basic Project Structure: A Java project set up with Gradle (including build.gradle and settings.gradle).

Setting Up the GitLab CI/CD Pipeline

To automate the build and deployment process, we’ll create a .gitlab-ci.yml file. Here’s a step-by-step breakdown:


1. Creating the .gitlab-ci.yml File

The .gitlab-ci.yml file defines the pipeline stages, jobs, and the sequence of operations. Below is the YAML configuration:

stages:

  - build

  - publish

 

variables:

  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

 

build:

  stage: build

  image: gradle:7.6-jdk11  # Use Gradle Docker image with JDK 11

  script:

    - gradle clean build  # Clean and build the project

  artifacts:

    paths:

      - build/libs/*.jar  # Store the JAR file for the next stage

 

publish:

  stage: publish

  image: alpine:3.17  # Lightweight image for pushing the JAR

  script:

    - apk add --no-cache curl git  # Install necessary tools

    - curl --header "PRIVATE-TOKEN: $CI_JOB_TOKEN" --upload-file build/libs/*.jar "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/generic/$CI_PROJECT_NAME/$CI_COMMIT_REF_NAME/myapp.jar"


2. Breaking Down the Pipeline

  • Stages:
    • build: Compiles and builds the Java project using Gradle.
    • publish: Uploads the generated JAR file to the GitLab Package Registry.
  • Variables:
    • GRADLE_OPTS: Disables the Gradle daemon for compatibility with CI/CD environments.
  • Jobs:
    • Build Job:
      • Uses the gradle:7.6-jdk11 image.
      • Runs gradle clean build to build the project and generates a JAR file in build/libs/.
      • Stores the JAR file as an artifact for the next stage.
    • Publish Job:
      • Uses a lightweight Alpine image.
      • Installs curl and git for uploading the artifact.
      • Uploads the JAR to the GitLab Package Registry using the curl command.

3. Configuring Your Project Structure

Ensure your project structure aligns with Gradle's expectations:

project-root/

── build.gradle

── settings.gradle

── src/

   ── main/

      ── java/

      └── resources/

   └── test/

       ── java/

       └── resources/

  • build.gradle Example:

plugins {

    id 'java'

}

 

group = 'com.example'

version = '1.0.0'

 

repositories {

    mavenCentral()

}

 

dependencies {

    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'

}

 

test {

    useJUnitPlatform()

}

  • settings.gradle Example:

rootProject.name = 'gradle-java-pipeline'


Uploading Local Files to Your GitLab Repository

If you have new files to upload to your repository (e.g., .gitlab-ci.yml), follow these steps:


1. Add Files to Git

Navigate to your local repository and stage the new files:

cd path/to/local/repo

git add .gitlab-ci.yml newfile.java


2. Commit Changes

Commit the staged files with a meaningful message:

git commit -m "Added CI/CD pipeline and new Java file"


3. Push to GitLab

Push the changes to your GitLab repository:

git push origin main


Verifying the Pipeline

After pushing the .gitlab-ci.yml file, the GitLab pipeline will automatically trigger. Check the pipeline's progress in the GitLab CI/CD > Pipelines section.


Troubleshooting Common Issues

  1. Gradle Not Found:
    • Ensure Gradle is installed or use the Gradle Wrapper:

./gradlew clean build

  1. JAVA_HOME Not Set:
    • Set the JAVA_HOME environment variable to your JDK installation path.
  2. Pipeline Fails:
    • Check the logs in the GitLab CI/CD section for errors and adjust the .gitlab-ci.yml file accordingly.

Conclusion

With this setup, you’ve automated the build and deployment of your Java application using Gradle in a GitLab CI/CD pipeline. This process not only saves time but also ensures consistency in how your application is built and deployed.

If you have further questions or issues, feel free to ask for assistance. 🚀

 

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...
//]]>