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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.batch.bootstrapper.BootstrapException;
  22. import org.sonar.batch.bootstrapper.BootstrapperIOUtils;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  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. public static void main(String[] args) {
  42. long startTime = System.currentTimeMillis();
  43. try {
  44. Properties props = loadProperties(args);
  45. Runner runner = Runner.create(props);
  46. log("Runner version: " + runner.getRunnerVersion());
  47. log("Java version: " + System.getProperty("java.version", "<unknown java version>")
  48. + ", vendor: " + System.getProperty("java.vendor", "<unknown vendor>"));
  49. log("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\"");
  50. log("Server: " + runner.getServerURL());
  51. log("Work directory: " + runner.getWorkDir().getCanonicalPath());
  52. runner.execute();
  53. } catch (IOException e) {
  54. throw new RunnerException(e);
  55. } finally {
  56. printStats(startTime);
  57. }
  58. }
  59. private static void printStats(long startTime) {
  60. long time = System.currentTimeMillis() - startTime;
  61. log("Total time: " + formatTime(time));
  62. System.gc();
  63. Runtime r = Runtime.getRuntime();
  64. long mb = 1024 * 1024;
  65. log("Final Memory: " + (r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M");
  66. }
  67. static String formatTime(long time) {
  68. long h = time / (60 * 60 * 1000);
  69. long m = (time - h * 60 * 60 * 1000) / (60 * 1000);
  70. long s = (time - h * 60 * 60 * 1000 - m * 60 * 1000) / 1000;
  71. long ms = time % 1000;
  72. final String format;
  73. if (h > 0) {
  74. format = "%1$d:%2$02d:%3$02d.%4$03ds";
  75. } else if (m > 0) {
  76. format = "%2$d:%3$02d.%4$03ds";
  77. } else {
  78. format = "%3$d.%4$03ds";
  79. }
  80. return String.format(format, h, m, s, ms);
  81. }
  82. static Properties loadProperties(String[] args) {
  83. Properties commandLineProps = new Properties();
  84. commandLineProps.putAll(System.getProperties());
  85. commandLineProps.putAll(parseArguments(args));
  86. Properties result = new Properties();
  87. result.putAll(loadRunnerProperties(commandLineProps));
  88. result.putAll(loadProjectProperties(commandLineProps));
  89. result.putAll(commandLineProps);
  90. return result;
  91. }
  92. static Properties loadRunnerProperties(Properties props) {
  93. File settingsFile = locatePropertiesFile(props, "runner.home", "conf/sonar-runner.properties", "runner.settings");
  94. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  95. log("Runner configuration file: " + settingsFile.getAbsolutePath());
  96. return toProperties(settingsFile);
  97. } else {
  98. log("Runner configuration file: NONE");
  99. }
  100. return new Properties();
  101. }
  102. static Properties loadProjectProperties(Properties props) {
  103. File settingsFile = locatePropertiesFile(props, "project.home", "sonar-project.properties", "project.settings");
  104. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  105. log("Project configuration file: " + settingsFile.getAbsolutePath());
  106. return toProperties(settingsFile);
  107. } else {
  108. log("Project configuration file: NONE");
  109. }
  110. return new Properties();
  111. }
  112. private static File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
  113. File settingsFile = null;
  114. String runnerHome = props.getProperty(homeKey);
  115. if (runnerHome != null && !"".equals(runnerHome)) {
  116. settingsFile = new File(runnerHome, relativePathFromHome);
  117. }
  118. if (settingsFile == null || !settingsFile.exists()) {
  119. String settingsPath = props.getProperty(settingsKey);
  120. if (settingsPath != null && !"".equals(settingsPath)) {
  121. settingsFile = new File(settingsPath);
  122. }
  123. }
  124. return settingsFile;
  125. }
  126. private static Properties toProperties(File file) {
  127. InputStream in = null;
  128. Properties properties = new Properties();
  129. try {
  130. in = new FileInputStream(file);
  131. properties.load(in);
  132. return properties;
  133. } catch (Exception e) {
  134. throw new BootstrapException(e);
  135. } finally {
  136. BootstrapperIOUtils.closeQuietly(in);
  137. }
  138. }
  139. static Properties parseArguments(String[] args) {
  140. Properties props = new Properties();
  141. for (int i = 0; i < args.length; i++) {
  142. String arg = args[i];
  143. if ("-h".equals(arg) || "--help".equals(arg)) {
  144. printUsage();
  145. } else if ("-X".equals(arg) || "--debug".equals(arg)) {
  146. props.setProperty(Runner.VERBOSE, "true");
  147. } else if ("-D".equals(arg) || "--define".equals(arg)) {
  148. i++;
  149. if (i >= args.length) {
  150. printError("Missing argument for option --define");
  151. }
  152. arg = args[i];
  153. parseProperty(arg, props);
  154. } else if (arg.startsWith("-D")) {
  155. arg = arg.substring(2);
  156. parseProperty(arg, props);
  157. } else {
  158. printError("Unrecognized option: " + arg);
  159. }
  160. }
  161. return props;
  162. }
  163. private static void parseProperty(String arg, Properties props) {
  164. final String key, value;
  165. int j = arg.indexOf('=');
  166. if (j == -1) {
  167. key = arg;
  168. value = "true";
  169. } else {
  170. key = arg.substring(0, j);
  171. value = arg.substring(j + 1);
  172. }
  173. props.setProperty(key, value);
  174. }
  175. private static void printUsage() {
  176. log("");
  177. log("usage: sonar-runner [options]");
  178. log("");
  179. log("Options:");
  180. log(" -h,--help Display help information");
  181. log(" -X,--debug Produce execution debug output");
  182. log(" -D,--define <arg> Define property");
  183. System.exit(0); // NOSONAR
  184. }
  185. private static void printError(String message) {
  186. log("");
  187. log(message);
  188. printUsage();
  189. }
  190. private static void log(String message) {
  191. System.out.println(message); // NOSONAR
  192. }
  193. private Main() {
  194. }
  195. }