Tuesday, November 10, 2020

Create Sonar Plugin - SonarQube and Sonar Scanner

 There are various ways to create a Sonar plugin(SonarQube Plugin). In this series of article I am going to explain the development of Sonar Plugin using Java.

Before going forward, let's look  at what is a Sonar ? What is Sonar Plugin and what it does ?

What is Sonar or SonarQube ?

SonarQube (formerly Sonar) is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages. SonarQube offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments, bugs, and security vulnerabilities.

https://en.wikipedia.org/wiki/SonarQube

https://www.sonarqube.org/

You can see lots of new terms like bugs, code smells ...etc. Do not worry. You do not need to know all these things in deep. But just have an idea. It will make your life easy and you will be easily understand what you want to do. :)

By the time you will familiar with SonarQube, you will realize that SonarQube is a Dashboard.

Below is the sample dashboard with a one project "MyProject" set up locally.

http://localhost:9000/projects





Great If you understand that, you are in good shape.

So if this is the Dashboard, then how the data comes to this dashboard ?

If you asked yourself ? you are Brilliant.

Yes, we need to push data to dashboard (SonarQube)

That's done using Sonar Scanner or Sonar CLI.

Sonar has lot's of tools. But basically these two types - you need to understand as a basic step to create Sonar Plugin.

Let's look at Scanner


Sonar Scanner

SonarScanner is a separate client type application that in connection with the SonarQube server will run project analysis and then send the results to the SonarQube server to process it. SonarScanner can handle most programming languages supported by SonarQube

Below is the link , how you config and run the Sonar Scanner

https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/


So I believe, you have some idea about SonarQube (DashBoard) and SonarScanner ( Analysis tool)

Why  I say Analysis tool for Sonar Scanner is , Actual Analysis happens when you run the scanner.


So In simple, When you want to do and some analysis on your project source code 

  1. Run the anaysis invoking sonar-scanner
  2. sonar-scanner will generate the report and upload to SonarQube - (Dashboard)


This is the simplest explanation.

Now you have some idea about SonarQube and SonarScanner.

Then let's see how the plugin actually works.

How Sonar Plugin works

All SonarQube plugins are installed or reside inside the "Plugins" directory inside SonarQube.

  • Below is a sample path where "SonarQube" version7.1 is installed in the "Software" directory.
  • C:\Software\sonarqube-7.1\extensions\plugins
As i told you above, Next you need to invoke Sonar Scanner via command line.
  • type "sonar-scanner" and enter
  • your scan will run and report will be upload to SonarQube



At the end, you can see the log where link to SonarQube with your Project. In here i have done a scan on a project call "MyProject". The sample log will be as follows


INFO: CPD calculation finished

INFO: Analysis report generated in 186ms, dir size=27 KB

INFO: Analysis reports compressed in 29ms, zip size=9 KB

INFO: Analysis report uploaded in 165ms

INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/MyProject

INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report

INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AXWtaKEPKlLJW_efck3G

INFO: Task total time: 11.976 s

INFO: ------------------------------------------------------------------------

INFO: EXECUTION SUCCESS

INFO: ------------------------------------------------------------------------

INFO: Total time: 16.182s

INFO: Final Memory: 16M/240M

INFO: ------------------------------------------------------------------------



 


No comments:

Post a Comment