/* * SonarScanner CLI * Copyright (C) 2011-2025 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 ch.qos.logback.classic.Level; import java.util.Map; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonarsource.scanner.lib.ScannerEngineBootstrapper; import org.sonarsource.scanner.lib.ScannerProperties; /** * Arguments : * * * @since 1.0 */ public class Main { private static final Logger LOG = LoggerFactory.getLogger(Main.class); private static final String FAILURE = "FAILURE"; private static final String SUCCESS = "SUCCESS"; private final Exit exit; private final Cli cli; private final Conf conf; private ScannerEngineBootstrapper scannerEngineBootstrapper; private final ScannerEngineBootstrapperFactory bootstrapperFactory; Main(Exit exit, Cli cli, Conf conf, ScannerEngineBootstrapperFactory bootstrapperFactory) { this.exit = exit; this.cli = cli; this.conf = conf; this.bootstrapperFactory = bootstrapperFactory; } public static void main(String[] args) { Exit exit = new Exit(); Cli cli = new Cli(exit).parse(args); Main main = new Main(exit, cli, new Conf(cli, System.getenv()), new ScannerEngineBootstrapperFactory()); main.analyze(); } void analyze() { Stats stats = new Stats().start(); int status = Exit.INTERNAL_ERROR; try { Properties p = conf.properties(); checkSkip(p); configureLogging(p); init(p); try (var result = scannerEngineBootstrapper.bootstrap()) { if (result.isSuccessful()) { var engine = result.getEngineFacade(); var success = engine.analyze((Map) p); if (success) { displayExecutionResult(stats, SUCCESS); status = Exit.SUCCESS; } else { displayExecutionResult(stats, FAILURE); status = Exit.SCANNER_ENGINE_ERROR; } } else { LOG.debug("Scanner engine bootstrapping failed"); displayExecutionResult(stats, FAILURE); status = Exit.INTERNAL_ERROR; } } } catch (Throwable e) { displayExecutionResult(stats, FAILURE); showError(e, cli.isDebugEnabled()); status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR; } finally { exit.exit(status); } } private void checkSkip(Properties properties) { if ("true".equalsIgnoreCase(properties.getProperty(ScannerProperties.SKIP))) { LOG.info("SonarScanner CLI analysis skipped"); exit.exit(Exit.SUCCESS); } } private void init(Properties p) { SystemInfo.print(); if (cli.isDisplayVersionOnly()) { exit.exit(Exit.SUCCESS); } scannerEngineBootstrapper = bootstrapperFactory.create(p, cli.getInvokedFrom()); } private static 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"))) { var rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.setLevel(Level.DEBUG); } } private static void displayExecutionResult(Stats stats, String resultMsg) { LOG.info("EXECUTION {}", resultMsg); stats.stop(); } private void showError(Throwable e, boolean debug) { var message = "Error during SonarScanner CLI execution"; if (debug || !isUserError(e)) { LOG.error(message, e); } else { LOG.error(message); LOG.error(e.getMessage()); String previousMsg = ""; for (Throwable cause = e.getCause(); cause != null && cause.getMessage() != null && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { LOG.error("Caused by: {}", cause.getMessage()); previousMsg = cause.getMessage(); } } if (!cli.isDebugEnabled()) { LOG.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()) { LOG.error("Re-run SonarScanner CLI using the -X switch to enable full debug logging."); } } }