Sonarqube - sandeepvalapi/DevOps GitHub Wiki
SonarQube (formerly Sonar[1]) is an open source platform for continuous inspection of code quality. Sonar can be used for monitoring code quality for any kind of project like Java, .Net or Mobile applications.
-
Ubuntu OS with Administrator access
-
MySql database (Optional)
-
Keep the Linux Mac OS ready
-
Follow below link for installing Java
https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get
- Install MySQL using below link
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-14-04
- Install Sonar:
http://dev.mamikon.net/installing-sonarqube-on-ubuntu/
- Default username and password for Sonar: admin/admin
While creating maven build using mvn sonar:sonar, we need to enable mysql as public access.
Helpful link:
Question: Failed to connect database cannot create pool connection factory
Answer: http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql
Maven Build using Sonar:
MVN –e –X –Dsonar.host.url = http://localhost:9000/sonar sonar: sonar
(Replace localhost with appropriate sonar server location)
Windows:
-
Download sonar from https://www.sonarqube.org/downloads/
-
Unzip downloaded sonar package into any of the folder
-
Sonar comes with embedded SQL which can be used for testing sample applications.
-
We will see how to configure Sonar with Mysql
-
Install MySql and workbench and login through command prompt using root credentials
-
Perform below steps:
http://dev.mamikon.net/installing-sonarqube-on-ubuntu/
-
Above link will create database and a user with respected privileges.
-
Update sonar properties in sonar unzip folder to point to Mysql and database credentials.
-
Run sonar qube server by running StartSonar.bat file
-
By default sonar runs on http://localhost:9000/
-
Login using default sonar credentials (admin/admin)
-
Settings in Ant Build file
-
Run ant sonar, if build is successful you will be able to see code quality report in sonar dashboard
-
Edit properties and make project name as unique among multiple projects
<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple Project analyzed with the SonarQube Scanner for Ant" default="all" basedir="."
xmlns:sonar="antlib:org.sonar.ant">
<!-- ========= Sonar properties for this project ========= -->
<property name="src.dir" value="src" />
<property name="build.dir" value="target" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="sonar.projectKey" value="org.sonarqube:sonarqube-scanner-ant-sample" />
<property name="sonar.projectName" value="Ant_Date_Util" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="src" />
<property name="sonar.binaries" value="target" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<!-- ========= Define "regular" targets: clean, compile, ... ========= -->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" fork="true" debug="true" includeAntRuntime="false" />
</target>
<!-- ========= Define SonarQube Scanner for Ant Target ========= -->
<target name="sonar" depends="compile">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib"
folder -->
<classpath path="C:/Users/svalapi/Downloads/sonar-ant-task-2.2.jar" />
</taskdef>
<!-- Execute SonarQube Scanner for Ant Analysis -->
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>
</target>
<!-- ========= The main target "all" ========= -->
<target name="all" depends="clean,compile,sonar" />
</project>
The above code snippet is used for configuring ant based project to run sonar. Modify the project name property accordingly.
-
Change inbound rules to point to 3306 - mysql
-
Update IP tables using commands to dport port 3306
-
Update /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/my.cnf file to change bind address to 0.0.0.0 from 127.0.0.1
-
restart mysql
https://www.youtube.com/watch?v=aHuQExY360I
- Update settings.xml with below configuration
In Plugin groups :
org.sonarsource.scanner.maven
Settings in profiles xml file :
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://sonardocker.technology:9000/sonar</sonar.host.url>
</properties>
</profile>
- Add dependencies in maven and run mvn clean install -U and then mvn sonar:sonar
<dependency>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</dependency>
Note : Do not use codehaus plugin or dependencies. Instead use org.sonarscanner plugins
-
Download SonarQube Server and unzip and start server.
-
Access newly started server.
-
Download sonar-runner 2.4 and unzip in different location.
-
In project, create sonar-runner.properties file with below configuration.
sonar.projectName=Sample_Training
sonar.projectVersion=1.0
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
sonar.sources=src
sonar.language=java
sonar.java.binaries=src
- For Mac users, open terminal and navigate to project path and run below command for maven users:
/Users/svalapi/Downloads/sonar-runner-2.4/bin/sonar-runner -X -Dproject.settings=sonar-runner.properties
- For Project structure or reference ref below project
https://github.com/sandeepvalapi/Sample_Training
- Helpful Links
-
http://tanaguru-sonar-plugin.readthedocs.io/en/latest/run-with-sonar-runner/
-
https://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode
-
Download Sonarqube 6.7 and Sonar Scanner 3.0
-
Extract the zip
-
Copy sonar properties file from sonar scanner to your project folder
-
To scan your source code, Run using below command:
#Use below two lines for making build fast
build.parallel=true
installed.tenants=
sonar.language=java
sonar.projectName=<Project name>
sonar.projectKey=<unique project key>
sonar.projectVersion=1.1
sonar.sourceEncoding=UTF-8
sonar.excludedExtensions=<Extensions which we dont want to pick for sonar tests>
#sonar.inclusions=<Projects which you want to include>
#for SonarQube = 6.4
sonar.host.url=http://localhost:9000
#sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
Below are the steps for integration sonar with hybris system
Sonar Server Setup:
-
Download Sonarqube 6.4 from https://www.sonarqube.org/
-
Unzip downloaded Sonarqube
-
Go to /sonarqube-6.4/bin/macosx-universal-64 or windows-x86-32/64 based on your Operating System.
-
Run ./sonar.sh start or (StartSonar.bat for windows)
-
Modify the sonar port to run on 19000 port (default port is 9000)
Update the sonar property in sonar.properties(({localdirectorypath}\sonarqube-6.4\sonarqube-6.4\conf)
sonar.web.port=19000
Add below property in sonar.properties
sonar.search.port=9006
-
Access http://localhost:19000
-
Default username and password is admin/admin
-
Click on Quality Profiles and navigate to Create->Restore Profile button as shown below:
-
Navigate to {HYBRIS_ROOT}/build-tools/sonarqube/ and import the relevant profile (e.g. java-hybris-profile.xml)
-
Make sure it is set as the default Java profile.
Add below entries in local.properties file
sonar.language=java
sonar.projectName=Project Name
sonar.projectKey=Key
sonar.projectVersion=1.0-SNAPSHOT
sonar.sourceEncoding=UTF-8
sonar.excludedExtensions=yemptypackage,acceleratorserviceshmc,bmecat,bmecathmc,classificationsystems,cmsfacades,commerceservices,commerceserviceshmc,commercewebservicescommons,oci,acceleratorcmshmc,acceleratorcms,acceleratorfacades,acceleratorservices,acceleratorstorefrontcommons,addonsupport,captchaaddon,commercefacades
sonar.exclusions=file:/gensrc/,/com/hybris/,/com/bazaarvoice/,/de/hybris/,/YEmptyConstants.java,/YEmptySystemSetup.java
sonar.host.url=http://localhost:19000
Jacoco Setup For Sonar Code Coverage:
-
Place the attached JAR file(download it externally) inside \hybris\bin\ext-accelerator\acceleratorservices\lib
-
Modify the line as below in \hybris\bin\ext-accelerator\acceleratorservices\resources\acceleratorservices\ant\acc-ant-sonar.xml
- Add below properties in local.properties
sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=${platformhome}/../../log/junit
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=${platformhome}/../../log/junit/jacoco.exec
- Run below ant task
ant jacocoalltests -Dtestclasses.extensions="commercewebservices,commercecore,b2bstorefront,commercefacades,commercestorefront"
- Run “ant sonar”
Once build is successful, you can verify the project quality report at http://localhost:19000/projects
SonarLint Setup in Eclipse:
Download sonarlint from eclipse marketplace and restart eclipse
-
Add this in platform build.xml.
-
change the package highlighted.
-
Run “ant testall”
-
Access hybris/log/junit/index.html
<target name="testall" description="executes only specific tests I provide">
<callback extname="" target="before_testall"/>
<annotationtests packages="com.packagename.*" annotations="unittests,demotests,integrationtests" web="false" />
<callback extname="" target="after_testall"/>
</target>
##For Generating PDF report in sonar follow below process(Yet to try if it works or not)
https://www.youtube.com/watch?v=Jtyyfp_28o8
Steps to Configure Sonarqube for Salesforce SFRA projects Checkout here