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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact 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.io.IOException;
  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.ERROR;
  62. try {
  63. Properties p = conf.properties();
  64. checkSkip(p);
  65. configureLogging(p);
  66. init(p);
  67. runner.start();
  68. logger.info("SonarQube server " + runner.serverVersion());
  69. runAnalysis(stats, p);
  70. status = Exit.SUCCESS;
  71. } catch (Exception e) {
  72. status = Exit.ERROR;
  73. displayExecutionResult(stats, "FAILURE");
  74. showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled());
  75. } finally {
  76. try {
  77. runner.stop();
  78. } catch (Throwable e) {
  79. status = Exit.ERROR;
  80. logger.error("Unable to properly stop the scanner", e);
  81. } finally {
  82. exit.exit(status);
  83. }
  84. }
  85. }
  86. private void checkSkip(Properties properties) {
  87. if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) {
  88. logger.info("SonarQube Scanner analysis skipped");
  89. exit.exit(Exit.SUCCESS);
  90. }
  91. }
  92. private void init(Properties p) throws IOException {
  93. SystemInfo.print(logger);
  94. if (cli.isDisplayVersionOnly()) {
  95. exit.exit(Exit.SUCCESS);
  96. }
  97. runner = runnerFactory.create(p);
  98. }
  99. private void configureLogging(Properties props) throws IOException {
  100. if ("true".equals(props.getProperty("sonar.verbose"))
  101. || "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level"))
  102. || "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level"))) {
  103. logger.setDebugEnabled(true);
  104. }
  105. }
  106. private void runAnalysis(Stats stats, Properties p) {
  107. runner.runAnalysis(p);
  108. displayExecutionResult(stats, "SUCCESS");
  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 (showStackTrace(e, debug)) {
  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 showStackTrace(Throwable e, boolean debug) {
  137. // class not available at compile time (loaded by isolated classloader)
  138. return debug || !"org.sonar.api.utils.MessageException".equals(e.getClass().getName());
  139. }
  140. private void suggestDebugMode() {
  141. logger.error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  142. }
  143. }