In this article we are going to cover How to Integrate SonarQube with Java Maven project using GitLab CI, Downloading latest version of maven, configuring environment variables for maven and check maven version using command line.
Table of Contents
Prerequisites
- Ubuntu 20.04 LTS with minimal Installation
- SSH Access with sudo privileges
- SonarQube Server Details
How to Install GitLab Runner
Please follow this article to Install GitLab Runner and Register Runner :
How to Install GitLab Runner on Ubuntu 20.04 LTS
How to Install Maven on Ubuntu 20.04 LTS using APT
Update the system packages
sudo apt update
Install maven on Ubuntu 20.04 LTS using APT
sudo apt install maven
Check Maven Version using command line on Ubuntu 20.04 LTS
mvn -version
To remove maven from Ubuntu 20.04 LTS using APT
sudo apt remove maven
How to Install Maven on Ubuntu 20.04 LTS by downloading Apache Maven Binaries
update the system packages
sudo apt update
Install OpenJDK 11 on Ubuntu 20.04 LTS
sudo apt install default-jdk -y
Check the Install OpenJDK version using command line
java --version
Downloading Latest Apache Maven on Ubuntu 20.04 LTS
Download the latest Apache Maven version on Ubuntu from Apache Maven official download page. Here we are downloading Apache Maven version 3.6.3.
Navigate to /tmp directory
cd /tmp
Download the Apache Maven version using wget
wget http://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
once downloaded extract the apache maven setup in /opt directory
sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt
Create a symlink for apache maven directory to update the maven versions
sudo ln -s /opt/apache-maven-3.6.3 /opt/maven
Setup Apache Maven Environment Variables
To setup apache maven environment variables in ubuntu 20.04 LTS, create a file named maven.sh in /etc/profile.d/ directory
sudo nano /etc/profile.d/maven.sh
Paste the below lines
export JAVA_HOME=/usr/lib/jvm/default-java export M2_HOME=/opt/maven export MAVEN_HOME=/opt/maven export PATH=${M2_HOME}/bin:${PATH}
Give the executable permission using chmod
sudo chmod +x /etc/profile.d/maven.sh
Load the Environment Variables using source command
source /etc/profile.d/maven.sh
Verify the installed maven version using command line
mvn -version
Creating variables for SonarQube inside GitLab
Go to settings<<CI/CD<<variables<<Expand it
#Add sonarqube url in $SONAR_URL variable #Add sonarqube username in $SONAR_LOGIN variable #Add sonarqube password in $SONAR_PASSWORD variable
When creating variables you have to remove protect variable flag
Integrate SonarQube with Java maven project using GitLab CI
Below is gitlab-ci.yml to Integrate SonarQube with Java maven project using GitLab CI/CD Pipeline
build-maven-project:
stage: build
script:
mvn package
artifacts:
paths:
- /home/gitlab-runner/
sonarqube_master_job:
stage: test
only:
- main
script:
- mvn --batch-mode verify sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD
Conclusion:
In this article we have covered How to Integrate SonarQube with Java Maven project using GitLab CI.
Related Articles:
SonarQube GitLab CI/CD Official page
SonarQube Integration for Java Gradle Project using GitLab CI
Reference:
Hi Shweta,
Thanks for the content above. I am impressed by the quality of it. I am working my way to become a DevOps engineer and would love your help.
Looking forward to hear from you.
Best regards
Taiwo