Integrating SonarQube with Java Maven CI

Aug 22, 2024

Integration of SonarQube with Java Maven Project Using GitLab CI

Introduction

  • Overview of the tutorial on integrating SonarQube with a Java Maven project using GitLab CI.
  • Mention of the setup environment including SonarQube dashboard and project repository.

Prerequisites

  • Ensure Maven and Java are installed on Ubuntu.
  • Use of Digital Ocean droplet and Mobile Extreme to connect to the Ubuntu terminal.

Step 1: Install Maven

  • Update system packages:
    sudo apt update  
    
  • Install Maven:
    sudo apt install maven  
    
  • Verify Maven installation:
    mvn -v  
    
  • Command to remove Maven if necessary:
    sudo apt remove maven  
    

Step 2: Install Java

  • Update system packages again:
    sudo apt update  
    
  • Install OpenJDK 11:
    sudo apt install openjdk-11-jdk  
    
  • Verify Java installation:
    java -version  
    

Step 3: Download and Setup Apache Maven Environment Variables

  • Download Maven and extract it:
    cd /tmp  
    wget [Maven download link]  
    tar -xvf apache-maven-x.x.x-bin.tar.gz  
    
  • Set up environment variables for Maven by editing ~/.bashrc:
    export MAVEN_HOME=/path/to/maven  
    export PATH=$PATH:$MAVEN_HOME/bin  
    
  • Load the environment variables:
    source ~/.bashrc  
    
  • Check Maven version again:
    mvn -v  
    

Step 4: Install GitLab Runner

  • Visit the official GitLab Runner page and follow installation instructions:
  • Add the official GitLab repository and install GitLab Runner:
    sudo apt-get install gitlab-runner  
    

Step 5: Register GitLab Runner

  • Register the GitLab runner using the command:
    gitlab-runner register  
    
  • Provide the GitLab instance URL and registration token:
  • Enter a description and tags for the runner.
  • Check if the runner registered successfully and adjust settings if necessary.

Step 6: Create and Run GitLab CI Pipeline

  • Create a new .gitlab-ci.yml file in the repository:
  • Define the CI/CD pipeline with two stages: build and test.
  • Add necessary variables in GitLab CI/CD settings:
    • Sonar URL
    • Sonar Login
    • Sonar Password
  • Example .gitlab-ci.yml file content:
    stages:  
      - build  
      - test  
    build:  
      script:  
        - mvn package  
    sonar:  
      script:  
        - mvn verify sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD  
    

Conclusion

  • Run the pipeline and check the SonarQube dashboard for the project analysis.
  • Summary of the integration process for the Java Maven project with SonarQube using GitLab CI.
  • Encouragement to like, share, and subscribe to the DevOpsTrend channel.