Code Analysis in CI/CD
Using Sonar Scanner:
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= \
Using Maven Plugin
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= \
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"
See Also
SonarQube™ is a trademark belonging to SonarSource SA. For further information, please visit www.sonarqube.org.
Last updated
Was this helpful?

