Skip to end of metadata
Go to start of metadata

Table of Contents

A plugin is a Set of Extensions




The sonar-plugin packaging accepts advanced parameters.

Extension Points

An extension point is a point in the application where plugin code can be invoked, such as webapp page or code analyzer. Extension points are generally interfaces that can be implemented by plugins. Implementations have to be declared in the method org.sonar.api.SonarPlugin#getExtensions() and are then injected in the IoC container.

The extension points are listed and documented in the Javadoc. See

Sensor or Decorator?

There are two extension points that allow plugins to save measures: Sensor and Decorator. A common problem when writing a plugin is to decide which one to use.


A Sensor is invoked once during the analysis of a project. The sensor can parse a flat file, connect to a web server... For example the Cobertura Sensor is able to parse the cobertura XML report file generated during execution of your tests and used to save the first-level of measures on resources (project, package or class).


Decorators are triggered once all sensors have completed. Their decorate method is called on every resource of a certain level bottom up. Decorators can read and save measures. The call is contextual, i.e it is only possible to access the resource and its children.
Decorators are generally used to consolidate / aggregate at higher levels, measures saved by Sensors at the lowest level.

How to Reuse Existing Components

Extensions are registered in an IoC container, with constructor injection. To communicate with other extensions or with existing components provided by the API, just declare them in the constructor of your extension. For example to get references on DatabaseSession or RulesProfile:

References will be automatically set at runtime.



You should never directly instantiate classes which implement BatchComponent or ServerComponent , because they should be retrieved as an IoC dependency. Otherwise it leads to issues with backward compatibility.

How to Quickly Start the Plugin


Changes on Java code

The sonar-dev-maven-plugin lets you start a SonarQube server and deploy your plugin. 

Once the server is launched, hit http://localhost:9000. By default, the in-process database (H2, or Derby prior to SonarQube 3.2) is used but you can specify a local MySQL instance instead with the property "-Dsonar.database=mysql". In that case, the sonar schema must exist in the MySQL DB along with the user sonar/sonar (login/password) which must have all rights on the sonar schema.

Then you can feed the database by inspecting projects:

Note for Mac


Add the property to the mvn command.





We've gathered all our debugging tips into this page.


How to Use External Libraries



Constraints are:

  • Libraries used for GWT compilation must be defined with scope provided
    2. There are side-effects on some libraries, for example the classloader must be explicitly set for XStream:

How to Log


Read the SLF4J manual for more details.

How to Get Configuration




Steps to cover a new language

  1. Write the grammar. This is the hardest part.
  2. Write a parser (a parser simply takes a grammar, an input, and will parse it, yielding a parse tree).
  3. Test your grammar, to ensure it is able to parse your real-life language files.
  4. Write a few parse tree visitors. Some visitors will compute metrics, while others will enforce coding rules. A dozen or so visitors is sufficient for an initial release.
  5. Write a SonarQube sensor to launch the visitors. It should query the API to get the list of source files, the list of active coding rules in the quality profile, and the API to save metrics and issues.
See also: SSLR.

Tips and Tricks

  • For generating charts use d3.js, which is packaged with SonarQube since version 4.1.
  • To create "template" rules (rules that can be duplicated by the user) set the rule's cardinality field to MULTIPLE
  • To access a constant from a Java class in a .erb file, use Java::OrgSonarPluginsPlug::MyConfiguration::MY_CONSTANT 



  • No labels