You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SonarQubeVersion.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api;
  21. import javax.annotation.concurrent.Immutable;
  22. import org.sonar.api.scanner.ScannerSide;
  23. import org.sonar.api.batch.sensor.Sensor;
  24. import org.sonar.api.batch.sensor.SensorContext;
  25. import org.sonar.api.ce.ComputeEngineSide;
  26. import org.sonar.api.server.ServerSide;
  27. import org.sonar.api.utils.Version;
  28. import static java.util.Objects.requireNonNull;
  29. /**
  30. * Version of SonarQube at runtime, but not at compilation time.
  31. * This component can be injected as a dependency of plugin extensions.
  32. * The main usage for a plugin is to benefit from new APIs
  33. * while keeping backward-compatibility with previous versions of API.
  34. *
  35. * <p>
  36. * Example: a plugin extension needs a new feature of API 6.0 without
  37. * breaking compatibility with version 5.6 at runtime. This new feature
  38. * would be disabled when plugin is executed within SonarQube 5.6.
  39. * </p>
  40. * <pre>
  41. * // Component provided by sonar-plugin-api
  42. * // @since 5.6
  43. * public interface AnApi {
  44. * // implicitly since 5.6
  45. * public void foo();
  46. *
  47. * // @since 6.0
  48. * public void bar();
  49. * }
  50. *
  51. * // Component provided by plugin
  52. * public class MyExtension {
  53. * private final SonarQubeVersion sonarQubeVersion;
  54. * private final AnApi api;
  55. *
  56. * public MyExtension(SonarQubeVersion sonarQubeVersion, AnApi api) {
  57. * this.sonarQubeVersion = sonarQubeVersion;
  58. * this.api = api;
  59. * }
  60. *
  61. * public void doSomething() {
  62. * // assume that runtime is 5.6+
  63. * api.foo();
  64. *
  65. * if (sonarQubeVersion.isGreaterThanOrEqual(Version.create(6, 0))) {
  66. * api.bar();
  67. * }
  68. * }
  69. * }
  70. * </pre>
  71. *
  72. * <p>
  73. * Note that {@link Sensor} extensions can directly get {@link SonarQubeVersion} through
  74. * {@link SensorContext#getSonarQubeVersion()}, without using constructor injection:
  75. * </p>
  76. * <pre>
  77. * public class MySensor implements Sensor {
  78. *
  79. * public void execute(SensorContext context) {
  80. * if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 0)) {
  81. * context.newMethodIntroducedIn6_0();
  82. * }
  83. * }
  84. *
  85. * }
  86. * </pre>
  87. * <p>
  88. * The minimal supported version of SonarQube is verified at runtime. As plugin is built
  89. * with sonar-plugin-api 6.0, we assume that the plugin requires v6.0 or greater at runtime.
  90. * As the plugin codebase is compatible with 5.6, the plugin must define what is the
  91. * effective minimal supported version through the configuration of sonar-packaging-maven-plugin 1.16+:
  92. * <p>
  93. * <pre>
  94. * &lt;packaging&gt;sonar-plugin&lt;/packaging&gt;
  95. *
  96. * &lt;dependencies&gt;
  97. * &lt;dependency&gt;
  98. * &lt;groupId&gt;org.sonarsource.sonarqube&lt;/groupId&gt;
  99. * &lt;artifactId&gt;sonar-plugin-api&lt;/artifactId&gt;
  100. * &lt;version&gt;6.0&lt;/version&gt;
  101. * &lt;scope&gt;provided&lt;/scope&gt;
  102. * &lt;/dependency&gt;
  103. * &lt;/dependencies&gt;
  104. *
  105. * &lt;build&gt;
  106. * &lt;plugins&gt;
  107. * &lt;plugin&gt;
  108. * &lt;groupId&gt;org.sonarsource.sonar-packaging-maven-plugin&lt;/groupId&gt;
  109. * &lt;artifactId&gt;sonar-packaging-maven-plugin&lt;/artifactId&gt;
  110. * &lt;version&gt;1.16&lt;/version&gt;
  111. * &lt;extensions&gt;true&lt;/extensions&gt;
  112. * &lt;configuration&gt;
  113. * &lt;!-- Override the default value 6.0 which is guessed from sonar-plugin-api dependency --&gt;
  114. * &lt;sonarQubeMinVersion&gt;5.6&lt;/sonarQubeMinVersion&gt;
  115. * &lt;/configuration&gt;
  116. * &lt;/plugin&gt;
  117. * &lt;/plugins&gt;
  118. * &lt;/build&gt;
  119. * </pre>
  120. *
  121. * <p>
  122. * The component {@link SonarRuntime}, introduced in version 6.0, is more complete.
  123. * It is preferred over {@link SonarQubeVersion} if compatibility with version 5.6 Long Term Support
  124. * is not required.
  125. * </p>
  126. *
  127. * @see SonarRuntime
  128. * @deprecated since 7.8 Use {@link SonarRuntime} instead.
  129. * @since 5.5
  130. */
  131. @ScannerSide
  132. @ServerSide
  133. @ComputeEngineSide
  134. @Immutable
  135. @Deprecated
  136. public class SonarQubeVersion {
  137. private final Version version;
  138. public SonarQubeVersion(Version version) {
  139. requireNonNull(version);
  140. this.version = version;
  141. }
  142. public Version get() {
  143. return this.version;
  144. }
  145. public boolean isGreaterThanOrEqual(Version than) {
  146. return this.version.isGreaterThanOrEqual(than);
  147. }
  148. }