In this article we are going to cover Build Java Project using Maven in GitLab CI/CD.
Table of Contents
#1.How to create Ubuntu 20.04 LTS EC2 Instance in AWS?
In this article we are going to learn how to build java project using maven ig GitLab CI/CD so there are 7 steps
Step 1: Go to Ec2 instance then click on launch instance then select Ubuntu 20.04 LTS
Step 2: Choose an Instance type so if you want another instance you can select as per your choice.
Step 3: Then Next is Configure Instance. Here you can specify the number of instances you want.
Step 4: Here they give us 8 GiB free space.
Step 5: Here you can give any meaning full name to your instance.
Step 6 : So here select “Select an existing security group” then select “default”
Step 7 : Then review once its correct or not then click on launch.
Then after click on launch there is pop up select an existing key pair or create a key pair so if have already a key pair then select a key pair otherwise create a key pair
So now here im using MobaXterm to connect terminal so copy the ip_address of your instance connect to your terminal
So you can see in this image firstly click on session then SSH then in Remote host paste the ip_address then in username you can specify only ubuntu for ubuntu 20.04 LTS. Then in Use private key you need to give the path of your key pair that we have created. Then click on Ok.
After click on OK your terminal is ready to run commands
#2.How to Install GitLab Runner on Ubuntu 20.04 LTS
Step 1: Add the Official GitLab Repository
First add the official GitLab Repository using below command, to check latest Gitlab Repository visit the official GitLab Runner page
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
Step 2: Install GitLab Runner on Ubuntu
Run below command to install latest GitLab Runner on Ubuntu 20.04 LTS
sudo apt-get install gitlab-runner
use below commands to install a specific version of GitLab Runner on Ubuntu.
apt-cache madison gitlab-runner
sudo apt-get install gitlab-runner=10.0.0
Command to check GitLab Runner version
sudo gitlab-runner -version
To check status if GitLab Runner service is running or not
sudo gitlab-runner status
Commands to Start, Stop and Restart GitLab Runner
sudo gitlab-runner start
sudo gitlab-runner stop
sudo gitlab-runner restart
#3.How to Register GitLab-Runner
Step 1: To Register GitLab-Runner run the below command:
sudo gitlab-runner register
Step 2: Then enter the URL and Registration Token from GitLab Account
- Go to the GitLab Account
- Click on Project
- Then click on setting and in below click on CI/CD
- Click on Runner Expand scroll down and copy URL and registration token
- And paste on command
Step 3: Then you can give any meaning full description
Step 4: Then Enter tags for the runner (any tag)
Step 5: Then choose any executor – shell , ssh , docker , etc.
Once you registered Runner for project then you will get runner as below,
Successfully, We have performed GitLab Runner Registration.
Error: This job is stuck because the project doesn’t have any runners online assigned to it. Go to Runners page.
Solution:
You have added tags while registering GitLab Runner however you have not added tags for your Job.
Follow below steps to add tags.
1. Navigate to Settings and click on CI/CD inside this click on Expand of Runners section
2. Go to Runners and check on Indicates whether this runner can pick jobs without tags Box
#4.How to Install OpenJDK and Maven on Ubuntu 20.04 LTS using APT (GitLab Runner Instance)
Step 1: Install OpenJDK on Ubuntu 20.04 LTS
sudo apt update, sudo apt install default-jdk-y
To check Java Version
java –version
Step 2: Install maven on Ubuntu 20.04 LTS using APT so run the below command:
sudo apt install maven
Step 3: To check Maven version run the below command:
mvn –version
Step 4: To remove maven from Ubuntu 20.04 LTS using APT run the command:
sudo apt remove maven
#5.How to Create a Demo Maven Project?
Step 1: Create a directory somewhere and start a shell in that directory so run the command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
ls
cd my-app
ls
cd
git clone “paste the HTTP clone of your GitLab repository”
They ask you username and password of your Gitlab Account
To check your repository clone or not
ls
cd my-app
sudo cp-R ./* /home/ubuntu/java-maven
cd
cd java-maven
git add .
git commit -m “java project using maven”
git push origin main
Note: java-maven is name of your repository
After run these commands you will see in your Repository there are two files added src and pom.xml
#6.Build Java Project using Maven in GitLab CI/CD
Step 1: Create .gitlab-ci.yml in root directory of GitLab Maven Project Branch
Step 2: Paste below GitLab CI/CD Pipeline code into it
stages:
- build
build-java-project:
stage: build
script:
mvn package
artifacts:
paths:
- /home/gitlab-runner/
Step 3: Check GitLab Pipeline Status
Step 4: Check .jar is created for Maven Project in Gitlab Runner
To check .jar is created or not Run below commands:
cd /home/gitlab-runner
ls
cd builds
ls
cd()gitlab-runner id
0/username/repository name
ls
cd target
ls
Then you will see here .jar file that means you have successfully Build Java Project using Maven in GitLab CI/CD.
Conclusion:
In this article we have covered Build Java Project using Maven in GitLab CI/CD.
Related Articles:
Reference
Where are the `java -jar SNAP….jar` command? Could anyone provide a useful, workable gitlab-ci.yml example? Could I find a simple example in the whole internet?