]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21197 add a property to enable the download of the required plugins only
authorMatteo Mara <matteo.mara@sonarsource.com>
Mon, 18 Dec 2023 16:25:34 +0000 (17:25 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Jan 2024 20:02:48 +0000 (20:02 +0000)
sonar-core/src/main/java/org/sonar/core/config/ScannerProperties.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginRepository.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginRepositoryTest.java

index b0d619f5ce1f941680046ba1a36969daf80151a4..729bd82740fbce014b6b149fe77f1bb5b7c96218 100644 (file)
@@ -40,6 +40,7 @@ public class ScannerProperties {
   public static final String FILE_SIZE_LIMIT = "sonar.filesize.limit";
   public static final String LINKS_SOURCES_DEV = "sonar.links.scm_dev";
   public static final String DISABLE_PROJECT_AND_ORG_AUTODETECTION = "sonar.keys_autodetection.disabled";
+  public static final String PLUGIN_LOADING_OPTIMIZATION_KEY = "sonar.plugins.downloadOnlyRequired";
 
   private ScannerProperties() {
     // only static stuff
@@ -91,6 +92,14 @@ public class ScannerProperties {
         .description(
           "Allows discarding files from analysis exceeding certain sizes.")
         .hidden()
+        .build(),
+      PropertyDefinition.builder(PLUGIN_LOADING_OPTIMIZATION_KEY)
+        .name("Enable scanner plugin loading optimization")
+        .description("When enabled, scanners will only download plugins required for detected languages.")
+        .category(CoreProperties.CATEGORY_GENERAL)
+        .subCategory("Performance")
+        .type(BOOLEAN)
+        .defaultValue("true")
         .build());
   }
 }
index 6b875da762098bbd515f3410030e7d205054c201..fb2834604f657d4b27d05c800da365404f7f4614 100644 (file)
@@ -40,6 +40,7 @@ import org.sonar.scanner.mediumtest.LocalPlugin;
 
 import static java.util.stream.Collectors.toMap;
 import static org.sonar.api.utils.Preconditions.checkState;
+import static org.sonar.core.config.ScannerProperties.PLUGIN_LOADING_OPTIMIZATION_KEY;
 
 /**
  * Orchestrates the installation and loading of plugins
@@ -56,7 +57,7 @@ public class ScannerPluginRepository implements PluginRepository, Startable {
   private Map<String, Plugin> pluginInstancesByKeys;
   private Map<String, ScannerPlugin> pluginsByKeys;
   private Map<ClassLoader, String> keysByClassLoader;
-  private boolean shouldLoadAllPluginsOnStart;
+  private boolean shouldLoadOnlyRequiredPluginsOnStart;
 
   public ScannerPluginRepository(PluginInstaller installer, PluginJarExploder pluginJarExploder, PluginClassLoader loader, Configuration properties) {
     this.installer = installer;
@@ -67,9 +68,9 @@ public class ScannerPluginRepository implements PluginRepository, Startable {
 
   @Override
   public void start() {
-    shouldLoadAllPluginsOnStart = properties.getBoolean("sonar.plugins.loadAll").orElse(false);
-    if (shouldLoadAllPluginsOnStart) {
-      LOG.warn("sonar.plugins.loadAll is true, so ALL available plugins will be downloaded");
+    shouldLoadOnlyRequiredPluginsOnStart = properties.getBoolean(PLUGIN_LOADING_OPTIMIZATION_KEY).orElse(true);
+    if (!shouldLoadOnlyRequiredPluginsOnStart) {
+      LOG.warn("{} is false, so ALL available plugins will be downloaded", PLUGIN_LOADING_OPTIMIZATION_KEY);
       pluginsByKeys = new HashMap<>(installer.installAllPlugins());
     } else {
       pluginsByKeys = new HashMap<>(installer.installRequiredPlugins());
@@ -96,7 +97,7 @@ public class ScannerPluginRepository implements PluginRepository, Startable {
   }
 
   public Collection<PluginInfo> installPluginsForLanguages(Set<String> languageKeys) {
-    if (shouldLoadAllPluginsOnStart) {
+    if (!shouldLoadOnlyRequiredPluginsOnStart) {
       return Collections.emptySet();
     }
 
index 2fa2343f2e15370f0c9f8d5dcbd4a083d226710d..0c3f7c14a3027b2eea3f1a572d89ed98be47b9b0 100644 (file)
@@ -47,6 +47,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.sonar.core.config.ScannerProperties.PLUGIN_LOADING_OPTIMIZATION_KEY;
 
 public class ScannerPluginRepositoryTest {
 
@@ -90,9 +91,8 @@ public class ScannerPluginRepositoryTest {
   }
 
   @Test
-  public void should_install_all_plugins_when_loadall_flag_is_set() {
-    when(properties.getBoolean("sonar.plugins.loadAll")).thenReturn(Optional.of(true));
-
+  public void should_install_all_plugins_when_downloadOnlyRequired_flag_is_false() {
+    when(properties.getBoolean(PLUGIN_LOADING_OPTIMIZATION_KEY)).thenReturn(Optional.of(false));
     underTest.start();
 
     verify(installer).installAllPlugins();