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.6KB

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