Saturday, April 14, 2018

SonarQube



1- Introduction
SonarQube is a continuous inspection tool. SonarQube provides the capability to not only show health of an application but also to highlight issues newly introduced. With a Quality Gate in place, you can fix the leak and therefore improve code quality systematically.

2- Version
SonarQube 6.7.3

3- Installation
Following is instruction-set to get setup with SonarQube:

1- Use given below link to download SonarQube 6.7.3
     https://www.sonarqube.org/downloads/

2- Unzip the downloaded file.

3- Go to <install_directory>/bin folder. You would find different folders related with OS platforms. As I have 64 bit Win system, I further went inside “windows-x86-64″ folder.

4- You shall find different shell scripts files. For my win platform, I found different *.bat file such as InstallNTService.bat, StartNTService.bat, StartSonar.bat etc.

5- Copy the path of this folder which may look like S<install_director>/bin/Windows-x86-64 (in case of my laptop), and append it to “Path” environment variable.

6- Open a command prompt, type “StartSonar” command and execute. This would start a web server at default port of 9000.

7- Open a web browser and access the page, http://localhost:9000.

4- Installation of Code Analyzer
Lets move further to analyze out HelloWorld project. I coded a HelloWorld Java project using Eclipse IDE.
Following is instruction-set to get setup with Code Analyzer (SonarQube Scanner). The SonarQube Scanner is recommended as the default launcher to analyze a project with SonarQube.

1- Use given below link to download SonarQube Scanner 3.1
     https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

2- Unzip the file to appropriate location.

3- Setup the SONAR_RUNNER_HOME environment variable and assign it to SonarRunner installation directory.

4- Add the <install_directory>/bin directory to your path.

5- Check the basic installation by opening a new shell and executing the command sonar-scanner -h (on Windows platform the command is sonar-scanner.bat -h).

6- If above get executed, you are all set to analyze your first project with SonarQube runner.

5- Analysis of HelloWorld Java Project
1- Create a configuration file in the root directory of the project, namely, sonar-project.properties.

2- Sample file is given below:
File Name- sonar-project.properties
File Contents-
sonar.projectKey=my:helloWorld
sonar.projectName=My project
sonar.projectVersion=1.0
sonar.sources=.
sonar.sources=src/main/java/
sonar.language=java
sonar.java.binaries=target/classes

3- Build the project before start analyze the source code.
    Command- mvn clean compile

4- Open a command prompt, and go to project root folder.

5- Execute “sonar-scanner” command to run the analysis. You would see the analysis run.

6- Goto the browser and access the page, http://localhost:9000.

7- You would find your project listed under “PROJECTS”. Click on your project listing and you would land up on the project dashboard.

6- Analysis of HelloWorld Java Project using (Eclipse, maven)
1- Add the given below maven dependency in pom.xml file.
    <dependency>
<groupId>org.sonarsource.scanner.maven</groupId>
         <artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
   </dependency>

2- Add the given below profile configuration into pom.xml file.
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
</profiles>

3- Right click on project -> Run As -> Maven build...
    Configuration and lunch screen will display. In side Goals text box type the given below command and click on Run button.
    clean compile sonar:sonar

4- Goto the browser and access the page, http://localhost:9000.

5- You would find your project listed under “PROJECTS”. Click on your project listing and you would land up on the project dashboard.










Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...