The following documentation has been written for Sonar versions >= 2.3. |
There are two ways to extend coding rules :
- Extend an existing Sonar plugin. For example Checkstyle and PMD plugins accept definition of custom checks.
- Embed and execute a code analyzer. For example the Checkstyle plugin configures and executes the library Checkstyle.
Solution 1: Extending Sonar plugins
The following plugins can be extended with new rules :
- Checkstyle
- PMD
- C, freeware plugin provided BySonarSource
- Cobol, commercial plugin provided by SonarSource
Checkstyle (Java)
Checkstyle provides extension mechanisms to develop your own coding rules. Tutorials to write such custom rules are available online. You can for instance define your own naming conventions, forbid access to a given API or anything else that is relevant in your context.
Once written, the Checkstyle classes must be packaged in a JAR file. This file must be copied in the directory $SONAR_HOME/extensions/rules/checkstyle/. A XML file must then be created in the same directory to "index" all the new rules. The name of this XML file doesn't matter but the .xml suffix must be used. This XML file must look like the following example :
PMD (Java)
C
See the SonarSource documentation.
Cobol
See the SonarSource documentation.
Solution 2: Executing a code analyzer
A code analyzer plugin executes the following steps :
- Register definitions of coding rules, when the server is started.
- Optionally define some templates of quality profiles, when the server is started.
- Analyze source code and inject results in database
1. Registering coding rules
This step relates to the extension point org.sonar.api.rules.RuleRepository.
Defining quality profiles
This step relates to the extension point org.sonar.api.profiles.RulesProfile.
Analyzing source code
This step relates to the extension point org.sonar.api.batch.Sensor.

