SonarQube Integration for Java Gradle Project using GitLab CI

In this article, We are going to perform How to Install Gradle on Ubuntu 20.04/18.04/16.04 LTS and SonarQube integration for Java gradle Project.

Prerequisites

  • Ubuntu Server 20.04/18.04/16.04 LTS
  • SSH access with sudo privileges

Installing OpenJDK on Ubuntu 20.04 LTS

Update system Packages:

sudo apt update

Install OpenJDK:

sudo apt install openjdk-11-jdk

Lets verify java version

Java --version

Output:

openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

#1:Install Gradle on Ubuntu

Download the gradle setup in /tmp directory using below command,

cd /tmp

To download specific version of gradle use below syntax

wget https://downloads.gradle-dn.com/distributions/gradle-7.0.2-bin.zip

Install unzip on Ubuntu if not installed

sudo apt install unzip

Extract Gradle Package:

sudo unzip -d /opt/gradle gradle-7.0.2-bin.zip

Set Environment Variables:

sudo nano /etc/profile.d/gradle.sh

Paste the following configuration:

export GRADLE_HOME=/opt/gradle/latest
export PATH=$PATH:/opt/gradle/gradle-7.0.2/bin

Load the environment variable using below command

source /etc/profile.d/gradle.sh

Give the excutable permission to above script

sudo chmod +x /etc/profile.d/gradle.sh

Verify Gradle version:

gradle -v

Output:

------------------------------------------------------------
Gradle 7.0.2
------------------------------------------------------------

Build time:   2021-05-14 12:02:31 UTC
Revision:     1ef1b260d39daacbf9357f9d8594a8a743e2152e

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.13 (Ubuntu 11.0.13+8-Ubuntu-0ubuntu1.20.04)
OS:           Linux 5.4.0-97-generic amd64

#2:Add this line in build.gradle

plugins {
id "org.sonarqube" version "3.3"
}

#3:Generate Token in sonarqube 

go to administrator<<my account<<security<<tokens

SonarQube Integration for Java Gradle Project using GitLab CI 1

#4:Add variables in your repository 

SONAR_TOKEN: <your sonar token>

  1. Uncheck the “Protect Variable” checkbox
  2. Check the “Mask Variable” checkbox

SONAR_HOST_URL : http://104.248.58.31:9000/

  1. Uncheck the “Protect Variable” checkbox
  2. Leave the “Mask variable” checkbox unchecked

SonarQube Integration for Java Gradle Project using GitLab CI 2

#5:SonarQube integration for Java gradle Project

Below gitlab-ci.yml file for SonarQube Integration for Java gradle Project.

image: gradle:alpine

sonarqube-check:
  stage: test
  script: gradle sonarqube -Dsonar.qualitygate.wait=true
  only:
      - main

Conclusion:

In this article we have covered SonarQube Integration for Java Gradle Project.

Related Articles:

SonarQube GitLab CI/CD Official page

Shweta Mamidwar

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

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