Code Analysis in CICD
Using Sonar Scanner:
Before analyzing the source code using [SonarScanner™](https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/), make sure you have
Downloaded and installed
[SonarScanner™](https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/)CLI plugin. Refer to Installing Sonar ScannerFollow the instructions here to generate security token
For on-premises instances, please use your organization-specific service URL instead of https://analyzer.integralzone.com
There are 2 ways in which the projects can be analyzed.
By creating and adding required properties in sonar-project.properties file in the parent directory of the project.
By directly passing all the properties as command line arguments
Steps outlined below uses the command line approach
Go to the project root directory from command line/terminal
Run
sonar-scannercommand with following options-Dsonar.projectKey=xxx Unique key for the project being uploaded
-Dsonar.projectName=xxx Diplay name of the project being uploaded
-Dsonar.projectVersion=x.x Version of the project being uploaded
-Dsonar.sources=. Source code directory
-Dsonar.login=xxx Security token generated from the server. This parameter is deprecated from SonarQube version > 10.x, use sonar.token instead
-Dsonar.token=xxx Security token generated from the server. This parameter is applicable from SonarQube version > 10.x, for versions < 10.x use sonar.login parameter
-Dsonar.organization=xxx + Optional. Applicable only for cloud version
-Dsonar.branch.name=xxx + Optional. SCM branch for which code is being analysed. Eg: -Dsonar.branch.name=master
Complete example may look like
Using IZ Analyzer cloud instance PROJECT_ROOT_DIR> sonar-scanner -Dsonar.projectKey=xxx -Dsonar.organization= -Dsonar.sources=. -Dsonar.exclusions=target -Dsonar.host.url=https://analyzer.integralzone.com -Dsonar.login= \
Using IZ Analyzer on-prem instance PROJECT_ROOT_DIR> sonar-scanner # -Dsonar.projectKey=xxx -Dsonar.projectName=xxx -Dsonar.sources=. -Dsonar.host.url= -Dsonar.login= \
For complete reference of all the properties that can be used with sonar-scanner, refer to Analysis Parameters
Using Maven Plugin
Before analyzing the source code using Maven Plugin, make sure you have:
Downloaded and installed
Apache Maven. Refer to Installing Apache MavenFollow the instructions here to generate security token
Go to the project root directory from command line/terminal
Run
mvn sonar:sonarcommand with following options-Dsonar.projectKey=xxx Unique key for the project being uploaded
-Dsonar.projectName=xxx Diplay name of the project being uploaded
-Dsonar.projectVersion=x.x Version of the project being uploaded
-Dsonar.sources=. Source code directory
-Dsonar.login=xxx Security token generated from the server. This parameter is deprecated from SonarQube version > 10.x, use sonar.token instead
-Dsonar.token=xxx Security token generated from the server. This parameter is applicable from SonarQube version > 10.x, for versions < 10.x use sonar.login parameter
-Dsonar.organization=xxx + Optional. Applicable only for cloud version
-Dsonar.branch.name=xxx + Optional. SCM branch for which code is being analysed. Eg: -Dsonar.branch.name=master
Complete example may look like
Using IZ Analyzer cloud instance PROJECT_ROOT_DIR> mvn sonar:sonar -Dsonar.projectKey=xxx -Dsonar.organization= -Dsonar.exclusions=target -Dsonar.sources=. -Dsonar.host.url=https://analyzer.integralzone.com -Dsonar.login=
Using IZ Analyzer on-prem instance PROJECT_ROOT_DIR> mvn sonar:sonar -Dsonar.projectKey=xxx -Dsonar.projectName=xxx -Dsonar.sources=. -Dsonar.host.url= -Dsonar.login= \
Follow the instructions from Scanner for Maven for addition information about configuring the plugin
Setting Proxy Details
If the system from which the projects are analyzed using sonar-scanner or maven is configured with proxy, then set the following environment variable with proxy server details before running the respective commands
Windows > set SONAR_SCANNER_OPTS="-Dhttps.proxyHost=PROXY_HOST -Dhttps.proxyPort=PROXY_PORT -Dhttp.proxyHost=PROXY_HOST -Dhttp.proxyPort=PROXY_PORT -Djava.net.useSystemProxies=true"
Linux > export SONAR_SCANNER_OPTS="-Dhttps.proxyHost=PROXY_HOST -Dhttps.proxyPort=PROXY_PORT -Dhttp.proxyHost=PROXY_HOST -Dhttp.proxyPort=PROXY_PORT -Djava.net.useSystemProxies=true"
Replace
PROXY_HOSTandPROXY_PORTwith appropriate values for Proxy server host and portIf
https.proxyPortis not specified, default value will be 443If
http.proxyPortis not specified, default value will be 80
See Also
SonarQube™ is a trademark belonging to SonarSource SA. For further information, please visit www.sonarqube.org.
Last updated
Was this helpful?

