You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.io.*;
  56. import java.util.*;
  57. /**
  58. * Command line entry point into Ant. This class is entered via the
  59. * cannonical `public static void main` entry point and reads the
  60. * command line arguments. It then assembles and executes an Ant
  61. * project.
  62. * <p>
  63. * If you integrating Ant into some other tool, this is not the class
  64. * to use as an entry point. Please see the source code of this
  65. * class to see how it manipulates the Ant project classes.
  66. *
  67. * @author duncan@x180.com
  68. */
  69. public class Main {
  70. /** Our current message output status. Follows Project.MSG_XXX */
  71. private static int msgOutputLevel = Project.MSG_INFO;
  72. /** File that we are using for configuration */
  73. private static File buildFile = new File("build.xml");
  74. /** Stream that we are using for logging */
  75. private static PrintStream out = System.out;
  76. /** The build targets */
  77. private static Vector targets = new Vector(5);
  78. /** Set of properties that can be used by tasks */
  79. private static Properties definedProps = new Properties();
  80. /**
  81. * Command line entry point. This method kicks off the building
  82. * of a project object and executes a build using either a given
  83. * target or the default target.
  84. *
  85. * @param args Command line args.
  86. */
  87. public static void main(String[] args) {
  88. // cycle through given args
  89. for (int i = 0; i < args.length; i++) {
  90. String arg = args[i];
  91. if (arg.equals("-help") || arg.equals("help")) {
  92. printUsage();
  93. return;
  94. } else if (arg.equals("-quiet") || arg.equals("-q") || arg.equals("q")) {
  95. msgOutputLevel = Project.MSG_WARN;
  96. } else if (arg.equals("-verbose") || arg.equals("-v") || arg.equals("v")) {
  97. msgOutputLevel = Project.MSG_VERBOSE;
  98. } else if (arg.equals("-logfile") || arg.equals("-l") || arg.equals("l")) {
  99. try {
  100. File logFile = new File(args[i+1]);
  101. i++;
  102. out = new PrintStream(new FileOutputStream(logFile));
  103. System.setOut(out);
  104. System.setErr(out);
  105. } catch (IOException ioe) {
  106. String msg = "Cannot write on the specified log file. " +
  107. "Make sure the path exists and you have write permissions.";
  108. System.out.println(msg);
  109. return;
  110. } catch (ArrayIndexOutOfBoundsException aioobe) {
  111. String msg = "You must specify a log file when " +
  112. "using the -log argument";
  113. System.out.println(msg);
  114. return;
  115. }
  116. } else if (arg.equals("-buildfile") || arg.equals("-file") || arg.equals("-f") || arg.equals("f")) {
  117. try {
  118. buildFile = new File(args[i+1]);
  119. i++;
  120. } catch (ArrayIndexOutOfBoundsException aioobe) {
  121. String msg = "You must specify a buildfile when " +
  122. "using the -buildfile argument";
  123. System.out.println(msg);
  124. return;
  125. }
  126. } else if (arg.startsWith("-D")) {
  127. /* Interestingly enough, we get to here when a user
  128. * uses -Dname=value. However, in some cases, the JDK
  129. * goes ahead * and parses this out to args
  130. * {"-Dname", "value"}
  131. * so instead of parsing on "=", we just make the "-D"
  132. * characters go away and skip one argument forward.
  133. *
  134. * I don't know how to predict when the JDK is going
  135. * to help or not, so we simply look for the equals sign.
  136. */
  137. String name = arg.substring(2, arg.length());
  138. String value = null;
  139. int posEq = name.indexOf("=");
  140. if (posEq > 0) {
  141. value = name.substring(posEq+1);
  142. name = name.substring(0, posEq);
  143. } else if (i < args.length)
  144. value = args[++i];
  145. definedProps.put(name, value);
  146. } else if (arg.startsWith("-")) {
  147. // we don't have any more args to recognize!
  148. String msg = "Unknown arg: " + arg;
  149. System.out.println(msg);
  150. printUsage();
  151. return;
  152. } else {
  153. // if it's no other arg, it may be the target
  154. targets.addElement(arg);
  155. }
  156. }
  157. // make sure buildfile exists
  158. if (!buildFile.exists()) {
  159. System.out.println("Buildfile: " + buildFile + " does not exist!");
  160. return;
  161. }
  162. // make sure it's not a directory (this falls into the ultra
  163. // paranoid lets check everything catagory
  164. if (buildFile.isDirectory()) {
  165. System.out.println("What? Buildfile: " + buildFile + " is a dir!");
  166. return;
  167. }
  168. // ok, so if we've made it here, let's run the damn build allready
  169. runBuild();
  170. return;
  171. }
  172. /**
  173. * Executes the build.
  174. */
  175. private static void runBuild() {
  176. // track when we started
  177. long startTime = System.currentTimeMillis();
  178. if (msgOutputLevel >= Project.MSG_INFO) {
  179. System.out.println("Buildfile: " + buildFile);
  180. }
  181. Project project = new Project(out, msgOutputLevel);
  182. // set user-define properties
  183. Enumeration e = definedProps.keys();
  184. while (e.hasMoreElements()) {
  185. String arg = (String)e.nextElement();
  186. String value = (String)definedProps.get(arg);
  187. project.setUserProperty(arg, value);
  188. }
  189. project.setUserProperty( "ant.file" , buildFile.getAbsolutePath() );
  190. // first use the ProjectHelper to create the project object
  191. // from the given build file.
  192. try {
  193. try {
  194. Class.forName("javax.xml.parsers.SAXParserFactory");
  195. ProjectHelper.configureProject(project, buildFile);
  196. } catch (NoClassDefFoundError ncdfe) {
  197. throw new BuildException("No JAXP compliant XML parser found. See http://java.sun.com/xml for the\nreference implementation.", ncdfe);
  198. } catch (ClassNotFoundException cnfe) {
  199. throw new BuildException("No JAXP compliant XML parser found. See http://java.sun.com/xml for the\nreference implementation.", cnfe);
  200. } catch (NullPointerException npe) {
  201. throw new BuildException("No JAXP compliant XML parser found. See http://java.sun.com/xml for the\nreference implementation.", npe);
  202. }
  203. } catch (BuildException be) {
  204. System.out.println("\nBUILD CONFIG ERROR\n");
  205. System.out.println(be.getMessage());
  206. if (be.getException() == null) {
  207. System.out.println(be.toString());
  208. } else {
  209. be.getException().printStackTrace();
  210. }
  211. System.exit(1);
  212. }
  213. // make sure that we have a target to execute
  214. if (targets.size() == 0) {
  215. targets.addElement(project.getDefaultTarget());
  216. }
  217. // actually do some work
  218. try {
  219. Enumeration en = targets.elements();
  220. while (en.hasMoreElements()) {
  221. project.executeTarget((String) en.nextElement());
  222. }
  223. } catch (BuildException be) {
  224. String msg = "\nBUILD FATAL ERROR\n\n";
  225. System.out.println(msg + be.toString());
  226. if (msgOutputLevel > Project.MSG_INFO) {
  227. be.printStackTrace();
  228. }
  229. System.exit(1);
  230. }
  231. // track our stop time and let the user know how long things took.
  232. long finishTime = System.currentTimeMillis();
  233. long elapsedTime = finishTime - startTime;
  234. if (msgOutputLevel >= Project.MSG_INFO) {
  235. System.out.println("Completed in " + (elapsedTime/1000)
  236. + " seconds");
  237. }
  238. }
  239. /**
  240. * Prints the usage of how to use this class to System.out
  241. */
  242. private static void printUsage() {
  243. String lSep = System.getProperty("line.separator");
  244. StringBuffer msg = new StringBuffer();
  245. msg.append("ant [options] [target]" + lSep);
  246. msg.append("Options: " + lSep);
  247. msg.append(" -help print this message" + lSep);
  248. msg.append(" -quiet be extra quiet" + lSep);
  249. msg.append(" -verbose be extra verbose" + lSep);
  250. msg.append(" -logfile <file> use given file for log" + lSep);
  251. msg.append(" -buildfile <file> use given buildfile" + lSep);
  252. msg.append(" -D<property>=<value> use value for given property" + lSep);
  253. System.out.println(msg.toString());
  254. }
  255. }