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:sonar without 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."

All Security Tools questions Roadmap: SonarQube — code quality & SAST

More on SonarQube