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

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