Wednesday, July 3, 2024
HomeLanguagesAnalyze Java code using Gradle in SonarQube and Jenkins

Analyze Java code using Gradle in SonarQube and Jenkins

Gradle is an open-source build automation tool that is designed to be flexible enough to build almost any type of software. Source: Gradle Documentation. A build automation tool is basically used to automate the creation of applications.

High-level overview of its most important features:

  • High performance
  • JVM foundation: Gradle runs on the JVM and you must have a Java Development Kit (JDK) installed to use it.
  • Conventions: Gradle takes a leaf out of Maven’s book and makes common types of projects — such as Java projects — easy to build by implementing conventions.
  • IDE support
  • Insight: Build scans provide extensive information about a build run that you can use to identify build issues.

Setup Pre-requisites

The following are needed for the guide to be successful

  • Installed and running SonarQube
  • Installed and running Jenkins Server
  • gradlew available in the root of your code
  • SonarQube plugin installed in Jenkins

Step 1: Add sonarqube plugin in your build.gradle file

Open up your “build.gradle” file in the root of your sources and add the sonarqube plugin lines as follows

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

Step 2: Add Analysis with Gradle stage in your Jenkinsfile

In this step, we are going to add a stage in our Jenkinsfile that will inform Jenkins that we should analyze the code using Gradle in SonarQube tool. Add the following stage in your Jenkinsfile.

        stage ('Scan using Gradle') {
            steps {
                withSonarQubeEnv(installationName: 'SonarQubeScanner', credentialsId: 'SonarQubeSecret') {
                sh "./gradlew sonarqube \
                  -Dsonar.projectKey=${serviceName} \
                  -Dsonar.host.url=${env.SONAR_HOST_URL} \
                  -Dsonar.login=${env.SONAR_AUTH_TOKEN} \
                  -Dsonar.projectName=${serviceName} \
                  -Dsonar.projectVersion=${BUILD_NUMBER}"
                }
             }
        }

As you can see, gradle will run with “sonarqube” task that is provided by the plugin we added in “build.gradle” file in Step 1. We are also using the “withSonarQubeEnv” wrapper as well as environment variables such as “${serviceName}” which you can declare in your Jenkinsfile environment section/block.

Step 3: Build the pipeline

In this step, we are going to build or trigger Jenkins to build the pipeline and we hope that it will execute the Scanner for Gradle from the server. The results should be as shared in the screenshot below once all of the stages have been executed successfully by Jenkins. You should see the SonarQube icon where you can click and get your results right inside Jenkins.

jenkins gradle scan icon to click

Clicking the link will redirect you to SonarQube where you will be able to view your results as illustrated below

sonarqube gradle results 1

Step 4: Add your other stages in Jenkinsfile

After scanning, we believe it is time to build and package your application into a jar file or any other format you prefer. A stage similar to the one below can suffice. But you will notice that gradle application installed under “/usr/local/gradle/” is used here and the “withGradle” wrapper around the command. We will cover the installation of gradle and gradle plugin in Jenkins soon so that this will be clearer. You should find your jar file inside “/build/libs/” directory.

        stage('Build App'){
            steps {
         withEnv(["PATH=/usr/local/gradle/bin"]) { 
                withGradle {                 
                 sh "gradle  fatJar --no-daemon"
               } 
             }
           }
        }

Concluding Remarks

We hope that your Java application will be finally analyzed and built using fatJar gradle task. If everything went well on your end, then we are extremely happy. Better ways of accomplishing this is welcome. Finally, we continue to that all of your support and uplifting comments. It means a lot.

Other guides you might enjoy include:

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments