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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Sonar Standalone Runner
  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.sonar.runner.bootstrapper.utils.PrivateIOUtils;
  22. import org.sonar.runner.bootstrapper.BootstrapException;
  23. import org.sonar.runner.utils.SonarRunnerVersion;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.util.Properties;
  29. /**
  30. * Arguments :
  31. * <ul>
  32. * <li>runner.home: optional path to runner home (root directory with sub-directories bin, lib and conf)</li>
  33. * <li>runner.settings: optional path to runner global settings, usually ${runner.home}/conf/sonar-runner.properties.
  34. * This property is used only if ${runner.home} is not defined</li>
  35. * <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>
  36. * <li>project.settings: optional path to project settings. Default value is ${project.home}/sonar-project.properties.</li>
  37. * </ul>
  38. *
  39. * @since 1.0
  40. */
  41. public final class Main {
  42. private static final String RUNNER_HOME = "runner.home";
  43. private static final String RUNNER_SETTINGS = "runner.settings";
  44. private static final String PROJECT_HOME = "project.home";
  45. private static final String PROJECT_SETTINGS = "project.settings";
  46. private static boolean debugMode = false;
  47. /**
  48. * Entry point of the program.
  49. */
  50. public static void main(String[] args) {
  51. long startTime = System.currentTimeMillis();
  52. try {
  53. Properties props = loadProperties(args);
  54. Runner runner = Runner.create(props);
  55. log("Runner version: " + SonarRunnerVersion.getVersion());
  56. log("Java version: " + System.getProperty("java.version", "<unknown java version>")
  57. + ", vendor: " + System.getProperty("java.vendor", "<unknown vendor>"));
  58. log("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\"");
  59. if (debugMode) {
  60. log("Other system properties:");
  61. log(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\"");
  62. }
  63. log("Server: " + runner.getSonarServerURL());
  64. try {
  65. log("Work directory: " + runner.getWorkDir().getCanonicalPath());
  66. } catch (IOException e) {
  67. throw new RunnerException(e);
  68. }
  69. runner.execute();
  70. } finally {
  71. printStats(startTime);
  72. }
  73. }
  74. private static void printStats(long startTime) {
  75. long time = System.currentTimeMillis() - startTime;
  76. log("Total time: " + formatTime(time));
  77. System.gc();
  78. Runtime r = Runtime.getRuntime();
  79. long mb = 1024 * 1024;
  80. log("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M");
  81. }
  82. static String formatTime(long time) {
  83. long h = time / (60 * 60 * 1000);
  84. long m = (time - h * 60 * 60 * 1000) / (60 * 1000);
  85. long s = (time - h * 60 * 60 * 1000 - m * 60 * 1000) / 1000;
  86. long ms = time % 1000;
  87. final String format;
  88. if (h > 0) {
  89. format = "%1$d:%2$02d:%3$02d.%4$03ds";
  90. } else if (m > 0) {
  91. format = "%2$d:%3$02d.%4$03ds";
  92. } else {
  93. format = "%3$d.%4$03ds";
  94. }
  95. return String.format(format, h, m, s, ms);
  96. }
  97. static Properties loadProperties(String[] args) {
  98. Properties commandLineProps = new Properties();
  99. commandLineProps.putAll(System.getProperties());
  100. commandLineProps.putAll(parseArguments(args));
  101. Properties result = new Properties();
  102. result.putAll(loadRunnerProperties(commandLineProps));
  103. result.putAll(loadProjectProperties(commandLineProps));
  104. result.putAll(commandLineProps);
  105. if (result.contains(PROJECT_HOME)) {
  106. // the real property of the Sonar Runner is "sonar.projectDir"
  107. String baseDir = result.getProperty(PROJECT_HOME);
  108. result.remove(PROJECT_HOME);
  109. result.put(Runner.PROPERTY_SONAR_PROJECT_BASEDIR, baseDir);
  110. }
  111. return result;
  112. }
  113. static Properties loadRunnerProperties(Properties props) {
  114. File settingsFile = locatePropertiesFile(props, RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS);
  115. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  116. log("Runner configuration file: " + settingsFile.getAbsolutePath());
  117. return toProperties(settingsFile);
  118. } else {
  119. log("Runner configuration file: NONE");
  120. }
  121. return new Properties();
  122. }
  123. static Properties loadProjectProperties(Properties props) {
  124. File settingsFile = locatePropertiesFile(props, PROJECT_HOME, "sonar-project.properties", PROJECT_SETTINGS);
  125. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  126. log("Project configuration file: " + settingsFile.getAbsolutePath());
  127. return toProperties(settingsFile);
  128. } else {
  129. log("Project configuration file: NONE");
  130. }
  131. return new Properties();
  132. }
  133. private static File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
  134. File settingsFile = null;
  135. String runnerHome = props.getProperty(homeKey);
  136. if (runnerHome != null && !"".equals(runnerHome)) {
  137. settingsFile = new File(runnerHome, relativePathFromHome);
  138. }
  139. if (settingsFile == null || !settingsFile.exists()) {
  140. String settingsPath = props.getProperty(settingsKey);
  141. if (settingsPath != null && !"".equals(settingsPath)) {
  142. settingsFile = new File(settingsPath);
  143. }
  144. }
  145. return settingsFile;
  146. }
  147. private static Properties toProperties(File file) {
  148. InputStream in = null;
  149. Properties properties = new Properties();
  150. try {
  151. in = new FileInputStream(file);
  152. properties.load(in);
  153. return properties;
  154. } catch (Exception e) {
  155. throw new BootstrapException(e);
  156. } finally {
  157. PrivateIOUtils.closeQuietly(in);
  158. }
  159. }
  160. static Properties parseArguments(String[] args) {
  161. Properties props = new Properties();
  162. for (int i = 0; i < args.length; i++) {
  163. String arg = args[i];
  164. if ("-h".equals(arg) || "--help".equals(arg)) {
  165. printUsage();
  166. } else if ("-X".equals(arg) || "--debug".equals(arg)) {
  167. props.setProperty(Runner.PROPERTY_VERBOSE, "true");
  168. debugMode = true;
  169. } else if ("-D".equals(arg) || "--define".equals(arg)) {
  170. i++;
  171. if (i >= args.length) {
  172. printError("Missing argument for option --define");
  173. }
  174. arg = args[i];
  175. parseProperty(arg, props);
  176. } else if (arg.startsWith("-D")) {
  177. arg = arg.substring(2);
  178. parseProperty(arg, props);
  179. } else {
  180. printError("Unrecognized option: " + arg);
  181. }
  182. }
  183. return props;
  184. }
  185. private static void parseProperty(String arg, Properties props) {
  186. final String key, value;
  187. int j = arg.indexOf('=');
  188. if (j == -1) {
  189. key = arg;
  190. value = "true";
  191. } else {
  192. key = arg.substring(0, j);
  193. value = arg.substring(j + 1);
  194. }
  195. props.setProperty(key, value);
  196. }
  197. private static void printUsage() {
  198. log("");
  199. log("usage: sonar-runner [options]");
  200. log("");
  201. log("Options:");
  202. log(" -h,--help Display help information");
  203. log(" -X,--debug Produce execution debug output");
  204. log(" -D,--define <arg> Define property");
  205. System.exit(0); // NOSONAR
  206. }
  207. private static void printError(String message) {
  208. log("");
  209. log(message);
  210. printUsage();
  211. }
  212. private static void log(String message) {
  213. System.out.println(message); // NOSONAR
  214. }
  215. private Main() {
  216. }
  217. }