I have created a sample Maven project and run sonar analysis from Jenkins. Also, I have used the Web API to assign the QualityGate.
You can use the below Jenkinsfile
as an example, to do sonar analysis.
Jenkinsfile
pipeline {
agent any
tools {
maven 'MAVEN_HOME1'
}
stages {
stage('Git') {
steps {
git credentialsId: 'gitlab-test', url: 'https://example.com/gitlab/repo1/simple-java-maven-app.git'
}
}
stage('Maven Install') {
steps {
sh "mvn install"
}
}
stage('Create Sonar Proejct') {
steps {
sh 'curl -X POST -u "admin:admin" "https://example.com/sonarqube/api/projects/create?name=stackoverflow&project=stackoverflow"'
}
}
stage('Set Quality Gate') {
steps {
sh 'curl -u "admin:admin" -X POST "https://example.com/sonarqube/api/qualitygates/select?projectKey=stackoverflow&gateId=10100"'
}
}
stage('Sonarqube Analysis') {
steps {
sh """mvn -U install sonar:sonar -Dsonar.host.url=https://example.com/sonarqube/ -Dsonar.login=7yha3f47967iuednd8cd -Dsonar.projectKey=stackoverflow -Dsonar.projectName=stackoverflow -Dsonar.sources=. -Dsonar.java.binaries=**/* -Dsonar.language=java -Dsonar.exclusions=src/test/java/com/mycompany/app/AppTest.java"""
}
}
}
}
Please find below the SonarQube Analysis Result and other screenshots, for your reference.
Screenshots:
Jenkins Console Output:
List of Available QualityGate:
Note: In the above image, “id”:10040,”name”:”SonarQube way” is the default QualityGate. I have used “id”:10100,”name”:”SASSonarQube way” for setting Quality Gate to analyze the project
stackoverflow
using Web API. All are marked inyellow
SonarQube Analysis
In above image, you can see the Quality Gate
SASSonarQube way
has been used to do sonar analysis. Marked inyellow
CLICK HERE to find out more related problems solutions.