A plugin is a Set of Extensions
A SonarQube TM plugin is a set of Java objects that implement extension points. These extension points are interfaces or abstract classes which model an aspect of the system and define contracts of what needs to be implemented. They can be for example pages in the web application or sensors generating measures.
<artifactId>sonar-foo-plugin</artifactId> <packaging>sonar-plugin</packaging> <build> <plugins> <plugin> <groupId>org.codehaus.sonar</groupId> <artifactId>sonar-packaging-maven-plugin</artifactId> <version>1.4<7</version> <extensions>true</extensions> <configuration> <pluginClass>com.mycompany.sonar.MyPlugin</pluginClass> </configuration> </plugin> </plugins> </build>
The sonar-plugin packaging accepts advanced parameters.
Here are the available extension points An extension point is a point in the Sonar plugin API. Please note that partial implementation of these extensions are available in the API as well. See all known sub-interfaces and implementing classes of org.sonar.api.Extension to get an exhaustive list.
CPD definition to detect duplicated lines on a new language
Used to decorate resources, i.e. calculate measures from other measures. Example: the number of lines of code of a package is the sum of the lines of its classes.
Define a new language.
Define a Maven2 plugin to execute before analyzing the project.
List new Metrics. A metric is an instance of org.sonar.api.measure.Metric. It can be quantitative (lines of code, number of rule violations) or qualitative (code coverage, complexity by class, rules compliance). As a rule, a metric is used to store a number but it can also be used to store a text field with list of key/value pairs, an XML or even binary data.
Post-jobs are tasks executed at the end of project analysis. Examples: purge database, generate report, send emails, ...
Job to delete data from database.
enables to ignore certain resources. The only implementations is the exclusion pattern management. (ExcludedResourceFilter in the core plugin).
Analyze project. It creates resources (classes, packages, ...) and measures. It can make requests on other systems. See below.
Enables to ignore violations. Has a very simple interface: public boolean isIgnored(Violation violation). A NoSonarFilter draft has been added to the Squid plugin.
Batch task executed before all other tasks (sensors, decorators, post-jobs)
Filter to exclude some files from analysis
Handle batch lifecycle events (sensor is started, stopped, ...)
Change project structure before analysis
Image generated by JFreeChart.
Extend the library sonar-colorizer to support new languages. By default only Java sources are colorized in SonarQubeTM.
Static HTML footer displayed in all pages.
Define a new language.
Add a page, linked from the left sidebar. The page is implemented as a GWT component. GWT RPC/servlets are not supported.
New view when displaying source code. It is used to add a new tab to the source viewer along with "Violations", "Sources", "Coverage", ... The new tab is implemented as a GWT component.
Add a page, linked from the left sidebar. The page is implemented as a JRuby on Rails view.
Experimental. Used to define new web services, written in JRuby on Rails.
New widget in project dashboards. It is implements as a JRuby on Rails template.
Define a new repository of rules, i.e. a list of coding rules to publish into the global referential
Programmatically define a Quality profile
Import a Quality profile, generally used when end-users create a profile by uploading a file
Export a Quality profile in a specific format, for example Checkstyle configuration format
Define a new Quality model, like SQALE
Delegates authentication of users to external system
Handle server startup notification
Handle server shutdown notification
Channel to publish notifications. An example is the Email channel.
Provides logic to determine which users are interested in receiving notification. Example: notify me when when someone changes review assigned to me or created by me.
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 http://docs.sonarsource.org/latest/apidocs/index.html?org/sonar/api/Extension.html.
Sensor or Decorator?
There are two extensions extension points that enables allow plugins to save measures: Sensor and Decorator. A common problem when writing a plugin is to decide which one to use.
You should never directly instantiate classes which implements implement BatchComponent or ServerComponent , because they should be retrieved as an IoC dependency. Otherwise it leads to issues with backward compatibility.
The sonar-dev-maven-plugin allows to lets you start a SonarQube TM server and to deploy the your plugin.
Once the server is launched, hit http://localhost:9000. By default, the in-process database (H2, or Derby prior to SonarQube TM 3.2) is used but you can specify to use 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) who which must have all rights on the sonar schema.
Changes on Java code are not reloaded dynamically. You need to rebuild the plugin and to re-execute sonar-dev-maven-plugin.
Debug Java batch
mvnDebug sonar:sonar and attach your IDE to the remote process. Example in Intellij Idea: Run > Edit configurations > Add new configuration > Remote.
We've gathered all our debugging tips into this page.
How to Use External Libraries?
A plugin benefits from all the dependencies provided by the API. Execute the following command on your plugin to list them: