How do you run SonarQube analysis for a Maven project?
BeginnerHands-onSonarQube
Answer
Short answer
Run mvn verify sonar:sonar with the server URL, project key and token. Run it after tests (verify) so the JaCoCo coverage report exists and SonarQube can show coverage.
Example
<!-- pom.xml: generate coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution><goals><goal>prepare-agent</goal></goals></execution>
<execution><id>report</id><phase>verify</phase><goals><goal>report</goal></goals></execution>
</executions>
</plugin>
mvn -B clean verify sonar:sonar \
-Dsonar.host.url=https://sonar.company.com \
-Dsonar.projectKey=orders-service \
-Dsonar.token=$SONAR_TOKEN
SonarQube finds target/site/jacoco/jacoco.xml automatically.
Common mistakes
- Running
sonar:sonarwithout tests → 0% coverage on the dashboard. - Passing the token in plain text in the Jenkinsfile — use credentials.
What to say in the interview
"mvn clean verify sonar:sonar with the URL, project key and a token from CI credentials. Verify runs tests and JaCoCo first, so the coverage XML is picked up and the quality gate can include coverage on new code."
More on SonarQube
What is SonarQube and how do you integrate it into a pipeline?BeginnerWhat does SonarQube analyse?BeginnerHow do you run SonarQube locally with Docker?BeginnerHow do you scan a project with sonar-scanner?BeginnerWhat is a quality gate in SonarQube?BeginnerHow do you make a Jenkins pipeline fail when the SonarQube quality gate fails?Intermediate