aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-cli/src/main/java/org/sonar/runner/cli
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-11-09 18:11:08 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2015-11-09 18:11:08 +0100
commit5a8d95da69a698a82faa01e7b396e33de61d4975 (patch)
treeb56e26f30a5ae5c20189d1f02224c9ddfb9e0711 /sonar-runner-cli/src/main/java/org/sonar/runner/cli
parent7b31d816877f1ce5126079cc642b915c7d4dbc88 (diff)
downloadsonar-scanner-cli-5a8d95da69a698a82faa01e7b396e33de61d4975.tar.gz
sonar-scanner-cli-5a8d95da69a698a82faa01e7b396e33de61d4975.zip
Extract SonarQube Scanner as a separate project
Diffstat (limited to 'sonar-runner-cli/src/main/java/org/sonar/runner/cli')
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java152
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java276
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Exit.java29
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java71
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java185
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java56
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java92
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java62
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java78
-rw-r--r--sonar-runner-cli/src/main/java/org/sonar/runner/cli/package-info.java24
10 files changed, 0 insertions, 1025 deletions
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java
deleted file mode 100644
index 3b627df..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Cli.java
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-import java.util.Properties;
-import org.sonar.runner.api.RunnerProperties;
-
-class Cli {
-
- private boolean debugMode = false;
- private boolean displayVersionOnly = false;
- private boolean displayStackTrace = false;
- private boolean interactive = false;
- private final Properties props = new Properties();
- private final Exit exit;
- private final Logs logger;
-
- public Cli(Exit exit, Logs logger) {
- this.exit = exit;
- this.logger = logger;
- }
-
- boolean isDebugMode() {
- return debugMode;
- }
-
- boolean isDisplayVersionOnly() {
- return displayVersionOnly;
- }
-
- boolean isDisplayStackTrace() {
- return displayStackTrace;
- }
-
- boolean isInteractive() {
- return interactive;
- }
-
- Properties properties() {
- return props;
- }
-
- Cli parse(String[] args) {
- reset();
- props.putAll(System.getProperties());
- for (int i = 0; i < args.length; i++) {
- String arg = args[i];
- if (i == 0 && !arg.startsWith("-")) {
- props.setProperty(RunnerProperties.TASK, arg);
-
- } else if ("-h".equals(arg) || "--help".equals(arg)) {
- printUsage();
-
- } else if ("-v".equals(arg) || "--version".equals(arg)) {
- displayVersionOnly = true;
-
- } else if ("-e".equals(arg) || "--errors".equals(arg)) {
- displayStackTrace = true;
- logger.setDisplayStackTrace(true);
-
- } else if ("-X".equals(arg) || "--debug".equals(arg)) {
- props.setProperty("sonar.verbose", "true");
- displayStackTrace = true;
- debugMode = true;
- logger.setDebugEnabled(true);
- logger.setDisplayStackTrace(true);
-
- } else if ("-D".equals(arg) || "--define".equals(arg)) {
- i++;
- if (i >= args.length) {
- printError("Missing argument for option --define");
- }
- arg = args[i];
- appendPropertyTo(arg, props);
-
- } else if (arg.startsWith("-D")) {
- arg = arg.substring(2);
- appendPropertyTo(arg, props);
-
- } else if ("-i".equals(arg) || "--interactive".equals(arg)) {
- interactive = true;
-
- } else {
- printError("Unrecognized option: " + arg);
- }
- }
-
- return this;
- }
-
- public void verify() {
- if ("fork".equals(props.getProperty("sonarRunner.mode")) && isInteractive()) {
- printError("Cannot run interactively in fork mode.");
- }
- }
-
- private void reset() {
- props.clear();
- debugMode = false;
- displayStackTrace = false;
- displayVersionOnly = false;
- }
-
- private static void appendPropertyTo(String arg, Properties props) {
- final String key, value;
- int j = arg.indexOf('=');
- if (j == -1) {
- key = arg;
- value = "true";
- } else {
- key = arg.substring(0, j);
- value = arg.substring(j + 1);
- }
- props.setProperty(key, value);
- }
-
- private void printError(String message) {
- logger.error(message);
- printUsage();
- }
-
- private void printUsage() {
- logger.info("");
- logger.info("usage: sonar-runner [options]");
- logger.info("");
- logger.info("Options:");
- logger.info(" -D,--define <arg> Define property");
- logger.info(" -e,--errors Produce execution error messages");
- logger.info(" -h,--help Display help information");
- logger.info(" -v,--version Display version information");
- logger.info(" -X,--debug Produce execution debug output");
- logger.info(" -i,--interactive Run interactively");
- exit.exit(Exit.SUCCESS);
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java
deleted file mode 100644
index be94193..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Conf.java
+++ /dev/null
@@ -1,276 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-
-class Conf {
- private static final String RUNNER_HOME = "runner.home";
- private static final String RUNNER_SETTINGS = "runner.settings";
- private static final String PROJECT_HOME = "project.home";
- private static final String PROJECT_SETTINGS = "project.settings";
- private static final String PROPERTY_MODULES = "sonar.modules";
- private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
- private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
- private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties";
-
- private final Cli cli;
- private final Logs logger;
-
- Conf(Cli cli, Logs logger) {
- this.cli = cli;
- this.logger = logger;
- }
-
- Properties properties() throws IOException {
- Properties result = new Properties();
- result.putAll(loadGlobalProperties());
- result.putAll(loadProjectProperties());
- result.putAll(System.getProperties());
- result.putAll(cli.properties());
- result.remove(PROJECT_HOME);
- return result;
- }
-
- private Properties loadGlobalProperties() throws IOException {
- File settingsFile = locatePropertiesFile(cli.properties(), RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS);
- if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
- logger.info("Runner configuration file: " + settingsFile.getAbsolutePath());
- return toProperties(settingsFile);
- }
- logger.info("Runner configuration file: NONE");
- return new Properties();
- }
-
- private Properties loadProjectProperties() throws IOException {
- Properties cliProps = cli.properties();
- File rootSettingsFile = locatePropertiesFile(cliProps, cliProps.containsKey(PROPERTY_PROJECT_BASEDIR) ? PROPERTY_PROJECT_BASEDIR : PROJECT_HOME,
- SONAR_PROJECT_PROPERTIES_FILENAME,
- PROJECT_SETTINGS);
- if (rootSettingsFile != null && rootSettingsFile.isFile() && rootSettingsFile.exists()) {
- logger.info("Project configuration file: " + rootSettingsFile.getAbsolutePath());
- Properties projectProps = new Properties();
- Properties rootProps = toProperties(rootSettingsFile);
- projectProps.putAll(rootProps);
- initRootProjectBaseDir(cliProps, rootProps);
- loadModulesProperties(rootProps, projectProps, "");
- return projectProps;
- }
- logger.info("Project configuration file: NONE");
- return new Properties();
- }
-
- private static void initRootProjectBaseDir(Properties cliProps, Properties rootProps) {
- if (!cliProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- String baseDir = cliProps.getProperty(PROJECT_HOME);
- rootProps.put(PROPERTY_PROJECT_BASEDIR, baseDir);
- } else {
- rootProps.put(PROPERTY_PROJECT_BASEDIR, cliProps.getProperty(PROPERTY_PROJECT_BASEDIR));
- }
- }
-
- private void loadModulesProperties(Properties parentProps, Properties projectProps, String prefix) {
- File parentBaseDir = new File(parentProps.getProperty(PROPERTY_PROJECT_BASEDIR));
- if (parentProps.containsKey(PROPERTY_MODULES)) {
- for (String module : getListFromProperty(parentProps, PROPERTY_MODULES)) {
- Properties moduleProps = extractModuleProperties(module, parentProps);
- moduleProps = loadChildConfigFile(parentBaseDir, moduleProps, module);
-
- // the child project may have children as well
- loadModulesProperties(moduleProps, projectProps, prefix + module + ".");
- // and finally add this child properties to global props
- merge(projectProps, prefix, module, moduleProps);
- }
- }
-
- }
-
- private static void merge(Properties projectProps, String prefix, String module, Properties moduleProps) {
- for (Map.Entry<Object, Object> entry : moduleProps.entrySet()) {
- projectProps.put(prefix + module + "." + entry.getKey(), entry.getValue());
- }
- }
-
- private Properties loadChildConfigFile(File parentBaseDir, Properties moduleProps, String moduleId) {
- final File baseDir;
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), parentBaseDir);
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- try {
- if (!parentBaseDir.getCanonicalFile().equals(baseDir.getCanonicalFile())) {
- tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
- }
- } catch (IOException e) {
- throw new IllegalStateException("Error when resolving baseDir", e);
- }
- } else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
- baseDir = loadPropsFile(parentBaseDir, moduleProps, moduleId);
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- moduleProps.remove(PROPERTY_PROJECT_CONFIG_FILE);
- } else {
- baseDir = new File(parentBaseDir, moduleId);
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
- }
-
- return moduleProps;
- }
-
- private static void setProjectBaseDir(File baseDir, Properties childProps, String moduleId) {
- if (!baseDir.isDirectory()) {
- throw new IllegalStateException(MessageFormat.format("The base directory of the module ''{0}'' does not exist: {1}", moduleId, baseDir.getAbsolutePath()));
- }
- childProps.put(PROPERTY_PROJECT_BASEDIR, baseDir.getAbsolutePath());
- }
-
- protected static Properties extractModuleProperties(String module, Properties properties) {
- Properties moduleProps = new Properties();
- String propertyPrefix = module + ".";
- int prefixLength = propertyPrefix.length();
- for (Map.Entry<Object, Object> entry : properties.entrySet()) {
- String key = (String) entry.getKey();
- if (key.startsWith(propertyPrefix)) {
- moduleProps.put(key.substring(prefixLength), entry.getValue());
- }
- }
- return moduleProps;
- }
-
- private static File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
- File settingsFile = null;
- String runnerHome = props.getProperty(homeKey, "");
- if (!"".equals(runnerHome)) {
- settingsFile = new File(runnerHome, relativePathFromHome);
- }
-
- if (settingsFile == null || !settingsFile.exists()) {
- String settingsPath = props.getProperty(settingsKey, "");
- if (!"".equals(settingsPath)) {
- settingsFile = new File(settingsPath);
- }
- }
- return settingsFile;
- }
-
- private static Properties toProperties(File file) {
- InputStream in = null;
- try {
- Properties properties = new Properties();
- in = new FileInputStream(file);
- properties.load(in);
- // Trim properties
- for (String propKey : properties.stringPropertyNames()) {
- properties.setProperty(propKey, properties.getProperty(propKey).trim());
- }
- return properties;
-
- } catch (Exception e) {
- throw new IllegalStateException("Fail to load file: " + file.getAbsolutePath(), e);
-
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- // Ignore errors
- }
- }
- }
- }
-
- /**
- * @return baseDir
- */
- protected File loadPropsFile(File parentBaseDir, Properties moduleProps, String moduleId) {
- File propertyFile = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), parentBaseDir);
- if (propertyFile.isFile()) {
- Properties propsFromFile = toProperties(propertyFile);
- for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
- moduleProps.put(entry.getKey(), entry.getValue());
- }
- File baseDir = null;
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
- } else {
- baseDir = propertyFile.getParentFile();
- }
- setProjectBaseDir(baseDir, moduleProps, moduleId);
- return baseDir;
- } else {
- throw new IllegalStateException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
- }
- }
-
- private static void tryToFindAndLoadPropsFile(File baseDir, Properties moduleProps, String moduleId) {
- File propertyFile = new File(baseDir, SONAR_PROJECT_PROPERTIES_FILENAME);
- if (propertyFile.isFile()) {
- Properties propsFromFile = toProperties(propertyFile);
- for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
- moduleProps.put(entry.getKey(), entry.getValue());
- }
- if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
- File overwrittenBaseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
- setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
- }
- }
- }
-
- /**
- * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute.
- */
- protected static File getFileFromPath(String path, File baseDir) {
- File propertyFile = new File(path.trim());
- if (!propertyFile.isAbsolute()) {
- propertyFile = new File(baseDir, propertyFile.getPath());
- }
- return propertyFile;
- }
-
- /**
- * Transforms a comma-separated list String property in to a array of trimmed strings.
- *
- * This works even if they are separated by whitespace characters (space char, EOL, ...)
- *
- */
- static String[] getListFromProperty(Properties properties, String key) {
- String value = properties.getProperty(key, "").trim();
- if (value.isEmpty()) {
- return new String[0];
- }
- String[] values = value.split(",");
- List<String> trimmedValues = new ArrayList<>();
- for (int i = 0; i < values.length; i++) {
- String trimmedValue = values[i].trim();
- if (!trimmedValue.isEmpty()) {
- trimmedValues.add(trimmedValue);
- }
- }
- return trimmedValues.toArray(new String[trimmedValues.size()]);
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Exit.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Exit.java
deleted file mode 100644
index 6d53dc2..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Exit.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-class Exit {
- static final int SUCCESS = 0;
- static final int ERROR = 1;
-
- void exit(int status) {
- System.exit(status);
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java
deleted file mode 100644
index 69c2a23..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Logs.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-import java.io.PrintStream;
-
-public class Logs {
- private boolean debugEnabled = false;
- private boolean displayStackTrace = false;
- private PrintStream stdOut;
- private PrintStream stdErr;
-
- public Logs(PrintStream stdOut, PrintStream stdErr) {
- this.stdErr = stdErr;
- this.stdOut = stdOut;
- }
-
- public void setDebugEnabled(boolean debugEnabled) {
- this.debugEnabled = debugEnabled;
- }
-
- public void setDisplayStackTrace(boolean displayStackTrace) {
- this.displayStackTrace = displayStackTrace;
- }
-
- public boolean isDebugEnabled() {
- return debugEnabled;
- }
-
- public void debug(String message) {
- if (isDebugEnabled()) {
- stdOut.println("DEBUG: " + message);
- }
- }
-
- public void info(String message) {
- stdOut.println("INFO: " + message);
- }
-
- public void warn(String message) {
- stdOut.println("WARN: " + message);
- }
-
- public void error(String message) {
- stdErr.println("ERROR: " + message);
- }
-
- public void error(String message, Throwable t) {
- stdErr.println("ERROR: " + message);
- if (t != null && displayStackTrace) {
- t.printStackTrace(stdErr);
- }
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java
deleted file mode 100644
index bceda70..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Main.java
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.charset.StandardCharsets;
-import java.util.Properties;
-import org.sonar.runner.api.EmbeddedRunner;
-
-/**
- * Arguments :
- * <ul>
- * <li>runner.home: optional path to runner home (root directory with sub-directories bin, lib and conf)</li>
- * <li>runner.settings: optional path to runner global settings, usually ${runner.home}/conf/sonar-runner.properties.
- * This property is used only if ${runner.home} is not defined</li>
- * <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>
- * <li>project.settings: optional path to project settings. Default value is ${project.home}/sonar-project.properties.</li>
- * </ul>
- *
- * @since 1.0
- */
-public class Main {
-
- private final Shutdown shutdown;
- private final Cli cli;
- private final Conf conf;
- private EmbeddedRunner runner;
- private BufferedReader inputReader;
- private RunnerFactory runnerFactory;
- private Logs logger;
-
- Main(Shutdown shutdown, Cli cli, Conf conf, RunnerFactory runnerFactory, Logs logger) {
- this.shutdown = shutdown;
- this.cli = cli;
- this.conf = conf;
- this.runnerFactory = runnerFactory;
- this.logger = logger;
- }
-
- public static void main(String[] args) {
- Exit exit = new Exit();
- Logs logs = new Logs(System.out, System.err);
- Cli cli = new Cli(exit, logs).parse(args);
- cli.verify();
- Shutdown shutdown = new Shutdown(exit, cli.isInteractive());
- Main main = new Main(shutdown, cli, new Conf(cli, logs), new RunnerFactory(logs), logs);
- main.execute();
- }
-
- void execute() {
- Stats stats = new Stats(logger).start();
-
- try {
- Properties p = conf.properties();
- init(p);
- runner.start();
-
- if (cli.isInteractive()) {
- interactiveLoop(p);
- } else {
- runAnalysis(stats, p);
- }
- } catch (Exception e) {
- displayExecutionResult(stats, "FAILURE");
- showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace());
- shutdown.exit(Exit.ERROR);
- }
-
- runner.stop();
- shutdown.exit(Exit.SUCCESS);
- }
-
- private void interactiveLoop(Properties p) throws IOException {
- do {
- Stats stats = new Stats(logger).start();
- try {
- runAnalysis(stats, p);
- } catch (Exception e) {
- displayExecutionResult(stats, "FAILURE");
- showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace());
- }
- } while (waitForUser());
- }
-
- private void init(Properties p) throws IOException {
- SystemInfo.print(logger);
- if (cli.isDisplayVersionOnly()) {
- shutdown.exit(Exit.SUCCESS);
- }
-
- if (cli.isDisplayStackTrace()) {
- logger.info("Error stacktraces are turned on.");
- }
-
- runner = runnerFactory.create(p);
- }
-
- private void runAnalysis(Stats stats, Properties p) {
- runner.runAnalysis(p);
- displayExecutionResult(stats, "SUCCESS");
- }
-
- private boolean waitForUser() throws IOException {
- if (inputReader == null) {
- inputReader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
- }
-
- shutdown.signalReady(true);
- if (shutdown.shouldExit()) {
- // exit before displaying message
- return false;
- }
-
- System.out.println("");
- System.out.println("<Press enter to restart analysis or Ctrl+C to exit the interactive mode>");
- String line = inputReader.readLine();
- shutdown.signalReady(false);
-
- return line != null;
- }
-
- // Visible for testing
- void setInputReader(BufferedReader inputReader) {
- this.inputReader = inputReader;
- }
-
- private void displayExecutionResult(Stats stats, String resultMsg) {
- logger.info("------------------------------------------------------------------------");
- logger.info("EXECUTION " + resultMsg);
- logger.info("------------------------------------------------------------------------");
- stats.stop();
- logger.info("------------------------------------------------------------------------");
- }
-
- private void showError(String message, Throwable e, boolean showStackTrace) {
- if (showStackTrace) {
- logger.error(message, e);
- if (!cli.isDebugMode()) {
- logger.error("");
- suggestDebugMode();
- }
- } else {
- logger.error(message);
- if (e != null) {
- 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();
- }
- }
- logger.error("");
- logger.error("To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.");
- if (!cli.isDebugMode()) {
- suggestDebugMode();
- }
- }
- }
-
- private void suggestDebugMode() {
- logger.error("Re-run SonarQube Runner using the -X switch to enable full debug logging.");
- }
-
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java
deleted file mode 100644
index 69406ba..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/RunnerFactory.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-import java.util.Properties;
-import org.sonar.runner.api.EmbeddedRunner;
-import org.sonar.runner.api.LogOutput;
-
-class RunnerFactory {
-
- private final Logs logger;
-
- public RunnerFactory(Logs logger) {
- this.logger = logger;
- }
-
- EmbeddedRunner create(Properties props) {
- return EmbeddedRunner.create(new DefaultLogOutput()).addGlobalProperties(props);
- }
-
- class DefaultLogOutput implements LogOutput {
- @Override
- public void log(String formattedMessage, Level level) {
- switch (level) {
- case TRACE:
- case DEBUG:
- logger.debug(formattedMessage);
- break;
- case ERROR:
- logger.error(formattedMessage);
- break;
- case INFO:
- case WARN:
- default:
- logger.info(formattedMessage);
- }
- }
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java
deleted file mode 100644
index 7da4f93..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Shutdown.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-class Shutdown {
- static final int SUCCESS = 0;
- static final int ERROR = 1;
- private static final long DEFAULT_MAX_WAIT = 10_000;
-
- private long maxWait;
- ShutdownHook hook = new ShutdownHook();
- private boolean isReady = false;
- private boolean exiting = false;
- private Object lock = new Object();
- private Exit exit;
-
- Shutdown(Exit exit, boolean isInteractive) {
- this(exit, isInteractive, DEFAULT_MAX_WAIT);
- }
-
- Shutdown(Exit exit, boolean isInteractive, long maxWait) {
- this.maxWait = maxWait;
- this.exit = exit;
- if (isInteractive) {
- Runtime.getRuntime().addShutdownHook(hook);
- }
- }
-
- void exit(int status) {
- synchronized (lock) {
- signalReady(true);
- }
- exit.exit(status);
- }
-
- void signalReady(boolean ready) {
- synchronized (lock) {
- this.isReady = ready;
- lock.notifyAll();
- }
- }
-
- boolean shouldExit() {
- synchronized (lock) {
- return exiting;
- }
- }
-
- class ShutdownHook extends Thread {
- private ShutdownHook() {
- this.setName("shutdown-hook");
- }
-
- @Override
- public void run() {
- long startTime = System.currentTimeMillis();
- synchronized (lock) {
- exiting = true;
-
- while (!isReady) {
- long waitTime = startTime + maxWait - System.currentTimeMillis();
- if (waitTime <= 0) {
- break;
- }
-
- try {
- lock.wait(waitTime);
- } catch (InterruptedException e) {
- // continue
- }
- }
- }
- }
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java
deleted file mode 100644
index 82fbdb5..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/Stats.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-class Stats {
- private final Logs logger;
- private long startTime;
-
- Stats(Logs logger) {
- this.logger = logger;
- }
-
- Stats start() {
- startTime = System.currentTimeMillis();
- return this;
- }
-
- Stats stop() {
- long stopTime = System.currentTimeMillis() - startTime;
- logger.info("Total time: " + formatTime(stopTime));
-
- System.gc();
- Runtime r = Runtime.getRuntime();
- long mb = 1024L * 1024;
- logger.info("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M");
-
- return this;
- }
-
- static String formatTime(long time) {
- long h = time / (60 * 60 * 1000);
- long m = (time - h * 60 * 60 * 1000) / (60 * 1000);
- long s = (time - h * 60 * 60 * 1000 - m * 60 * 1000) / 1000;
- long ms = time % 1000;
- final String format;
- if (h > 0) {
- format = "%1$d:%2$02d:%3$02d.%4$03ds";
- } else if (m > 0) {
- format = "%2$d:%3$02d.%4$03ds";
- } else {
- format = "%3$d.%4$03ds";
- }
- return String.format(format, h, m, s, ms);
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java
deleted file mode 100644
index 5d98720..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/SystemInfo.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-package org.sonar.runner.cli;
-
-import org.sonar.runner.api.RunnerVersion;
-
-class SystemInfo {
- private static System2 system = new System2();
-
- private SystemInfo() {
- }
-
- static void setSystem(System2 system) {
- SystemInfo.system = system;
- }
-
- static void print(Logs logger) {
- logger.info("SonarQube Runner " + RunnerVersion.version());
- logger.info(java());
- logger.info(os());
- String runnerOpts = system.getenv("SONAR_RUNNER_OPTS");
- if (runnerOpts != null) {
- logger.info("SONAR_RUNNER_OPTS=" + runnerOpts);
- }
- }
-
- static String java() {
- StringBuilder sb = new StringBuilder();
- sb
- .append("Java ")
- .append(system.getProperty("java.version"))
- .append(" ")
- .append(system.getProperty("java.vendor"));
- String bits = system.getProperty("sun.arch.data.model");
- if ("32".equals(bits) || "64".equals(bits)) {
- sb.append(" (").append(bits).append("-bit)");
- }
- return sb.toString();
- }
-
- static String os() {
- StringBuilder sb = new StringBuilder();
- sb
- .append(system.getProperty("os.name"))
- .append(" ")
- .append(system.getProperty("os.version"))
- .append(" ")
- .append(system.getProperty("os.arch"));
- return sb.toString();
- }
-
- static class System2 {
- String getProperty(String key) {
- return System.getProperty(key);
- }
-
- String getenv(String key) {
- return System.getenv(key);
- }
- }
-}
diff --git a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/package-info.java b/sonar-runner-cli/src/main/java/org/sonar/runner/cli/package-info.java
deleted file mode 100644
index 4a17c31..0000000
--- a/sonar-runner-cli/src/main/java/org/sonar/runner/cli/package-info.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * SonarQube Runner - CLI - Distribution
- * Copyright (C) 2011 SonarSource
- * sonarqube@googlegroups.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 02
- */
-@ParametersAreNonnullByDefault
-package org.sonar.runner.cli;
-
-import javax.annotation.ParametersAreNonnullByDefault;
-