Skip to content

SonarQube

Run SonarQube locally in Docker and analyse a Maven project with the SonarScanner for Maven.

3 min readUpdated

SonarQube is a static analysis server: it collects bugs, code smells, security issues and test coverage for every analysis of a project. The SonarScanner for Maven runs the analysis as a Maven goal and uploads the results to the server.

This note starts SonarQube Community Build in Docker, configures the scanner in pom.xml and settings.xml, and runs an analysis.

Start SonarQube in Docker

Add a sonarqube service to your Compose file. The image stores its embedded database and Elasticsearch indexes in /opt/sonarqube/data, plugins in /opt/sonarqube/extensions and logs in /opt/sonarqube/logs; named volumes keep them across restarts:

docker-compose.yml
services:
sonarqube:
image: sonarqube:community
ulimits:
nofile:
soft: "65536"
hard: "65536"
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
ports:
- "9000:9000"
volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:

SonarQube embeds Elasticsearch, so the host must allow enough memory maps and open files. On a Linux host, set them for the current session as root:

Terminal window
sysctl -w vm.max_map_count=524288
sysctl -w fs.file-max=131072

Start the container:

Terminal window
docker compose up -d

The embedded H2 database is fine for local use but not for production; the image documentation explains how to connect an external database.

Create a project and a token

  1. Open http://localhost:9000, the value of sonar.host.url below.
  2. Log in with the default administrator account, user admin and password admin. SonarQube asks you to change the password on first login.
  3. Create a project manually: enter its display name (PROJECT_NAME), key (PROJECT_KEY) and main branch name.
  4. Generate a token for the analysis (TOKEN) and copy it into settings.xml below.

Configure Maven

Declare the scanner plugin in the project’s pom.xml so that every developer and CI job uses the same version:

pom.xml
<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>5.1.0.4751</version>
</plugin>
</plugins>
</build>

This note originally used version 3.10.0.2594; 5.1.0.4751 is the latest release on Maven Central at the time of the update.

Keep the server address and the token out of the project, in a profile in ~/.m2/settings.xml. activeByDefault turns the profile on for every build:

~/.m2/settings.xml
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.projectKey>[PROJECT_KEY]</sonar.projectKey>
<sonar.projectName>[PROJECT_NAME]</sonar.projectName>
<sonar.host.url>http://localhost:9000</sonar.host.url>
<sonar.token>[TOKEN]</sonar.token>
<sonar.exclusions>**/*.py,**/*.css,**/*.scss,**/*.js,**/*.xhtml</sonar.exclusions>
</properties>
</profile>
  • sonar.projectKey and sonar.projectName must match the project created in SonarQube. If you omit them, the scanner uses groupId:artifactId and the project name from the POM.
  • sonar.token authenticates the analysis. It replaces the older sonar.login. You can also pass it as -Dsonar.token=... or in the SONAR_TOKEN environment variable.
  • sonar.exclusions lists file patterns to leave out of the analysis, here Python, stylesheets, JavaScript and JSF pages.

Other properties that were useful in some projects:

Property Purpose
sonar.organization Organization key, required on SonarQube Cloud (SonarCloud)
sonar.sourceEncoding Source file encoding, for example UTF-8
sonar.branch.name Branch to analyse (not available in the Community Build)
sonar.sources Source directories, for example src/main/java
sonar.scm.provider SCM used for blame information, for example git
sonar.skip true skips the analysis for a module

Run an analysis

Build the project and run the analysis in one command. verify runs the tests first, so coverage reports are available to the scanner:

Terminal window
mvn clean verify sonar:sonar

The results appear in the project dashboard at http://localhost:9000 when the build finishes. The SonarSource documentation uses the fully qualified goal, which works without declaring the plugin in the POM:

Terminal window
mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar

Stop SonarQube

Stop and remove the container. -v also deletes the named volumes, and with them all analyses; leave it out to keep the data:

Terminal window
docker compose down -v

References

Related notes

2 min read

ngrok

Expose a local web server on a public HTTPS address with ngrok, for webhook testing, demos and testing on real devices.

  • Developer Tools
2 min read

NetBeans

Build Apache NetBeans from source, clear the Recent Projects list and choose the JDK the IDE runs on.

  • Developer Tools
3 min read

Vim and Neovim

Set up Vim with VimTeX and Zathura for LaTeX on macOS, and add the Neo-tree file explorer to Neovim.

  • Developer Tools

Type to search the whole site.