SDKs overview
SonarSource, the creator of SonarQube, SonarCloud, and SonarLint, provides various software development kits (SDKs) and client libraries designed to integrate its code quality and security analysis tools into existing development environments and CI/CD pipelines. These SDKs allow developers to automate the process of initiating code scans, retrieving analysis results, and managing quality gate statuses directly from their applications or build scripts. The goal is to embed continuous code inspection seamlessly into the software development lifecycle, enabling early detection of bugs, vulnerabilities, and code smells.
While SonarSource primarily offers scanner wrappers and API clients that interact with the SonarQube and SonarCloud platforms, these tools function as SDKs, providing structured access to the underlying analysis capabilities. For instance, the SonarScanner for Maven or Gradle integrates directly with common build systems to initiate scans, while the Web API clients allow for more granular control over the platform via HTTP requests. This architecture supports a wide range of programming languages and build tools, reflecting the diverse ecosystems in which Sonar products operate.
For detailed information on the full capabilities and integration patterns, developers can reference the official SonarScanners documentation, which provides comprehensive guides for each scanner flavor.
Official SDKs by language
SonarSource provides official scanners and client libraries that support integration across various programming languages and build systems. These tools are designed to streamline the process of submitting code for analysis to SonarQube or SonarCloud and retrieving the results. The primary official SDKs are typically referred to as 'scanners' because their main function is to prepare and send source code to the Sonar analysis engine.
Below is a table summarizing some of the key official scanners and their primary use cases:
| Language/Ecosystem | Package/Tool | Primary Use | Installation Command Example | Maturity |
|---|---|---|---|---|
| Java/Maven | SonarScanner for Maven | Integrates with Maven builds | mvn org.sonarsource.scanner.maven:sonar-maven-plugin:sonar |
Stable |
| Java/Gradle | SonarScanner for Gradle | Integrates with Gradle builds | Add plugin to build.gradle |
Stable |
| .NET/C# | SonarScanner for .NET (MSBuild) | Integrates with .NET/MSBuild projects | dotnet tool install --global dotnet-sonarscanner |
Stable |
| JavaScript/TypeScript | SonarScanner for Linter-based analysis (e.g., ESLint) | Integrates with JS/TS projects | npm install sonar-scanner --save-dev |
Stable |
| Generic CLI | SonarScanner CLI | For projects without specific build tools | Download and extract from SonarScanner CLI download page | Stable |
| Azure DevOps | SonarQube/SonarCloud Extension | Integrates with Azure DevOps pipelines | Install from Azure DevOps Marketplace | Stable |
Each scanner is specifically optimized for its respective ecosystem, providing native integration points and leveraging existing build configurations. For instance, the Maven and Gradle scanners automatically detect project structures and dependencies, simplifying the setup process for Java applications. The .NET scanner hooks into the MSBuild process, enabling analysis directly within Visual Studio solutions. The SonarScanner CLI offers a flexible option for environments not covered by specific build integrations, requiring manual configuration via a sonar-project.properties file.
Beyond these scanners, SonarQube and SonarCloud expose a comprehensive Web API that allows for custom integrations and automation. This RESTful API enables developers to programmatically fetch project analysis status, manage quality gates, retrieve issues, and perform administrative tasks. While not a traditional SDK, interacting with this API directly using standard HTTP client libraries in any language (e.g., Python's requests, Node.js's axios) acts as a form of custom SDK development.
Installation
The installation process varies depending on the specific SonarScanner or integration method chosen. Generally, installation involves either adding a dependency to a project's build file, installing a global command-line tool, or downloading and configuring a standalone executable.
SonarScanner for Maven
- Ensure Maven is installed and configured.
- Add the SonarQube plugin to your project's
pom.xmlfile within the<build>><plugins>section. Alternatively, you can run it directly via the command line without modifying thepom.xmlfor a one-off scan. - Example
pom.xmlsnippet for plugin declaration:<plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.9.1.2184</version> <!-- Use the latest version --> </plugin> - Run the scan:
mvn sonar:sonar -Dsonar.projectKey=my-project -Dsonar.host.url=http://localhost:9000 -Dsonar.token=YOUR_TOKEN
SonarScanner for .NET
- Install the .NET Core global tool:
dotnet tool install --global dotnet-sonarscanner --version 5.10.0(check for latest .NET scanner version). - Navigate to your solution directory.
- Begin analysis:
dotnet sonarscanner begin /k:"my-project" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="YOUR_TOKEN" - Build your project:
dotnet build - End analysis:
dotnet sonarscanner end /d:sonar.login="YOUR_TOKEN"
SonarScanner CLI
- Download the appropriate SonarScanner CLI distribution from the SonarSource Scanner CLI documentation.
- Extract the downloaded archive (e.g.,
sonar-scanner-cli-<VERSION>.zip) to a directory. - Add the
bindirectory of the extracted folder to your system's PATH environment variable. - Create a
sonar-project.propertiesfile in the root of your project with configuration details (e.g.,sonar.projectKey,sonar.sources). - Run the scan from your project directory:
sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.token=YOUR_TOKEN
Quickstart example
This quickstart demonstrates how to run a basic scan on a Java Maven project using the SonarScanner for Maven, targeting a local SonarQube instance. This example assumes you have Maven installed and a SonarQube server running on http://localhost:9000 with an authenticated user token.
Prerequisites:
- Java Development Kit (JDK) installed.
- Apache Maven installed and configured.
- A running SonarQube instance (e.g., local Docker container or direct installation).
- A SonarQube user token generated from your SonarQube profile (e.g.,
YOUR_TOKEN).
Steps:
-
Create a sample Java Maven project:
If you don't have one, create a simple project using Maven's archetype generation:
mvn archetype:generate \ -DgroupId=com.mycompany.app \ -DartifactId=my-java-app \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DarchetypeVersion=1.4 \ -DinteractiveMode=falseNavigate into the newly created project directory:
cd my-java-app -
Execute the Sonar scan:
Run the Maven command to perform the SonarQube analysis. Replace
YOUR_TOKENwith your actual SonarQube user token and adjustsonar.host.urlif your SonarQube instance is not atlocalhost:9000.mvn clean verify sonar:sonar \ -Dsonar.projectKey=my-java-app \ -Dsonar.host.url=http://localhost:9000 \ -Dsonar.login=YOUR_TOKENThis command performs the following actions:
clean: Cleans the project build directory.verify: Executes any integration tests and checks the results.sonar:sonar: Invokes the SonarScanner Maven plugin to analyze the code.-Dsonar.projectKey=my-java-app: Sets a unique key for your project in SonarQube.-Dsonar.host.url=http://localhost:9000: Specifies the URL of your SonarQube server.-Dsonar.login=YOUR_TOKEN: Provides the authentication token for SonarQube.
-
Review results:
After the command completes, a message similar to "ANALYSIS SUCCESSFUL" will appear in the console, along with a link to the project dashboard on your SonarQube instance (e.g.,
http://localhost:9000/dashboard?id=my-java-app). You can then navigate to this URL in your web browser to view the detailed code analysis results, including bugs, vulnerabilities, code smells, and technical debt metrics.This quickstart provides a foundational understanding of how to initiate a scan. For more complex projects, additional configurations may be necessary within the
pom.xmlorsonar-project.propertiesfile, such as specifying excluded files, test coverage reports, or custom rulesets. The SonarQube Getting Started documentation offers further guidance on advanced configurations.
Community libraries
While SonarSource maintains official scanners and API documentation for direct integration, the open-source nature of SonarQube has fostered a community that develops additional tools, plugins, and client libraries. These community-contributed resources often extend Sonar's capabilities or provide more idiomatic integrations for specific languages or frameworks where an official scanner might not offer the desired level of abstraction.
Community libraries frequently emerge to simplify tasks such as:
- Custom rule development: Plugins that allow developers to define and implement their own static analysis rules beyond those provided by SonarSource, often for niche domain-specific languages or coding standards.
- Reporting and visualization: Tools that extract data from SonarQube's API to generate custom reports, dashboards, or visualizations that are tailored to specific organizational needs, potentially integrating with business intelligence tools.
- CI/CD pipeline helpers: Scripts or small libraries that wrap SonarScanner commands with additional logic, error handling, or dynamic configuration generation to fit complex CI/CD environments (e.g., Jenkins Shared Libraries, GitHub Actions custom actions not directly published by SonarSource).
- Language-specific API clients: While the SonarQube Web API is language-agnostic, community members may develop client libraries in languages like Python or Node.js to provide a more object-oriented or fluent interface for interacting with the API, abstracting away raw HTTP requests. For example, a Python library might offer functions like
sonar_client.get_project_issues('my-project'). - Integration with other tools: Connectors or adapters to non-mainstream version control systems, issue trackers, or other developer tools that don't have first-party SonarSource support.
Discovering community libraries typically involves exploring marketplaces, such as the SonarSource Marketplace for SonarQube extensions, GitHub repositories, or package managers specific to programming languages (e.g., npm for Node.js, PyPI for Python). It is crucial for developers to evaluate the maintenance status, documentation quality, and community support for any third-party library before incorporating it into production workflows. Although powerful, community tools may not always align with the latest SonarQube versions or best practices as quickly as official solutions. Developers often consult community forums or project maintainers for compatibility and support details. As an example of a related tool that integrates with API-focused development, one might consider how a tool like Kong Konnect could manage traffic to an API that itself consumes SonarQube data.