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:
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:
sysctl -w vm.max_map_count=524288sysctl -w fs.file-max=131072Start the container:
docker compose up -dThe 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
- Open
http://localhost:9000, the value ofsonar.host.urlbelow. - Log in with the default administrator account, user
adminand passwordadmin. SonarQube asks you to change the password on first login. - Create a project manually: enter its display name (
PROJECT_NAME), key (PROJECT_KEY) and main branch name. - Generate a token for the analysis (
TOKEN) and copy it intosettings.xmlbelow.
Configure Maven
Declare the scanner plugin in the project’s pom.xml so that every developer and CI job uses the same version:
<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:
<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.projectKeyandsonar.projectNamemust match the project created in SonarQube. If you omit them, the scanner usesgroupId:artifactIdand the project name from the POM.sonar.tokenauthenticates the analysis. It replaces the oldersonar.login. You can also pass it as-Dsonar.token=...or in theSONAR_TOKENenvironment variable.sonar.exclusionslists 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:
mvn clean verify sonar:sonarThe 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:
mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonarStop SonarQube
Stop and remove the container. -v also deletes the named volumes, and with them all analyses; leave it out to keep the data:
docker compose down -v