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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. try {
  43. Properties props = loadProperties(args);
  44. Runner runner = Runner.create(props);
  45. log("Runner version: " + runner.getRunnerVersion());
  46. log("Server: " + runner.getServerURl());
  47. log("Work directory: " + runner.getWorkDir().getCanonicalPath());
  48. runner.execute();
  49. } catch (IOException e) {
  50. throw new RuntimeException(e);
  51. } finally {
  52. printMemoryUsage();
  53. }
  54. }
  55. private static final long MB = 1024 * 1024;
  56. private static void printMemoryUsage() {
  57. Runtime r = Runtime.getRuntime();
  58. log("Final Memory: " + (r.totalMemory() - r.freeMemory()) / MB + "M/" + r.totalMemory() / MB + "M");
  59. }
  60. static Properties loadProperties(String[] args) {
  61. Properties commandLineProps = new Properties();
  62. commandLineProps.putAll(System.getProperties());
  63. commandLineProps.putAll(parseArguments(args));
  64. Properties result = new Properties();
  65. result.putAll(loadRunnerProperties(commandLineProps));
  66. result.putAll(loadProjectProperties(commandLineProps));
  67. result.putAll(commandLineProps);
  68. return result;
  69. }
  70. static Properties loadRunnerProperties(Properties props) {
  71. File settingsFile = locatePropertiesFile(props, "runner.home", "conf/sonar-runner.properties", "runner.settings");
  72. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  73. log("Runner settings: " + settingsFile.getAbsolutePath());
  74. return toProperties(settingsFile);
  75. }
  76. return new Properties();
  77. }
  78. static Properties loadProjectProperties(Properties props) {
  79. File settingsFile = locatePropertiesFile(props, "project.home", "sonar-project.properties", "project.settings");
  80. if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) {
  81. log("Project settings: " + settingsFile.getAbsolutePath());
  82. return toProperties(settingsFile);
  83. }
  84. return new Properties();
  85. }
  86. private static File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) {
  87. File settingsFile = null;
  88. String runnerHome = props.getProperty(homeKey);
  89. if (runnerHome != null && !"".equals(runnerHome)) {
  90. settingsFile = new File(runnerHome, relativePathFromHome);
  91. }
  92. if (settingsFile == null || !settingsFile.exists()) {
  93. String settingsPath = props.getProperty(settingsKey);
  94. if (settingsPath != null && !"".equals(settingsPath)) {
  95. settingsFile = new File(settingsPath);
  96. }
  97. }
  98. return settingsFile;
  99. }
  100. private static Properties toProperties(File file) {
  101. InputStream in = null;
  102. Properties properties = new Properties();
  103. try {
  104. in = new FileInputStream(file);
  105. properties.load(in);
  106. return properties;
  107. } catch (Exception e) {
  108. throw new BootstrapException(e);
  109. } finally {
  110. BootstrapperIOUtils.closeQuietly(in);
  111. }
  112. }
  113. static Properties parseArguments(String[] args) {
  114. Properties props = new Properties();
  115. for (int i = 0; i < args.length; i++) {
  116. String arg = args[i];
  117. if ("-h".equals(arg) || "--help".equals(arg)) {
  118. printUsage();
  119. } else if ("-X".equals(arg) || "--debug".equals(arg)) {
  120. props.setProperty(Runner.VERBOSE, "true");
  121. } else if ("-D".equals(arg) || "--define".equals(arg)) {
  122. i++;
  123. if (i >= args.length) {
  124. printError("Missing argument for option --define");
  125. }
  126. arg = args[i];
  127. parseProperty(arg, props);
  128. } else if (arg.startsWith("-D")) {
  129. arg = arg.substring(2);
  130. parseProperty(arg, props);
  131. } else {
  132. printError("Unrecognized option: " + arg);
  133. }
  134. }
  135. return props;
  136. }
  137. private static void parseProperty(String arg, Properties props) {
  138. final String key, value;
  139. int j = arg.indexOf('=');
  140. if (j == -1) {
  141. key = arg;
  142. value = "true";
  143. } else {
  144. key = arg.substring(0, j);
  145. value = arg.substring(j + 1);
  146. }
  147. props.setProperty(key, value);
  148. }
  149. private static void printUsage() {
  150. log("");
  151. log("usage: sonar-runner [options]");
  152. log("");
  153. log("Options:");
  154. log(" -h,--help Display help information");
  155. log(" -X,--debug Produce execution debug output");
  156. log(" -D,--define <arg> Define property");
  157. System.exit(0); // NOSONAR
  158. }
  159. private static void printError(String message) {
  160. log("");
  161. log(message);
  162. printUsage();
  163. }
  164. private static void log(String message) {
  165. System.out.println(message); // NOSONAR
  166. }
  167. private Main() {
  168. }
  169. }