]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SQSCANNER-26 Support SONARQUBE_SCANNER_PARAMS and sonar.scanner.skip
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 3 Aug 2016 11:53:23 +0000 (13:53 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 3 Aug 2016 11:53:23 +0000 (13:53 +0200)
it/projects/java-sample-no-properties/.sonar/.sonar_lock [new file with mode: 0644]
it/projects/java-sample-no-properties/.sonar/report-task.txt [new file with mode: 0644]
it/projects/java-sample-no-properties/src/basic/Hello.java [new file with mode: 0644]
it/projects/java-sample-no-properties/src/basic/World.java [new file with mode: 0644]
it/src/test/java/com/sonar/runner/it/JavaTest.java
pom.xml
src/main/java/org/sonarsource/scanner/cli/Conf.java
src/main/java/org/sonarsource/scanner/cli/Main.java
src/test/java/org/sonarsource/scanner/cli/ConfTest.java

diff --git a/it/projects/java-sample-no-properties/.sonar/.sonar_lock b/it/projects/java-sample-no-properties/.sonar/.sonar_lock
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/it/projects/java-sample-no-properties/.sonar/report-task.txt b/it/projects/java-sample-no-properties/.sonar/report-task.txt
new file mode 100644 (file)
index 0000000..c8e06cf
--- /dev/null
@@ -0,0 +1,5 @@
+projectKey=java:sample
+serverUrl=http://localhost:33151
+dashboardUrl=http://localhost:33151/dashboard/index/java:sample
+ceTaskId=AVZQO0KqHsi6TBOtG5xl
+ceTaskUrl=http://localhost:33151/api/ce/task?id=AVZQO0KqHsi6TBOtG5xl
diff --git a/it/projects/java-sample-no-properties/src/basic/Hello.java b/it/projects/java-sample-no-properties/src/basic/Hello.java
new file mode 100644 (file)
index 0000000..b9db5a0
--- /dev/null
@@ -0,0 +1,9 @@
+package basic;
+
+public class Hello {
+
+  public void hello() {
+    int i=356;
+    if (true) i=5658;
+  }
+}
diff --git a/it/projects/java-sample-no-properties/src/basic/World.java b/it/projects/java-sample-no-properties/src/basic/World.java
new file mode 100644 (file)
index 0000000..c65d91c
--- /dev/null
@@ -0,0 +1,8 @@
+package basic;
+
+public final class World {
+
+  public void world() {
+    System.out.println("hello world");
+  }
+}
index 7209ea9633d702bd44af1cf3c9a02a7f942a2bad..c93e3ccb3cd400afd18b1aefce8bb294e2826ad5 100644 (file)
@@ -211,6 +211,20 @@ public class JavaTest extends ScannerTestCase {
     assertThat(logs).contains(expectedLog);
   }
 
+  @Test
+  public void should_use_environment_props() {
+    SonarScanner build = newScanner(new File("projects/java-sample-no-properties"))
+      .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
+        + "\"sonar.projectKey\" : \"java:sample\"," +
+        "\"sonar.projectName\" : \"Java Sample, with comma\"," +
+        "\"sonar.projectDescription\" : \"This is a Java sample\"," +
+        "\"sonar.projectVersion\" : \"1.2.3\"," +
+        "\"sonar.sources\" : \"src\" }");
+    String logs = orchestrator.executeBuild(build).getLogs();
+    System.out.println(logs);
+
+  }
+
   @Test
   public void should_fail_if_unable_to_connect() {
     SonarScanner build = newScanner(new File("projects/java-sample"))
diff --git a/pom.xml b/pom.xml
index 2742ca02419b4bed77b46583a27e525afa7e3f2c..5efc7ad799fab58c272cec0108a7cb1e4ffe503b 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
   <parent>
     <groupId>org.sonarsource.parent</groupId>
     <artifactId>parent</artifactId>
-    <version>31</version>
+    <version>36</version>
   </parent>
 
   <groupId>org.sonarsource.scanner.cli</groupId>
     <dependency>
       <groupId>org.sonarsource.scanner.api</groupId>
       <artifactId>sonar-scanner-api</artifactId>
-      <version>2.6</version>
+      <version>2.7-build634</version>
+    </dependency>
+    <dependency>
+      <groupId>com.eclipsesource.minimal-json</groupId>
+      <artifactId>minimal-json</artifactId>
+      <version>0.9.4</version>
     </dependency>
     <dependency>
       <groupId>com.google.code.findbugs</groupId>
             <configuration>
               <rules>
                 <requireFilesSize>
-                  <minsize>500000</minsize>
-                  <maxsize>510000</maxsize>
+                  <minsize>510000</minsize>
+                  <maxsize>530000</maxsize>
                   <files>
                     <file>${project.build.directory}/sonar-scanner-${project.version}.zip</file>
                   </files>
index cf2b37f3e240172706c494dfd4ab53aaf8d724c0..790703035d5822b7935004e1239f327f9100c120 100644 (file)
@@ -27,10 +27,16 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
+import com.eclipsesource.json.Json;
+import com.eclipsesource.json.JsonObject;
+import com.eclipsesource.json.JsonObject.Member;
+import com.eclipsesource.json.JsonValue;
+
 class Conf {
   private static final String SCANNER_HOME = "scanner.home";
   private static final String SCANNER_SETTINGS = "scanner.settings";
@@ -40,13 +46,16 @@ class Conf {
   private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
   private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
   private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties";
+  private static final String SONARQUBE_SCANNER_PARAMS = "SONARQUBE_SCANNER_PARAMS";
 
   private final Cli cli;
   private final Logs logger;
+  private final Map<String, String> env;
 
-  Conf(Cli cli, Logs logger) {
+  Conf(Cli cli, Logs logger, Map<String, String> env) {
     this.cli = cli;
     this.logger = logger;
+    this.env = env;
   }
 
   Properties properties() throws IOException {
@@ -54,6 +63,7 @@ class Conf {
     result.putAll(loadGlobalProperties());
     result.putAll(loadProjectProperties());
     result.putAll(System.getProperties());
+    result.putAll(loadEnvironmentProperties());
     result.putAll(cli.properties());
     // root project base directory must be present and be absolute
     result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString());
@@ -61,8 +71,33 @@ class Conf {
     return result;
   }
 
+  private Properties loadEnvironmentProperties() {
+    Properties props = new Properties();
+
+    String scannerParams = env.get(SONARQUBE_SCANNER_PARAMS);
+    if (scannerParams != null) {
+      try {
+
+        JsonValue jsonValue = Json.parse(scannerParams);
+        JsonObject jsonObject = jsonValue.asObject();
+        Iterator<Member> it = jsonObject.iterator();
+
+        while (it.hasNext()) {
+          Member member = it.next();
+          String key = member.getName();
+          String value = member.getValue().asString();
+          props.put(key, value);
+        }
+      } catch (Exception e) {
+        throw new IllegalStateException("Failed to parse JSON in SONARQUBE_SCANNER_PARAMS environment variable", e);
+      }
+    }
+    return props;
+  }
+
   private Properties loadGlobalProperties() throws IOException {
-    Path settingsFile = locatePropertiesFile(cli.properties(), SCANNER_HOME, "conf/sonar-scanner.properties", SCANNER_SETTINGS);
+    Path settingsFile = locatePropertiesFile(cli.properties(), SCANNER_HOME, "conf/sonar-scanner.properties",
+      SCANNER_SETTINGS);
     if (settingsFile != null && Files.isRegularFile(settingsFile)) {
       logger.info("Scanner configuration file: " + settingsFile);
       return toProperties(settingsFile);
@@ -89,13 +124,15 @@ class Conf {
 
     Properties projectProps = new Properties();
 
-    // include already root base directory and eventually props loaded from root config file
+    // include already root base directory and eventually props loaded from
+    // root config file
     projectProps.putAll(rootProps);
 
     rootProps.putAll(knownProps);
     rootProps.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(rootProps).toString());
 
-    // projectProps will be overridden by any properties found in child project settings
+    // projectProps will be overridden by any properties found in child
+    // project settings
     loadModulesProperties(rootProps, projectProps, "");
     return projectProps;
   }
@@ -164,7 +201,8 @@ class Conf {
 
   private static void setModuleBaseDir(Path absoluteBaseDir, Properties childProps, String moduleId) {
     if (!Files.isDirectory(absoluteBaseDir)) {
-      throw new IllegalStateException(MessageFormat.format("The base directory of the module ''{0}'' does not exist: {1}", moduleId, absoluteBaseDir));
+      throw new IllegalStateException(MessageFormat
+        .format("The base directory of the module ''{0}'' does not exist: {1}", moduleId, absoluteBaseDir));
     }
     childProps.put(PROPERTY_PROJECT_BASEDIR, absoluteBaseDir.toString());
   }
@@ -182,7 +220,8 @@ class Conf {
     return moduleProps;
   }
 
-  private static Path locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
+  private static Path locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome,
+    String settingsKey) {
     Path settingsFile = null;
     String scannerHome = props.getProperty(homeKey, "");
     if (!"".equals(scannerHome)) {
@@ -222,18 +261,21 @@ class Conf {
   }
 
   protected void loadModulePropsFile(Path parentAbsoluteBaseDir, Properties moduleProps, String moduleId) {
-    Path propertyFile = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), parentAbsoluteBaseDir);
+    Path propertyFile = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE),
+      parentAbsoluteBaseDir);
     if (Files.isRegularFile(propertyFile)) {
       moduleProps.putAll(toProperties(propertyFile));
       Path absoluteBaseDir;
       if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
-        absoluteBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParent());
+        absoluteBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR),
+          propertyFile.getParent());
       } else {
         absoluteBaseDir = propertyFile.getParent();
       }
       setModuleBaseDir(absoluteBaseDir, moduleProps, moduleId);
     } else {
-      throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile);
+      throw new IllegalStateException(
+        "The properties file of the module '" + moduleId + "' does not exist: " + propertyFile);
     }
   }
 
@@ -245,13 +287,15 @@ class Conf {
 
     moduleProps.putAll(toProperties(propertyFile));
     if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
-      Path overwrittenBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParent());
+      Path overwrittenBaseDir = getAbsolutePath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR),
+        propertyFile.getParent());
       setModuleBaseDir(overwrittenBaseDir, moduleProps, moduleId);
     }
   }
 
   /**
-   * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute.
+   * Returns the file denoted by the given path, may this path be relative to
+   * "baseDir" or absolute.
    */
   protected static Path getAbsolutePath(String path, Path baseDir) {
     Path propertyFile = Paths.get(path.trim());
@@ -262,9 +306,11 @@ class Conf {
   }
 
   /**
-   * Transforms a comma-separated list String property in to a array of trimmed strings.
+   * Transforms a comma-separated list String property in to a array of
+   * trimmed strings.
    *
-   * This works even if they are separated by whitespace characters (space char, EOL, ...)
+   * This works even if they are separated by whitespace characters (space
+   * char, EOL, ...)
    *
    */
   static String[] getListFromProperty(Properties properties, String key) {
index f0b76bfb4bb740da4d854e82bbceac6f322ad8c7..19b953c8c149f7dbcb7288a11ce39a399c7ada31 100644 (file)
@@ -56,7 +56,7 @@ public class Main {
     Logs logs = new Logs(System.out, System.err);
     Exit exit = new Exit();
     Cli cli = new Cli(exit, logs).parse(args);
-    Main main = new Main(exit, cli, new Conf(cli, logs), new ScannerFactory(logs), logs);
+    Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerFactory(logs), logs);
     main.execute();
   }
 
index 3450e887ce87db226975ede2d91b0491e4397989..506e01d2ccffd198848c3dbf91bf807292745a75 100644 (file)
  */
 package org.sonarsource.scanner.cli;
 
+import static org.fest.assertions.Assertions.assertThat;
+import static org.junit.Assume.assumeTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
+
 import org.apache.commons.lang.SystemUtils;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
-import static org.fest.assertions.Assertions.assertThat;
-import static org.junit.Assume.assumeTrue;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
 public class ConfTest {
 
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
 
+  Map<String, String> env = new HashMap<>();
   Properties args = new Properties();
   Logs logs = new Logs(System.out, System.err);
   Cli cli = mock(Cli.class);
-  Conf conf = new Conf(cli, logs);
+  Conf conf = new Conf(cli, logs, env);
 
   @Before
   public void initConf() {
+    env.clear();
     when(cli.properties()).thenReturn(args);
   }
 
@@ -115,6 +120,17 @@ public class ConfTest {
     assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
   }
 
+  @Test
+  public void shouldLoadEnvironmentProperties() throws IOException {
+    env.put("SONARQUBE_SCANNER_PARAMS", "{\"sonar.key1\" : \"v1\", \"sonar.key2\" : \"v2\"}");
+    args.put("sonar.key2", "v3");
+
+    Properties props = conf.properties();
+
+    assertThat(props.getProperty("sonar.key1")).isEqualTo("v1");
+    assertThat(props.getProperty("sonar.key2")).isEqualTo("v3");
+  }
+
   @Test
   public void shouldSupportDeepModuleConfigurationInRoot() throws Exception {
     Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI());