Sonarqube - sandeepvalapi/DevOps GitHub Wiki

About Sonar

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.

Sonar flow

List of softwares required for running sonar(Linux /Mac)

  • Ubuntu OS with Administrator access

  • MySql database (Optional)

Installing Sonar on Ubuntu

  1. Keep the Linux Mac OS ready

  2. Follow below link for installing Java

https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get

  1. Install MySQL using below link

https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-14-04

  1. Install Sonar:

http://dev.mamikon.net/installing-sonarqube-on-ubuntu/

  1. Default username and password for Sonar: admin/admin

Observations and Faq:

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)


Sonar configuration for Ant based projects

Windows:

  1. Download sonar from https://www.sonarqube.org/downloads/

  2. Unzip downloaded sonar package into any of the folder

  3. Sonar comes with embedded SQL which can be used for testing sample applications.

  4. We will see how to configure Sonar with Mysql

  5. Install MySql and workbench and login through command prompt using root credentials

  6. Perform below steps:

http://dev.mamikon.net/installing-sonarqube-on-ubuntu/

  1. Above link will create database and a user with respected privileges.

  2. Update sonar properties in sonar unzip folder to point to Mysql and database credentials.

  3. Run sonar qube server by running StartSonar.bat file

  4. By default sonar runs on http://localhost:9000/

  5. Login using default sonar credentials (admin/admin)

  6. Settings in Ant Build file

  7. Run ant sonar, if build is successful you will be able to see code quality report in sonar dashboard

  8. 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.


For configuring 3306 port on EC2

  • 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

Helpful Links

https://www.youtube.com/watch?v=aHuQExY360I

Sonar + Maven configuration

  • 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

Configure Sonar without modifying or touching code

  1. Download SonarQube Server and unzip and start server.

  2. Access newly started server.

  3. Download sonar-runner 2.4 and unzip in different location.

  4. 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

  1. 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

  1. For Project structure or reference ref below project

https://github.com/sandeepvalapi/Sample_Training

  1. Helpful Links

Configure Sonar for Hybris 6.7 with Sonar-Scanner

  • 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

Setting up sonar with jacoco for Hybris projects

Below are the steps for integration sonar with hybris system

Sonar Server Setup:

  1. Download Sonarqube 6.4 from https://www.sonarqube.org/

  2. Unzip downloaded Sonarqube

  3. Go to /sonarqube-6.4/bin/macosx-universal-64 or windows-x86-32/64 based on your Operating System.

  4. Run ./sonar.sh start or (StartSonar.bat for windows)

  5. 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

  6. Access http://localhost:19000

  7. Default username and password is admin/admin

  8. Click on Quality Profiles and navigate to Create->Restore Profile button as shown below:

  9. Navigate to {HYBRIS_ROOT}/build-tools/sonarqube/ and import the relevant profile (e.g. java-hybris-profile.xml)

  10. Make sure it is set as the default Java profile.

Steps to update in Hybris:

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:

  1. Place the attached JAR file(download it externally) inside \hybris\bin\ext-accelerator\acceleratorservices\lib

  2. Modify the line as below in \hybris\bin\ext-accelerator\acceleratorservices\resources\acceleratorservices\ant\acc-ant-sonar.xml

  1. 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

  1. Run below ant task

ant jacocoalltests -Dtestclasses.extensions="commercewebservices,commercecore,b2bstorefront,commercefacades,commercestorefront"

  1. 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

Steps from other implementation

  • 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

⚠️ **GitHub.com Fallback** ⚠️