Setup SonarQube For Static Analysis
- Sachin Pathare
- Apr 7, 2017
- 3 min read
What is SoanarQube :
SonarQube is an open source platform used to do static analysis of code. mostly it is used by development team to manage source code quality.
It is used to write clean code, detect bugs , also it supports multiple languages for code analyzer e.g Java, Python, C++, PHP, Swift, Flex, Objective-c ...etc
It is a one place which gives overview and shared vision of code quality for developers, leads, managers and project owner. We can integrate it with CI.
SonarQube Configuration or Setup :
Download Sonar Qube from https://www.sonarqube.org/downloads/ and Sonar Runner https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner.
Unzip both the zip files.
For Windows :
1. Go to the unziped sonarqube folder and run StartSonar.bar
C:\sonarqube\bin\windows-x86-xx\StartSonar.bat
2. Then open a localhost:9000 in browser you will see the sonarqube dashboard with 0 projects. because until we have't configured/scan any project.
3. Go to the sonar-scanner folder copy the path and set an environment variable
sonar_scanner and path up home dir
C:\sonar-scanner
and include in path %sonar_scanner%\bin.
4. Go to the your project dir and run
C:\sonar-scanner\bin\sonar-scanner.bat
It will shows successfull after completion if no error occures.
After completion you can browse the results at http://localhost:9000
For Linux flavors :
1. Unzip the sonarqube and sonar-scanner at /opt or /usr/share.
2. Set the sonarqube and sonar-runner environment variable
e.g suppose you have extracted the zip files at /opt
vim .bash_profile and add following entries.
export SONARQUBE_HOME=/opt/sonarqube-6.8 export PATH=${PATH}:${SONARQUBE_HOME}/bin
export SONAR_RUNNER_HOME=/opt/sonar-runner-2.4 export PATH=${PATH}:${SONAR_RUNNER_HOME}/bin
3. Then check the env variables
echo $SONARQUBE_HOME echo $SONAR_RUNNNER_HOME
Start sonarqube Server
Go to the sonarqube6.2/bin/your_sys_name_archtecture
e.g /sonarqube-6.2/bin/linux-x86-64
./sonar.sh console or ./sonar.sh start.
4. Then open a localhost:9000 in browser you will see the sonarqube dashboard with 0 projects. because until we have't configured/scan any project.
then go to the your project and run the scanner
sonar-scanner.
If it runs wihout error then it will show successfull at the end/after completion.
After completion you can browse the results at http://localhost:9000
(Default system admin credentials are admin/admin)
Database setup for production instance :
Install a mysql server or any other like sql/oracle/postgre sql.
We are using mysql server
1. mysql -u root -p (enter password after prompt)
2. show database;
3. Then create database schema for sonar :
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci
4. Then create user sonar . CREATE USER 'sonar' IDENTIFIED BY 'Sonar@123';
5. Grant a access/give all permission to sonar user
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'Sonar@123';
6. On which machine your setup sonar if locally use localhost if any use its address instead if localhost. GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'Sonar@123';
7. mysql> FLUSH PRIVILEGES;
Then do configuration for sonarqube and sonar-runner
1. provide sonar database user credaentials in
vim /opt/sonarqube-6.3/conf/sonar.properties
Uncomment those two lines in properties file and provide credentials.
sonar.jdbc.username=sonar sonar.jdbc.password=Sonar@123
2. After that according to which database your using uncomment those database section in properties file.
e.g i am using my sql so i uncommented mysql section.
#----- MySQL 5.6 or greater # Only InnoDB storage engine is supported (not myISAM). # Only the bundled driver is supported. It can not be changed. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
(localhost if your database is locally other wise its address in above url)
3. Then configure sonar-runner/scanner
vim /opt/sonar-scanner/sonar-scanner.properties
Inside this file you can configure sonar server url sonar.host.url=http://localhost:9000
and uncomment database url sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
/opt/sonarqube-6.2/bin/your_os_bit/sonar.sh console
e.g /opt/sonarqube-6.2/bin/linux-x86-64/sonar.sh console sonar.projectKey=transtats sonar.projectName=transtats sonar.projectVersion=0.0.1 sonar.sources = /home/spathare/automation/transtats
Note : Instead of local host please use your hostname/address if your database/server instance is on different machine.
Comments