SonarScanner CLI
The SonarScanner CLI is a code analysis scanner for CI/CD pipelines, used when no build-system-specific scanner is available. Distinct from the SonarQube CLI, a developer management interface.
The SonarScanner CLI is the scanner to use when there is no specific scanner for your build system. It supports ARM architecture for macOS and Linux.
Important: The SonarScanner CLI doesn't support C# or VB.NET analysis. To analyze C# or VB.NET projects, use the Introduction instead.
Prerequisites
Java 21 or later, Java 17 has been removed. See Java runtime environment (JRE) for more details.
With JRE auto-provisioning:
Java 11 or later from SonarScanner CLI version 7.2
Java 17 or later before SonarScanner CLI version 7.2
See also General requirements and Managing JRE auto-provisioning for more details.
Configuring your project
Create a configuration file in your project’s root directory called sonar-project.properties.
Running SonarScanner CLI from the zip file
To run SonarScanner CLI from the zip file, follow these steps:
Expand the downloaded file into the directory of your choice. We’ll refer to it as
<INSTALL_DIRECTORY>in the next steps.Add the
<INSTALL_DIRECTORY>/bindirectory to your path.Verify your installation by opening a new shell and executing the command
sonar-scanner -h. You should get an output like this:usage: sonar-scanner [options]Options:-D,--define <arg> Define property-h,--help Display help information-v,--version Display version information-X,--debug Produce execution debug outputIf you need more debug information, you can add one of the following to your command line:
-X,--verbose, or-Dsonar.verbose=true.Run the following command from the project base directory to launch analysis:
Alternatively, you can use environment variables to define the token (
SONAR_TOKEN) and the server base URL (SONAR_HOST_URL).For more information, see Analysis parameters and Managing your tokens.
Expand the downloaded file into the directory of your choice. We’ll refer to it as
<INSTALL_DIRECTORY>in the next steps.Add the
<INSTALL_DIRECTORY>\bindirectory to your path.Verify your installation by opening a new shell and executing the command
sonar-scanner -h, orsonar-scanner.bat -hon Windows. You should get an output like this:usage: sonar-scanner [options]Options:-D,--define <arg> Define property-h,--help Display help information-v,--version Display version information-X,--debug Produce execution debug outputIf you need more debug information, you can add one of the following to your command line:
-X,--verbose, or-Dsonar.verbose=true.Run the following command from the project base directory to launch analysis:
Alternatively, you can use environment variables to define the token (
SONAR_TOKEN) and the server base URL (SONAR_HOST_URL).For more information, see Analysis parameters and Managing your tokens.
Running SonarScanner CLI from the Docker image
To scan using the SonarScanner CLI Docker image, use the following command:
Use a sonar-project.properties file to configure other analysis parameters: see Configuring your project above.
Sample projects
To help you get started, simple project samples are available for most languages on GitHub. They can be browsed or downloaded. You’ll find them filed under sonarqube-scanner/src.
Alternatives to sonar-project.properties
If a sonar-project.properties file cannot be created in the root directory of the project, there are several alternatives:
The properties can be specified directly through the command line. Example:
sonar-scanner -Dsonar.projectKey=myproject -Dsonar.sources=src1The root folder of the project to analyze can be set through the
sonar.projectBaseDirproperty. This folder may contain asonar-project.propertiesfile. Additional analysis parameters can be defined in this project configuration file or through command-line parameters.The property
project.settingscan be used to specify the path to the project configuration file. In this case, anysonar-project.propertieslocated at the root of the project base directory is ignored. Example:sonar-scanner -Dproject.settings=../myproject.properties
In all cases, sonar.projectKey is mandatory and must be defined either in the project configuration file or on the command line.
Important:
project.settingsis resolved relative to the directory you run the scanner in, not relative tosonar.projectBaseDir. To control where the scanner looks forsonar-project.properties, passsonar.projectBaseDiras a command-line argument. Settingsonar.projectBaseDirinsidesonar-project.propertiesresolves too late to influence the file's location.
Alternate analysis directory
If the files to be analyzed are not in the directory where the analysis starts from, use the sonar.projectBaseDir property to move analysis to a different directory. E.g. analysis begins from jenkins/jobs/myjob/workspace but the files to be analyzed are in ftpdrop/cobol/project1. This is configured in sonar-project.properties as follows:
Advanced configuration
If your SonarQube Community Build instance is secured
If your SonarQube Community Build instance is secured behind a proxy and a self-signed certificate, you must add the self-signed certificate to the trusted CA certificates of the SonarScanner. In addition, if mutual TLS is used, you must define the access to the client certificate at the SonarScanner level.
See Reverse proxy and network security andTLS certificates on client side for more information.
Troubleshooting
Java heap space error or java.lang.OutOfMemoryError
Increase the memory via the SONAR_SCANNER_JAVA_OPTS environment variable (from SonarScanner CLI version 6.0, or SONAR_SCANNER_OPTS, otherwise) when running the scanner from a zip file:
In Windows environments, avoid the double quotes, since they get misinterpreted, and combine the two parameters into a single one.
"java" cannot be opened because the developer cannot be verified
The SonarScanner CLI is not yet Apple verified therefore, when using the macOS AArch64 version, you may get an OS security window displaying this message. A solution us to run:
Unsupported major.minor version
Install the last version of SonarScanner CLI (from version 6.0, no JRE installation is required). Otherwise, upgrade the version of Java being used for analysis or use one of the core packages that embed its own Java runtime.
Property missing: ‘sonar.cs.analyzer.projectOutPaths’. No protobuf files will be loaded for this project.
The SonarScanner CLI is not able to analyze .NET projects. Please, use the Introduction. If you are running the SonarScanner for .NET, ensure that you are not hitting a known limitation.
Last updated
Was this helpful?

