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 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.BufferedReader;
  56. import java.io.File;
  57. import java.io.InputStreamReader;
  58. import java.util.Properties;
  59. import java.util.Enumeration;
  60. /**
  61. * Command line entry point into Ant. This class is entered via the
  62. * cannonical `public static void main` entry point and reads the
  63. * command line arguments. It then assembles and executes an Ant
  64. * project.
  65. * <p>
  66. * If you integrating Ant into some other tool, this is not the class
  67. * to use as an entry point. Please see the source code of this
  68. * class to see how it manipulates the Ant project classes.
  69. *
  70. * @author duncan@x180.com
  71. */
  72. public class Main {
  73. /** Our current message output status. Follows Project.MSG_XXX */
  74. private static int msgOutputLevel = Project.MSG_INFO;
  75. /** File that we are using for configuration */
  76. private static File buildFile = new File("build.xml");
  77. // XXX
  78. // Change the targets to use a vector or something. I'm not keen
  79. // on the idea of having an artificial limit, even if it isn't
  80. // likely that somebody will want to build more than 20 targets.
  81. private static String targets[] = new String[20];
  82. private static int targetCount=0;
  83. /** Set of properties that can be used by tasks */
  84. private static Properties definedProps = new Properties();
  85. /** The Ant security manager */
  86. private static AntSecurityManager securityManager;
  87. /**
  88. * Command line entry point. This method kicks off the building
  89. * of a project object and executes a build using either a given
  90. * target or the default target.
  91. *
  92. * @param args Command line args.
  93. */
  94. public static void main(String[] args) {
  95. // cycle through given args
  96. for (int i = 0; i < args.length; i++) {
  97. String arg = args[i];
  98. if (arg.equals("-help") || arg.equals("help")) {
  99. printUsage();
  100. return;
  101. } else if (arg.equals("-quiet") || arg.equals("-q") ||
  102. arg.equals("q")) {
  103. msgOutputLevel = Project.MSG_WARN;
  104. } else if (arg.equals("-verbose") || arg.equals("-v") ||
  105. arg.equals("v")) {
  106. msgOutputLevel = Project.MSG_VERBOSE;
  107. } else if (arg.equals("-buildfile") || arg.equals("-file") || arg.equals("-f")) {
  108. try {
  109. buildFile = new File(args[i+1]);
  110. i++;
  111. } catch (ArrayIndexOutOfBoundsException aioobe) {
  112. String msg = "You must specify a buildfile when " +
  113. "using the -buildfile argument";
  114. System.out.println(msg);
  115. return;
  116. }
  117. } else if (arg.startsWith("-D")) {
  118. /* Interestingly enough, we get to here when a user
  119. * uses -Dname=value. However, the JDK goes ahead
  120. * and parses this out to args {"-Dname", "value"}
  121. * so instead of parsing on "=", we just make the "-D"
  122. * characters go away and skip one argument forward.
  123. */
  124. String name = arg.substring(2, arg.length());
  125. String value = args[++i];
  126. definedProps.put(name, value);
  127. } else if (arg.startsWith("-")) {
  128. // we don't have any more args to recognize!
  129. String msg = "Unknown arg: " + arg;
  130. System.out.println(msg);
  131. printUsage();
  132. return;
  133. } else {
  134. // if it's no other arg, it may be the target
  135. targets[targetCount]=arg;
  136. targetCount++;
  137. }
  138. }
  139. // make sure buildfile exists
  140. if (!buildFile.exists()) {
  141. System.out.println("Buildfile: " + buildFile + " does not exist!");
  142. return;
  143. }
  144. // make sure it's not a directory (this falls into the ultra
  145. // paranoid lets check everything catagory
  146. if (buildFile.isDirectory()) {
  147. System.out.println("What? Buildfile: " + buildFile + " is a dir!");
  148. return;
  149. }
  150. // ok, so if we've made it here, let's run the damn build allready
  151. runBuild();
  152. // se should force the exit() to allow everything to cleanup since
  153. // there could be leftover threads running around (some stupid AWT code
  154. // used for image generation does this! grrrr)
  155. exit(0);
  156. }
  157. /**
  158. * Executes the build.
  159. */
  160. private static void runBuild() {
  161. // track when we started
  162. long startTime = System.currentTimeMillis();
  163. if (msgOutputLevel >= Project.MSG_INFO) {
  164. System.out.println("Buildfile: " + buildFile);
  165. }
  166. Project project = new Project();
  167. project.setOutputLevel(msgOutputLevel);
  168. // set user-define properties
  169. Enumeration e = definedProps.keys();
  170. while (e.hasMoreElements()) {
  171. String arg = (String)e.nextElement();
  172. String value = (String)definedProps.get(arg);
  173. project.setUserProperty(arg, value);
  174. }
  175. // first use the ProjectHelper to create the project object
  176. // from the given build file.
  177. try {
  178. ProjectHelper.configureProject(project, buildFile);
  179. } catch (BuildException be) {
  180. String msg = "BUILD CONFIG ERROR: ";
  181. System.out.println(msg + be.getMessage());
  182. be.printStackTrace();
  183. exit(1);
  184. }
  185. // make sure that we have a target to execute
  186. if (targetCount == 0) {
  187. String target = project.getDefaultTarget();
  188. targets[0]=target;
  189. targetCount=1;
  190. }
  191. // set the security manager
  192. securityManager = new AntSecurityManager();
  193. System.setSecurityManager(securityManager);
  194. // actually do some work
  195. try {
  196. for(int i=0; i< targetCount; i++)
  197. project.executeTarget(targets[i]);
  198. } catch (BuildException be) {
  199. String msg = "BUILD FATAL ERROR: ";
  200. System.out.println(msg + be.getMessage());
  201. if (msgOutputLevel > Project.MSG_INFO) {
  202. be.printStackTrace();
  203. }
  204. exit(1);
  205. }
  206. // track our stop time and let the user know how long things
  207. // took.
  208. long finishTime = System.currentTimeMillis();
  209. long elapsedTime = finishTime - startTime;
  210. if (msgOutputLevel >= Project.MSG_INFO) {
  211. System.out.println("Completed in " + (elapsedTime/1000)
  212. + " seconds");
  213. }
  214. }
  215. /**
  216. * Prints the usage of how to use this class to System.out
  217. */
  218. private static void printUsage() {
  219. String lSep = System.getProperty("line.separator");
  220. StringBuffer msg = new StringBuffer();
  221. msg.append("ant [options] [target]" + lSep);
  222. msg.append("Options: " + lSep);
  223. msg.append(" -help print this message" + lSep);
  224. msg.append(" -quiet be extra quiet" + lSep);
  225. msg.append(" -verbose be extra verbose" + lSep);
  226. msg.append(" -buildfile <file> use given buildfile" + lSep);
  227. msg.append(" -D<property>=<value> use value for given property"
  228. + lSep);
  229. System.out.println(msg.toString());
  230. }
  231. private static void exit(int code) {
  232. securityManager.setExit(true);
  233. System.exit(code);
  234. }
  235. }

No Description