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.

Main.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-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.sonarsource.scanner.cli;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. import org.sonarsource.scanner.api.EmbeddedScanner;
  24. import org.sonarsource.scanner.api.ScanProperties;
  25. /**
  26. * Arguments :
  27. * <ul>
  28. * <li>scanner.home: optional path to Scanner home (root directory with sub-directories bin, lib and conf)</li>
  29. * <li>scanner.settings: optional path to runner global settings, usually ${scanner.home}/conf/sonar-scanner.properties.
  30. * This property is used only if ${scanner.home} is not defined</li>
  31. * <li>project.home: path to project root directory. If not set, then it's supposed to be the directory where the runner is executed</li>
  32. * <li>project.settings: optional path to project settings. Default value is ${project.home}/sonar-project.properties.</li>
  33. * </ul>
  34. *
  35. * @since 1.0
  36. */
  37. public class Main {
  38. private static final String SEPARATOR = "------------------------------------------------------------------------";
  39. private final Exit exit;
  40. private final Cli cli;
  41. private final Conf conf;
  42. private EmbeddedScanner runner;
  43. private ScannerFactory runnerFactory;
  44. private Logs logger;
  45. Main(Exit exit, Cli cli, Conf conf, ScannerFactory runnerFactory, Logs logger) {
  46. this.exit = exit;
  47. this.cli = cli;
  48. this.conf = conf;
  49. this.runnerFactory = runnerFactory;
  50. this.logger = logger;
  51. }
  52. public static void main(String[] args) {
  53. Logs logs = new Logs(System.out, System.err);
  54. Exit exit = new Exit();
  55. Cli cli = new Cli(exit, logs).parse(args);
  56. Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerFactory(logs), logs);
  57. main.execute();
  58. }
  59. void execute() {
  60. Stats stats = new Stats(logger).start();
  61. int status = Exit.INTERNAL_ERROR;
  62. try {
  63. Properties p = conf.properties();
  64. checkSkip(p);
  65. configureLogging(p);
  66. init(p);
  67. runner.start();
  68. logger.info(String.format("Analyzing on %s", conf.isSonarCloud(null) ? "SonarCloud" : ("SonarQube server " + runner.serverVersion())));
  69. execute(stats, p);
  70. status = Exit.SUCCESS;
  71. } catch (Throwable e) {
  72. displayExecutionResult(stats, "FAILURE");
  73. showError("Error during SonarScanner execution", e, cli.isDebugEnabled());
  74. status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR;
  75. } finally {
  76. exit.exit(status);
  77. }
  78. }
  79. private void checkSkip(Properties properties) {
  80. if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) {
  81. logger.info("SonarScanner analysis skipped");
  82. exit.exit(Exit.SUCCESS);
  83. }
  84. }
  85. private void init(Properties p) {
  86. SystemInfo.print(logger);
  87. if (cli.isDisplayVersionOnly()) {
  88. exit.exit(Exit.SUCCESS);
  89. }
  90. runner = runnerFactory.create(p);
  91. }
  92. private void configureLogging(Properties props) {
  93. if ("true".equals(props.getProperty("sonar.verbose"))
  94. || "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level"))
  95. || "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level"))) {
  96. logger.setDebugEnabled(true);
  97. }
  98. }
  99. private void execute(Stats stats, Properties p) {
  100. runner.execute((Map) p);
  101. displayExecutionResult(stats, "SUCCESS");
  102. }
  103. private void displayExecutionResult(Stats stats, String resultMsg) {
  104. logger.info(SEPARATOR);
  105. logger.info("EXECUTION " + resultMsg);
  106. logger.info(SEPARATOR);
  107. stats.stop();
  108. logger.info(SEPARATOR);
  109. }
  110. private void showError(String message, Throwable e, boolean debug) {
  111. if (debug || !isUserError(e)) {
  112. logger.error(message, e);
  113. } else {
  114. logger.error(message);
  115. logger.error(e.getMessage());
  116. String previousMsg = "";
  117. for (Throwable cause = e.getCause(); cause != null
  118. && cause.getMessage() != null
  119. && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
  120. logger.error("Caused by: " + cause.getMessage());
  121. previousMsg = cause.getMessage();
  122. }
  123. }
  124. if (!cli.isDebugEnabled()) {
  125. logger.error("");
  126. suggestDebugMode();
  127. }
  128. }
  129. private static boolean isUserError(Throwable e) {
  130. // class not available at compile time (loaded by isolated classloader)
  131. return "org.sonar.api.utils.MessageException".equals(e.getClass().getName());
  132. }
  133. private void suggestDebugMode() {
  134. if (!cli.isEmbedded()) {
  135. logger.error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  136. }
  137. }
  138. }