Package org.sonar.api

Interface Plugin


  • public interface Plugin
    Entry-point for plugins to inject extensions into SonarQube.

    The JAR manifest must declare the name of the implementation class in the property Plugin-Class. This property is automatically set by sonar-packaging-maven-plugin when building plugin.

    Example of implementation

     public class MyPlugin implements Plugin {
      @Override
       public void define(Context context) {
         context.addExtensions(MySensor.class, MyRules.class);
         if (context.getRuntime().getApiVersion().isGreaterThanOrEqual(Version.create(6, 0))) {
           // Extension which supports only versions 6.0 and greater
           // See org.sonar.api.SonarRuntime for more details.
           context.addExtension(MyNewExtension.class);
         }
       }
     }
     

    Example of pom.xml

     <project>
       ...
       <packaging>sonar-plugin</packaging>
    
       <build>
         <plugins>
           <plugin>
             <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
             <artifactId>sonar-packaging-maven-plugin</artifactId>
             <extensions>true</extensions>
             <configuration>
               <pluginClass>com.mycompany.sonarqube.MyPlugin</pluginClass>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </project>
     

    Example of Test Add a test dependency to sonar-plugin-api-impl to have access to implementation classes in tests.

    @Test
     public void test_plugin_extensions_compatible_with_5_6() {
       SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(5, 6), SonarQubeSide.SCANNER);
       Plugin.Context context = new PluginContextImpl.Builder().setSonarRuntime(runtime).build();
       MyPlugin underTest = new MyPlugin();
    
       underTest.define(context);
    
       assertThat(context.getExtensions()).hasSize(4);
     }
     
    Since:
    5.5
    See Also:
    for unit tests
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Plugin.Context  
    • Method Detail

      • define

        void define​(Plugin.Context context)
        This method is executed at runtime when:
        • Web Server starts
        • Compute Engine starts
        • Scanner starts