Thursday, 21 April 2016

Install and setup SonarQube on Windows ?


A. Setup database for SonarQube
1. Install MySQL

2. Setup MySQL using below commands or script
# Create SonarQube database and user.
# Command: mysql -u root -p < create-sonar-db.sql
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;

Ensure that MySQL instance is running and able to connect database “sonar” through the SQL client.
All the tables would be created automatically after the sonar ant task would run.


B. Install and configure SonarQube
Install SonarQube


Configure SonarQube server
Set Sonar configuration (sonarqube/conf/sonar.properties) : 

a. Update JDBC properties 
sonar.jdbc.url=jdbc:mysql://<host>:3306/sonar
sonar.jdbc.username=root 
sonar.jdbc.password=root 
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver

b. Update Sonar server properties sonar.host.url=http://<host>:8090 
sonar.web.host=<yourhost> 
sonar.web.port=8090 
sonar.web.context=/

c. Start the Sonar server 
Go to  sonarqube-4.0\bin and select your operating system (Eg: windows-x86-64 for window 7) and start Sonar server.  
Example (Windows)
cd C:\E_Drive_Backup\jenkins_backfile\sonarqube-4.0\bin\windows-x86-64 
Run the StartSonar.bat file. 

Example (Linux)
/home/sonarqube-4.0/bin/linux-x86-64/sonar.sh start | stop | status

d. Check sonar logs 
tail -f /home/sonarqube-4.0/logs/sonar.log




e. Check the sonar process 
Sample
root   51589 51587 18 12:06 ?        00:01:08 /opt/jdksun64/1.7.0_11/bin/java -Djava.awt.headless=true -XX:MaxPermSize=128m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 -Djruby.management.enabled=false -Xms256m -Xmx512m -Djava.library.path=./lib -classpath ../../lib/sonar-application-4.0.jar:../../lib/tomcat-embed-core-7.0.42.jar:../../lib/tomcat-embed-jasper-7.0.42.jar:../../lib/logback-classic-1.0.13.jar:../../lib/tomcat-embed-logging-juli-7.0.42.jar:../../lib/slf4j-api-1.7.5.jar:../../lib/wrapper-3.2.3.jar:../../lib/logback-core-1.0.13.jar:../../lib/jul-to-slf4j-1.7.5.jar:../../lib/commons-io-2.4.jar:../../lib/logback-access-1.0.13.jar:../../conf:../../extensions/jdbc-driver/h2/h2-1.3.172.jar:../../extensions/jdbc-driver/mysql/mysql-connector-java-5.1.26.jar:../../extensions/jdbc-driver/postgresql/postgresql-9.1-901-1.jdbc4.jar:../../extensions/jdbc-driver/mssql/jtds-1.2.7.jar -Dwrapper.key=XvBBAc9aNgEiKSZJ -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=51587 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wrapper -Dwrapper.service=TRUE -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1 org.tanukisoftware.wrapper.WrapperSimpleApp org.sonar.application.StartServer



C. Sonar Scanner installation 
1. Download Sonar scanner from :    http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Scanner 
   Extract the downloaded file into any directory <install_directory>

2. Update the global settings (Server URL) by editing <install_directory>/conf/sonar-runner.properties : 
  #----- Default SonarQube server
  #sonar.host.url=http://localhost:9000

3. Set environment variables    a. SONAR_RUNNER_HOME set to <install_directory>
    b. Add the <install_directory>/bin directory to PATH

4. Check the installation in a new shell / window
Linux
sonar-runner -h
sonar-runner -h -Dsonar.verbose=true

Windows
sonar-runner.bat -h
sonar-runner.bat -h -Dsonar.verbose=true

 
D. Sonar Scanner configuration and launch
 
1. Set Sonar runner properties
Add Mysql and Sonar web server configuration in sonar-scanner/conf/sonar-runner.properties
Example
# Configure here general information about the environment, such as SonarQube DB details for example
# No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://10.239.199.191:8090/
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8


 
2. Change PROJECT_HOME in sonar-runner.batset PROJECT_HOME=%SONAR_RUNNER_HOME%\Projects

3. Make "Projects" folder in %SONAR_RUNNER_HOME% directory
 
4. Create a configuration file "sonar-project.properties" in the root directory of the project: 
Example
# must be unique in a given SonarQube instance
sonar.projectKey=MyTool
# Name displayed in the SonarQube UI
sonar.projectName=MyTool
 Analysis
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file.
# Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.

sonar.sources=.
# For Example 
# sonar.sources=C:/workspace/G3R6C1/admin/src/main/java 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
# Language JAVA
sonar.language=java


5. Launch Sonar runner from project base directory
sonar-runner


Note : Scanner does directory indexing and Indexing is only done from where “sonar-runner” command is executed.


 
Check SonarQube Dashboard
Check sonarqube dashboard on sonarqube server UI.
There will be “MyTool Analysis” report.
Double click on that link and view and analyze the code quality of project.
 
Example

No comments:

Post a Comment

Note: only a member of this blog may post a comment.