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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.apache.commons.io.IOUtils;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.util.Locale;
  27. import java.util.Properties;
  28. /**
  29. * Arguments :
  30. * <ul>
  31. * <li>runner.home: optional path to runner home (root directory with sub-directories bin, lib and conf)</li>
  32. * <li>runner.settings: optional path to runner global settings, usually ${runner.home}/conf/sonar-runner.properties.
  33. * This property is used only if ${runner.home} is not defined</li>
  34. * <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>
  35. * <li>project.settings: optional path to project settings. Default value is ${project.home}/sonar-project.properties.</li>
  36. * </ul>
  37. *
  38. * @since 1.0
  39. */
  40. public final class Main {
  41. private static final String RUNNER_HOME = "runner.home";
  42. private static final String RUNNER_SETTINGS = "runner.settings";
  43. private static final String PROJECT_HOME = "project.home";
  44. private static final String PROJECT_SETTINGS = "project.settings";
  45. // TODO Remove this after everything is updated to support tasks
  46. private static final String TASK_COMMAND = "sonar.task";
  47. boolean debugMode = false;
  48. boolean displayVersionOnly = false;
  49. boolean displayStackTrace = false;
  50. String command;
  51. Properties globalProperties;
  52. Properties projectProperties;
  53. /**
  54. * Entry point of the program.
  55. */
  56. public static void main(String[] args) {
  57. new Main().execute(args);
  58. }
  59. Main() {
  60. }
  61. private void execute(String[] args) {
  62. Properties argsProperties = parseArguments(args);
  63. System.out.println("Runner version: " + Version.getVersion());
  64. System.out.println("Java version: " + System.getProperty("java.version", "<unknown>")
  65. + ", vendor: " + System.getProperty("java.vendor", "<unknown>"));
  66. System.out
  67. .println("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\"");
  68. if (!displayVersionOnly) {
  69. int result = execute(argsProperties);
  70. System.exit(result);
  71. }
  72. }
  73. private int execute(Properties argsProperties) {
  74. if (displayStackTrace) {
  75. Logs.info("Error stacktraces are turned on.");
  76. }
  77. Stats stats = new Stats().start();
  78. try {
  79. loadProperties(argsProperties);
  80. Runner runner = Runner.create(command, globalProperties, projectProperties);
  81. Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + runner.getSourceCodeEncoding() + "\""
  82. + (runner.isEncodingPlatformDependant() ? " (analysis is platform dependent)" : ""));
  83. Logs.debug("Other system properties:");
  84. Logs.debug(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\"");
  85. Logs.info("Server: " + runner.getSonarServerURL());
  86. try {
  87. Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
  88. Logs.info("Cache directory: " + runner.getCache().getCacheLocation());
  89. } catch (IOException e) {
  90. throw new RunnerException("Unable to resolve directory", e);
  91. }
  92. runner.execute();
  93. } catch (Exception e) {
  94. displayExecutionResult(stats, "FAILURE");
  95. showError("Error during Sonar runner execution", e, displayStackTrace);
  96. return 1;
  97. }
  98. displayExecutionResult(stats, "SUCCESS");
  99. return 0;
  100. }
  101. private void displayExecutionResult(Stats stats, String resultMsg) {
  102. Logs.info("------------------------------------------------------------------------");
  103. Logs.info("EXECUTION " + resultMsg);
  104. Logs.info("------------------------------------------------------------------------");
  105. stats.stop();
  106. Logs.info("------------------------------------------------------------------------");
  107. }
  108. public void showError(String message, Throwable e, boolean showStackTrace) {
  109. if (showStackTrace) {
  110. Logs.error(message, e);
  111. if (!debugMode) {
  112. Logs.error("");
  113. suggestDebugMode();
  114. }
  115. }
  116. else {
  117. Logs.error(message);
  118. if (e != null) {
  119. Logs.error(e.getMessage());
  120. String previousMsg = "";
  121. for (Throwable cause = e.getCause(); cause != null
  122. && cause.getMessage() != null
  123. && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) {
  124. Logs.error("Caused by: " + cause.getMessage());
  125. previousMsg = cause.getMessage();
  126. }
  127. }
  128. Logs.error("");
  129. Logs.error("To see the full stack trace of the errors, re-run Sonar Runner with the -e switch.");
  130. if (!debugMode) {
  131. suggestDebugMode();
  132. }
  133. }
  134. }
  135. private void suggestDebugMode() {
  136. Logs.error("Re-run Sonar Runner using the -X switch to enable full debug logging.");
  137. }
  138. void loadProperties(Properties argsProperties) {
  139. globalProperties = loadGlobalProperties(argsProperties);
  140. projectProperties = loadProjectProperties(argsProperties);
  141. }
  142. Properties loadGlobalProperties(Properties argsProperties) {
  143. Properties commandLineProps = new Properties();
  144. commandLineProps.putAll(System.getProperties());
  145. commandLineProps.putAll(argsProperties);
  146. Properties result = new Properties();
  147. result.putAll(loadRunnerConfiguration(commandLineProps));
  148. result.putAll(commandLineProps);
  149. return result;
  150. }
  151. Properties loadProjectProperties(Properties argsProperties) {
  152. Properties commandLineProps = new Properties();
  153. commandLineProps.putAll(System.getProperties());
  154. commandLineProps.putAll(argsProperties);
  155. Properties result = new Properties();
  156. result.putAll(loadProjectConfiguration(commandLineProps));
  157. result.putAll(commandLineProps);
  158. if (result.containsKey(PROJECT_HOME)) {
  159. // the real property of the Sonar Runner is "sonar.projectDir"
  160. String baseDir = result.getProperty(PROJECT_HOME);
  161. result.remove(PROJECT_HOME);
  162. result.put(Runner.PROPERTY_SONAR_PROJECT_BASEDIR, baseDir);
  163. }
  164. return result;
  165. }
  166. Properties loadRunnerConfiguration(Properties props) {
  167. File settingsFile = locatePropertiesFile(props, RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS);
  168. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  169. Logs.info("Runner configuration file: " + settingsFile.getAbsolutePath());
  170. return toProperties(settingsFile);
  171. }
  172. Logs.info("Runner configuration file: NONE");
  173. return new Properties();
  174. }
  175. private Properties loadProjectConfiguration(Properties props) {
  176. File settingsFile = locatePropertiesFile(props, PROJECT_HOME, "sonar-project.properties", PROJECT_SETTINGS);
  177. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  178. Logs.info("Project configuration file: " + settingsFile.getAbsolutePath());
  179. return toProperties(settingsFile);
  180. }
  181. Logs.info("Project configuration file: NONE");
  182. return new Properties();
  183. }
  184. private File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
  185. File settingsFile = null;
  186. String runnerHome = props.getProperty(homeKey);
  187. if (runnerHome != null && !"".equals(runnerHome)) {
  188. settingsFile = new File(runnerHome, relativePathFromHome);
  189. }
  190. if (settingsFile == null || !settingsFile.exists()) {
  191. String settingsPath = props.getProperty(settingsKey);
  192. if (settingsPath != null && !"".equals(settingsPath)) {
  193. settingsFile = new File(settingsPath);
  194. }
  195. }
  196. return settingsFile;
  197. }
  198. private Properties toProperties(File file) {
  199. InputStream in = null;
  200. Properties properties = new Properties();
  201. try {
  202. in = new FileInputStream(file);
  203. properties.load(in);
  204. return properties;
  205. } catch (Exception e) {
  206. throw new IllegalStateException("Fail to load file: " + file.getAbsolutePath(), e);
  207. } finally {
  208. IOUtils.closeQuietly(in);
  209. }
  210. }
  211. Properties parseArguments(String[] args) {
  212. int i = 0;
  213. if (args.length > 0 && !args[0].startsWith("-")) {
  214. command = args[0];
  215. i++;
  216. }
  217. else {
  218. command = null;
  219. }
  220. Properties props = new Properties();
  221. for (; i < args.length; i++) {
  222. String arg = args[i];
  223. if ("-h".equals(arg) || "--help".equals(arg)) {
  224. printUsage();
  225. }
  226. else if ("-v".equals(arg) || "--version".equals(arg)) {
  227. displayVersionOnly = true;
  228. }
  229. else if ("-e".equals(arg) || "--errors".equals(arg)) {
  230. displayStackTrace = true;
  231. }
  232. else if ("-X".equals(arg) || "--debug".equals(arg)) {
  233. props.setProperty(Runner.PROPERTY_VERBOSE, "true");
  234. displayStackTrace = true;
  235. debugMode = true;
  236. Logs.setDebugEnabled(true);
  237. }
  238. else if ("-D".equals(arg) || "--define".equals(arg)) {
  239. i++;
  240. if (i >= args.length) {
  241. printError("Missing argument for option --define");
  242. }
  243. arg = args[i];
  244. appendPropertyTo(arg, props);
  245. }
  246. else if (arg.startsWith("-D")) {
  247. arg = arg.substring(2);
  248. appendPropertyTo(arg, props);
  249. }
  250. else {
  251. printError("Unrecognized option: " + arg);
  252. }
  253. }
  254. return props;
  255. }
  256. private void appendPropertyTo(String arg, Properties props) {
  257. final String key, value;
  258. int j = arg.indexOf('=');
  259. if (j == -1) {
  260. key = arg;
  261. value = "true";
  262. } else {
  263. key = arg.substring(0, j);
  264. value = arg.substring(j + 1);
  265. }
  266. if (TASK_COMMAND.equals(key)) {
  267. command = value;
  268. }
  269. else {
  270. props.setProperty(key, value);
  271. }
  272. }
  273. private void printError(String message) {
  274. Logs.error(message);
  275. printUsage();
  276. }
  277. private void printUsage() {
  278. Logs.info("");
  279. Logs.info("usage: sonar-runner [command] [options]");
  280. Logs.info("");
  281. Logs.info("Command:");
  282. Logs.info(" analyse-project Run Sonar analysis task on the current project (default)");
  283. Logs.info(" list-tasks Display all tasks available");
  284. Logs.info("Options:");
  285. Logs.info(" -D,--define <arg> Define property");
  286. Logs.info(" -e,--errors Produce execution error messages");
  287. Logs.info(" -h,--help Display help information");
  288. Logs.info(" -v,--version Display version information");
  289. Logs.info(" -X,--debug Produce execution debug output");
  290. System.exit(0);
  291. }
  292. }