@ScannerSide @ServerSide @ComputeEngineSide @SonarLintSide public interface SonarRuntime
A usage for plugins is to benefit from new APIs while keeping backward-compatibility with previous versions of SonarQube or SonarLint.
Example: a plugin extension wants to use a new feature of API 6.1 without breaking compatibility with version 6.0 at runtime. This new feature would be enabled only in 6.1 and greater runtimes.
// Component provided by sonar-plugin-api // @since 6.0 public interface AnApi { // implicitly since 6.0 public void foo(); // @since 6.1 public void bar(); } // Plugin extension public class MyExtension { private final SonarRuntime sonarRuntime; private final AnApi api; public MyExtension(SonarRuntime sonarRuntime, AnApi api) { this.sonarRuntime = sonarRuntime; this.api = api; } public void doSomething() { // assume that minimal supported runtime is 6.0 api.foo(); if (sonarRuntime.getApiVersion().isGreaterThanOrEqual(Version.create(6, 1))) { api.bar(); } } }
Note that Sensor
extensions can directly get SonarRuntime
through
SensorContext.runtime()
, without using constructor injection:
public class MySensor implements Sensor { public void execute(SensorContext context) { if (context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(6, 1)) { context.newMethodIntroducedIn6_0(); } } }
The minimal supported version of plugin API is verified at runtime. As plugin is built with sonar-plugin-api 6.1, we assume that the plugin requires v6.1 or greater at runtime. For this reason the plugin must override the minimal supported version in the configuration of sonar-packaging-maven-plugin 1.16+:
<packaging>sonar-plugin</packaging> <dependencies> <dependency> <groupId>org.sonarsource.sonarqube</groupId> <artifactId>sonar-plugin-api</artifactId> <version>6.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> <artifactId>sonar-packaging-maven-plugin</artifactId> <version>1.16</version> <extensions>true</extensions> <configuration> <!-- Override the default value 6.0 which is guessed from sonar-plugin-api dependency --> <sonarQubeMinVersion>6.0</sonarQubeMinVersion> </configuration> </plugin> </plugins> </build>
As this component was introduced in version 6.0, the pattern described above can't be
exactly applied when plugin must support version 5.6 Long Term Support. In this case plugin
should use SonarQubeVersion
, for example through Plugin.Context.getSonarQubeVersion()
or
SensorContext.getSonarQubeVersion()
.
Unit tests of plugin extensions can create instances of SonarRuntime
via SonarRuntimeImpl
.
Modifier and Type | Method and Description |
---|---|
Version |
getApiVersion()
Version of API (sonar-plugin-api artifact) at runtime.
|
SonarProduct |
getProduct()
The product being executed at runtime.
|
SonarQubeSide |
getSonarQubeSide()
The SonarQube stack being executed at runtime.
|
Version getApiVersion()
SonarProduct getProduct()
SonarQubeSide getSonarQubeSide()
UnsupportedOperationException
- if getProduct()
is not equal to SonarProduct.SONARQUBE
Copyright © 2009–2017 SonarSource. All rights reserved.