SDKs overview
SonarQube's approach to code analysis relies on a suite of language-specific scanners rather than a single universal SDK. These scanners are specialized tools designed to interpret code written in particular programming languages and frameworks, extract relevant metrics, and identify potential issues such as bugs, vulnerabilities, and code smells. The results are then transmitted to a central SonarQube server for aggregation, reporting, and visualization. This distributed model allows SonarQube to support a broad spectrum of languages and integrate seamlessly into diverse build environments and continuous integration/continuous delivery (CI/CD) pipelines.
The core components include the SonarScanner CLI, which is a generic runner for analysis, and specific scanners tailored for different ecosystems, such as SonarScanner for Maven, SonarScanner for Gradle, and SonarScanner for .NET. These tools are typically invoked as part of a project's build process, analyzing the source code and sending the analysis report to the configured SonarQube server instance. The server then processes these reports, applies predefined quality gates and profiles, and displays the results through its web interface.
Official SDKs by language
SonarQube provides official support for a wide array of programming languages through its dedicated scanners and plugins. These scanners are developed and maintained by SonarSource to ensure compatibility with the latest SonarQube server versions and to provide comprehensive analysis capabilities for each language. The table below outlines some of the key official scanners, their typical installation methods, and their maturity levels.
| Language | Scanner/Package | Installation Method (Example) | Maturity |
|---|---|---|---|
| Java | SonarScanner for Maven, SonarScanner for Gradle | mvn sonar:sonar, gradle sonarRunner |
Stable |
| C# / VB.NET | SonarScanner for .NET, SonarQube Extension for Azure DevOps | dotnet sonarscanner begin /k:"myproject" |
Stable |
| JavaScript / TypeScript | SonarScanner CLI | Integrated with build systems (e.g., npm scripts) | Stable |
| Python | SonarScanner CLI | sonar-scanner (via command line) |
Stable |
| C / C++ | SonarScanner for C/C++/Objective-C | Integrated with build tools (e.g., CMake, Make) | Stable |
| Go | SonarScanner CLI | sonar-scanner (via command line) |
Stable |
| PHP | SonarScanner CLI | sonar-scanner (via command line) |
Stable |
| Kotlin | SonarScanner for Gradle | gradle sonarRunner |
Stable |
| Ruby | SonarScanner CLI | sonar-scanner (via command line) |
Stable |
| Swift | SonarScanner CLI | sonar-scanner (via command line) |
Stable |
Each scanner is designed to understand the specific syntax, semantics, and common pitfalls of its target language, allowing for highly accurate and relevant analysis results. For a comprehensive list of supported languages and their specific analysis requirements, refer to the official SonarQube documentation.
Installation
The installation process for SonarQube scanners typically involves two main steps: setting up the SonarQube server and then configuring the appropriate scanner(s) for your development environment. The server component, which stores analysis results and provides the web interface, can be installed on various platforms, including Docker, Kubernetes, or directly on Linux/Windows servers.
Once the server is operational, the installation of language-specific scanners varies depending on the build system and programming language. For instance:
- SonarScanner CLI: This is a standalone command-line tool. You typically download the distribution, extract it, and add its
bindirectory to your system's PATH environment variable. - Maven Projects: The SonarScanner for Maven is available as a Maven plugin. You configure it in your project's
pom.xmlfile, and Maven handles the dependency management. - Gradle Projects: Similar to Maven, the SonarScanner for Gradle is a Gradle plugin. You apply the plugin in your
build.gradlefile. - .NET Projects: The SonarScanner for .NET is distributed as a NuGet package and integrates with the
dotnet CLI. - Other Languages (e.g., JavaScript, Python, Go): For many languages, the analysis is performed by the generic SonarScanner CLI, which is configured to point to your project's source code.
Configuration usually involves specifying the SonarQube server URL and a project key. These parameters can be set via command-line arguments, environment variables, or configuration files (e.g., sonar-project.properties). For CI/CD environments, these configurations are often managed by the pipeline scripts, ensuring automated analysis upon every build or commit. Developers can choose to integrate SonarQube analyses at various points in their workflow, from local pre-commit hooks to nightly builds, depending on their desired feedback loop speed and resource availability.
Quickstart example
This example demonstrates how to perform a basic SonarQube analysis using the SonarScanner CLI for a generic project. Before running this, ensure you have a SonarQube server running and accessible, and the SonarScanner CLI installed and added to your system's PATH. You will also need a SonarQube authentication token if your server requires authentication.
# 1. Navigate to your project's root directory
cd /path/to/your/project
# 2. Create a sonar-project.properties file in the root directory
# This file tells the scanner how to analyze your project.
# Replace <YOUR_PROJECT_KEY>, <YOUR_PROJECT_NAME>, <YOUR_PROJECT_VERSION> as needed.
# Adjust 'sonar.sources' to point to your source code directory (e.g., src, app).
echo "sonar.projectKey=<YOUR_PROJECT_KEY>" > sonar-project.properties
echo "sonar.projectName=<YOUR_PROJECT_NAME>" >> sonar-project.properties
echo "sonar.projectVersion=<YOUR_PROJECT_VERSION>" >> sonar-project.properties
echo "sonar.sources=." >> sonar-project.properties
echo "sonar.host.url=http://localhost:9000" >> sonar-project.properties # Adjust if your server URL is different
# If authentication is required, add your token:
# echo "sonar.login=<YOUR_SONARQUBE_TOKEN>" >> sonar-project.properties
# 3. Run the SonarScanner CLI
# This command executes the analysis and sends results to the SonarQube server.
sonar-scanner
# Expected output will show logs about the analysis progress and eventually success.
# Example success message: 'ANALYSIS SUCCESSFUL'
# 4. View results in SonarQube UI
# Open your browser to the SonarQube server URL (e.g., http://localhost:9000)
# and navigate to your project dashboard using the project key.
For projects built with Maven, Gradle, or .NET, the commands are integrated directly into the build system. For example, a Maven project can be analyzed by running mvn clean verify sonar:sonar after configuring the plugin in pom.xml, as detailed in the SonarQube Maven documentation.
Community libraries
While SonarSource provides official scanners for a wide range of languages, the open-source nature of SonarQube also fosters a vibrant community that develops additional plugins, rules, and integrations. These community libraries can extend SonarQube's capabilities beyond the official offerings, supporting niche languages, specific coding standards, or integrating with other development tools. These contributions are often found on platforms like GitHub or the SonarQube plugin marketplace.
Examples of community-driven extensions include:
- Language Plugins: Community members may develop plugins for languages not officially supported by SonarSource, or offer alternative implementations with different rule sets.
- Custom Rules: Developers can write custom rules to enforce project-specific coding guidelines or detect particular patterns that are not covered by the default SonarQube quality profiles. These rules are typically packaged within SonarQube plugins.
- Integration Libraries: Although SonarQube itself focuses on analysis, community efforts might include libraries that help integrate SonarQube analysis results into other dashboards, reporting tools, or custom CI/CD workflows. For example, some tools might consume the SonarQube Web API to build custom reports.
When considering community libraries, it is advisable to evaluate their maintenance status, compatibility with your SonarQube server version, and the reputation of their developers. The SonarQube community forum and official documentation often provide guidance on discovering and utilizing these valuable extensions. For a broader understanding of how static analysis tools operate, resources like the Microsoft documentation on code analysis offer general insights into the principles behind such tools, which are applicable to SonarQube's underlying mechanisms.