您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Main.java 7.6KB

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