Build Java Project using Maven in GitLab CI/CD

In this article we are going to cover Build Java Project using Maven in GitLab CI/CD.

#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

Build Java Project using Maven in GitLab CI/CD 1
launch instance
Build Java Project using Maven in GitLab CI/CD 2
AMI

Step 2: Choose an Instance type so if you want another instance you can select as per your choice.

Build Java Project using Maven in GitLab CI/CD 3
choose an instance type

Step 3: Then Next is Configure Instance. Here you can specify the number of instances you want.

Build Java Project using Maven in GitLab CI/CD 4
configure instance

Step 4: Here they give us 8 GiB free space.

Build Java Project using Maven in GitLab CI/CD 5
add storage

Step 5: Here you can give any meaning full name to your instance.

Build Java Project using Maven in GitLab CI/CD 6
add tag

Step 6 : So here select “Select an existing security group” then select “default

Build Java Project using Maven in GitLab CI/CD 7
configure security group

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

Build Java Project using Maven in GitLab CI/CD 8
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.

Build Java Project using Maven in GitLab CI/CD 9
connect to mobaxterm

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.

Build Java Project using Maven in GitLab CI/CD 10
gitlab-runner

Once you registered Runner for project then you will get runner as below,

Build Java Project using Maven in GitLab CI/CD 11
gitlab-runner1

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

Build Java Project using Maven in GitLab CI/CD 12
untagged jobs

#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

GitLab Official Page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

1 thought on “Build Java Project using Maven in GitLab CI/CD”

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

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap