/* * SonarScanner CLI * Copyright (C) 2011-2024 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package org.sonarsource.scanner.cli; import java.util.Map; import java.util.Properties; import org.sonarsource.scanner.lib.ScanProperties; import org.sonarsource.scanner.lib.ScannerEngineBootstrapper; import org.sonarsource.scanner.lib.ScannerEngineFacade; /** * Arguments : * * * @since 1.0 */ public class Main { private static final String SEPARATOR = "------------------------------------------------------------------------"; private final Exit exit; private final Cli cli; private final Conf conf; private ScannerEngineBootstrapper scannerEngineBootstrapper; private final ScannerEngineBootstrapperFactory bootstrapperFactory; private final Logs logger; Main(Exit exit, Cli cli, Conf conf, ScannerEngineBootstrapperFactory bootstrapperFactory, Logs logger) { this.exit = exit; this.cli = cli; this.conf = conf; this.bootstrapperFactory = bootstrapperFactory; this.logger = logger; } public static void main(String[] args) { 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, System.getenv()), new ScannerEngineBootstrapperFactory(logs), logs); main.analyze(); } void analyze() { Stats stats = new Stats(logger).start(); int status = Exit.INTERNAL_ERROR; try { Properties p = conf.properties(); checkSkip(p); configureLogging(p); init(p); try (var engine = scannerEngineBootstrapper.bootstrap()) { logServerType(engine); engine.analyze((Map) p); displayExecutionResult(stats, "SUCCESS"); status = Exit.SUCCESS; } } catch (Throwable e) { displayExecutionResult(stats, "FAILURE"); showError("Error during SonarScanner execution", e, cli.isDebugEnabled()); status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR; } finally { exit.exit(status); } } private void logServerType(ScannerEngineFacade engine) { if (engine.isSonarCloud()) { logger.info("Communicating with SonarCloud"); } else { String serverVersion = engine.getServerVersion(); logger.info(String.format("Communicating with SonarQube Server %s", serverVersion)); } } private void checkSkip(Properties properties) { if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) { logger.info("SonarScanner analysis skipped"); exit.exit(Exit.SUCCESS); } } private void init(Properties p) { SystemInfo.print(logger); if (cli.isDisplayVersionOnly()) { exit.exit(Exit.SUCCESS); } scannerEngineBootstrapper = bootstrapperFactory.create(p, cli.getInvokedFrom()); } private void configureLogging(Properties props) { if ("true".equals(props.getProperty("sonar.verbose")) || "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level")) || "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level"))) { logger.setDebugEnabled(true); } } private void displayExecutionResult(Stats stats, String resultMsg) { logger.info(SEPARATOR); logger.info("EXECUTION " + resultMsg); logger.info(SEPARATOR); stats.stop(); logger.info(SEPARATOR); } private void showError(String message, Throwable e, boolean debug) { if (debug || !isUserError(e)) { logger.error(message, e); } else { logger.error(message); logger.error(e.getMessage()); String previousMsg = ""; for (Throwable cause = e.getCause(); cause != null && cause.getMessage() != null && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { logger.error("Caused by: " + cause.getMessage()); previousMsg = cause.getMessage(); } } if (!cli.isDebugEnabled()) { logger.error(""); suggestDebugMode(); } } private static boolean isUserError(Throwable e) { // class not available at compile time (loaded by isolated classloader) return "org.sonar.api.utils.MessageException".equals(e.getClass().getName()); } private void suggestDebugMode() { if (!cli.isEmbedded()) { logger.error("Re-run SonarScanner using the -X switch to enable full debug logging."); } } }