Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Main.java 7.5KB

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