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:
- GitLab
Repository: A repository created in GitLab.
- Java
Development Kit (JDK): Installed on your local system for local Gradle
builds.
- Gradle:
Installed globally or as a wrapper in your project.
- Git
CLI: Installed and configured with access to your GitLab repository.
- 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
- Gradle
Not Found:
- Ensure
Gradle is installed or use the Gradle Wrapper:
./gradlew clean build
- JAVA_HOME
Not Set:
- Set
the JAVA_HOME environment variable to your JDK installation path.
- 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