Every software starts with a good documentation and good code. Whenever a team starts a project are there different minds working on the same thing and sometimes this leads to differences in coding standards. To overcome this problem there are many standalone tools, which can go through your code and depending on configured rules in that tool. It gives lines of code which under coding rules violation.

There are many code quality tools in java such as,

Actually, sonar is the combination of all these, That’s the reason I prefer Sonar over the individual tool. Moreover, it has code coverage plugin as well Cobertura which is quite decent.

Steps to install Sonar and Sonar-Scanner.

Install required version of JRE.

https://docs.sonarqube.org/display/SONAR/Requirements

https://www.sonarqube.org/downloads/

Read licensing and stuff (Completely optional, The current version is licensed under LGPL v3 )

https://softwareengineering.stackexchange.com/questions/47323/can-i-use-an-lgpl-licenced-library-in-my-commercial-app

Install Sonar Scanner (It’s a replacement for Sonar runner). If you are using maven and  gradle then it’s not required.

https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

Extract both the zips on one location as shown below.

Let Sonar-scanner know  Sonarqube URL (Optional, If you haven’t updated the port or URL).

sonar-scanner\conf\sonar-scanner.properties –> Update the sonar.host.url Add Sonar-scanner\bin path to your system and verify by running sonar-scanner.bat -h in the console.

Spin up the Sonarqube server by running StartSonar.bat from Sonarqube bin directory and verify if it has come properly or not by going to the browser.

localhost:9000 click tutorial and make a note of the generated key.

Create sonar-project.properties in the root of the project which you want to scan.

sonar-project.properties

# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=My project
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=.
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

Run solar-scanner command from the root of your project.

In case you are not using maven or gradle you might face the following issue.

Just add a follwoing line in sonar-project.properties.

sonar.java.binaries=<replace with your binaries path. bin***.class> #path of your class files.

Once you get ‘Execution Success’. Revisit your localhost:9000.

That’s setup is complete, You can browse through the site and find out the easy.