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 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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", "Ant", 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.File;
  56. import java.io.FileInputStream;
  57. import java.io.PrintStream;
  58. import java.io.FileOutputStream;
  59. import java.io.IOException;
  60. import java.io.InputStream;
  61. import java.util.Vector;
  62. import java.util.Properties;
  63. import java.util.Enumeration;
  64. /**
  65. * Command line entry point into Ant. This class is entered via the
  66. * cannonical "public static void main" entry point and reads the
  67. * command line arguments. It then assembles and executes an Ant
  68. * project.
  69. * <p>
  70. * If you integrating Ant into some other tool, this is not the class
  71. * to use as an entry point. Please see the source code of this
  72. * class to see how it manipulates the Ant project classes.
  73. *
  74. * @author duncan@x180.com
  75. */
  76. public class Main {
  77. private AntBean ant=new AntBean();
  78. /**
  79. * Indicates if this ant should be run.
  80. */
  81. private boolean readyToRun = false;
  82. /**
  83. * Indicates we should only parse and display the project help information
  84. */
  85. private boolean projectHelp = false;
  86. /**
  87. * Prints the message of the Throwable if it's not null.
  88. */
  89. private static void printMessage(Throwable t) {
  90. String message = t.getMessage();
  91. if (message != null) {
  92. System.err.println(message);
  93. }
  94. }
  95. /**
  96. * Entry point method.
  97. */
  98. public static void start(String[] args, Properties additionalUserProperties,
  99. ClassLoader coreLoader) {
  100. Main m = null;
  101. try {
  102. m = new Main(args);
  103. } catch(Throwable exc) {
  104. printMessage(exc);
  105. System.exit(1);
  106. }
  107. AntBean ant=m.ant;
  108. ant.setCoreLoader( coreLoader );
  109. if (additionalUserProperties != null) {
  110. for (Enumeration e = additionalUserProperties.keys(); e.hasMoreElements(); ) {
  111. String key = (String) e.nextElement();
  112. String property = additionalUserProperties.getProperty(key);
  113. ant.setUserProperty(key, property);
  114. }
  115. }
  116. try {
  117. Project project=ant.getProject();
  118. ant.execute();
  119. System.exit(0);
  120. } catch (BuildException be) {
  121. // ?? What is that, and how should it be implemented
  122. // XXX if (m.err != System.err) {
  123. printMessage(be);
  124. //}
  125. System.exit(1);
  126. } catch(Throwable exc) {
  127. exc.printStackTrace();
  128. printMessage(exc);
  129. System.exit(1);
  130. }
  131. }
  132. /**
  133. * Command line entry point. This method kicks off the building
  134. * of a project object and executes a build using either a given
  135. * target or the default target.
  136. *
  137. * @param args Command line args.
  138. */
  139. public static void main(String[] args) {
  140. start(args, null, null);
  141. }
  142. protected Main(String[] args) throws BuildException {
  143. String searchForThis = null;
  144. // cycle through given args
  145. for (int i = 0; i < args.length; i++) {
  146. String arg = args[i];
  147. if (arg.equals("-help")) {
  148. printUsage();
  149. return;
  150. } else if (arg.equals("-version")) {
  151. printVersion();
  152. return;
  153. } else if (arg.equals("-quiet") || arg.equals("-q")) {
  154. ant.setOutputLevel( Project.MSG_WARN );
  155. } else if (arg.equals("-verbose") || arg.equals("-v")) {
  156. printVersion();
  157. ant.setOutputLevel( Project.MSG_VERBOSE );
  158. } else if (arg.equals("-debug")) {
  159. printVersion();
  160. ant.setOutputLevel( Project.MSG_DEBUG );
  161. } else if (arg.equals("-logfile") || arg.equals("-l")) {
  162. try {
  163. ant.setLogfile( args[i+1] );
  164. i++;
  165. } catch (ArrayIndexOutOfBoundsException aioobe) {
  166. String msg = "You must specify a log file when " +
  167. "using the -log argument";
  168. System.out.println(msg);
  169. return;
  170. }
  171. } else if (arg.equals("-buildfile") || arg.equals("-file") || arg.equals("-f")) {
  172. try {
  173. ant.setBuildfile( args[i+1] );
  174. i++;
  175. } catch (ArrayIndexOutOfBoundsException aioobe) {
  176. String msg = "You must specify a buildfile when " +
  177. "using the -buildfile argument";
  178. System.out.println(msg);
  179. return;
  180. }
  181. } else if (arg.equals("-listener")) {
  182. try {
  183. ant.addListener(args[i+1]);
  184. i++;
  185. } catch (ArrayIndexOutOfBoundsException aioobe) {
  186. String msg = "You must specify a classname when " +
  187. "using the -listener argument";
  188. System.out.println(msg);
  189. return;
  190. }
  191. } else if (arg.startsWith("-D")) {
  192. /* Interestingly enough, we get to here when a user
  193. * uses -Dname=value. However, in some cases, the JDK
  194. * goes ahead * and parses this out to args
  195. * {"-Dname", "value"}
  196. * so instead of parsing on "=", we just make the "-D"
  197. * characters go away and skip one argument forward.
  198. *
  199. * I don't know how to predict when the JDK is going
  200. * to help or not, so we simply look for the equals sign.
  201. */
  202. String name = arg.substring(2, arg.length());
  203. String value = null;
  204. int posEq = name.indexOf("=");
  205. if (posEq > 0) {
  206. value = name.substring(posEq+1);
  207. name = name.substring(0, posEq);
  208. } else if (i < args.length-1) {
  209. value = args[++i];
  210. }
  211. ant.setUserProperty(name, value);
  212. } else if (arg.equals("-logger")) {
  213. try {
  214. ant.setLogger( args[++i] );
  215. } catch (ArrayIndexOutOfBoundsException aioobe) {
  216. System.out.println("You must specify a classname when " +
  217. "using the -logger argument");
  218. return;
  219. }
  220. } else if (arg.equals("-emacs")) {
  221. ant.setEmacs( true );
  222. } else if (arg.equals("-projecthelp")) {
  223. // set the flag to display the targets and quit
  224. projectHelp = true;
  225. } else if (arg.equals("-find")) {
  226. // eat up next arg if present, default to build.xml
  227. if (i < args.length-1) {
  228. ant.setFind( args[++i] );
  229. } else {
  230. ant.setFind( null );
  231. }
  232. } else if (arg.startsWith("-propertyfile")) {
  233. try {
  234. ant.addPropertyfile( args[i+1] );
  235. i++;
  236. } catch (ArrayIndexOutOfBoundsException aioobe) {
  237. String msg = "You must specify a property filename when " +
  238. "using the -propertyfile argument";
  239. System.out.println(msg);
  240. return;
  241. }
  242. } else if (arg.startsWith("-")) {
  243. // we don't have any more args to recognize!
  244. String msg = "Unknown argument: " + arg;
  245. System.out.println(msg);
  246. printUsage();
  247. return;
  248. } else {
  249. // if it's no other arg, it may be the target
  250. ant.addTarget(arg);
  251. }
  252. }
  253. readyToRun = true;
  254. }
  255. protected void addBuildListeners(Project project) {
  256. ant.addBuildListeners( project );
  257. }
  258. /**
  259. * Prints the usage of how to use this class to System.out
  260. */
  261. private static void printUsage() {
  262. String lSep = System.getProperty("line.separator");
  263. StringBuffer msg = new StringBuffer();
  264. msg.append("ant [options] [target [target2 [target3] ...]]" + lSep);
  265. msg.append("Options: " + lSep);
  266. msg.append(" -help print this message" + lSep);
  267. msg.append(" -projecthelp print project help information" + lSep);
  268. msg.append(" -version print the version information and exit" + lSep);
  269. msg.append(" -quiet be extra quiet" + lSep);
  270. msg.append(" -verbose be extra verbose" + lSep);
  271. msg.append(" -debug print debugging information" + lSep);
  272. msg.append(" -emacs produce logging information without adornments" + lSep);
  273. msg.append(" -logfile <file> use given file for log" + lSep);
  274. msg.append(" -logger <classname> the class which is to perform logging" + lSep);
  275. msg.append(" -listener <classname> add an instance of class as a project listener" + lSep);
  276. msg.append(" -buildfile <file> use given buildfile" + lSep);
  277. msg.append(" -D<property>=<value> use value for given property" + lSep);
  278. msg.append(" -propertyfile <name> load all properties from file with -D" + lSep);
  279. msg.append(" properties taking precedence" + lSep);
  280. msg.append(" -find <file> search for buildfile towards the root of the" + lSep);
  281. msg.append(" filesystem and use it" + lSep);
  282. System.out.println(msg.toString());
  283. }
  284. private static void printVersion() throws BuildException {
  285. System.out.println(AntBean.getAntVersion());
  286. }
  287. public static synchronized String getAntVersion() throws BuildException {
  288. return AntBean.getAntVersion();
  289. }
  290. /**
  291. * Print the project description, if any
  292. */
  293. private static void printDescription(Project project) {
  294. if (project.getDescription() != null) {
  295. System.out.println(project.getDescription());
  296. }
  297. }
  298. /**
  299. * Print out a list of all targets in the current buildfile
  300. */
  301. private static void printTargets(Project project, boolean printSubTargets) {
  302. // find the target with the longest name
  303. int maxLength = 0;
  304. Enumeration ptargets = project.getTargets().elements();
  305. String targetName;
  306. String targetDescription;
  307. Target currentTarget;
  308. // split the targets in top-level and sub-targets depending
  309. // on the presence of a description
  310. Vector topNames = new Vector();
  311. Vector topDescriptions = new Vector();
  312. Vector subNames = new Vector();
  313. while (ptargets.hasMoreElements()) {
  314. currentTarget = (Target)ptargets.nextElement();
  315. targetName = currentTarget.getName();
  316. targetDescription = currentTarget.getDescription();
  317. // maintain a sorted list of targets
  318. if (targetDescription == null) {
  319. int pos = findTargetPosition(subNames, targetName);
  320. subNames.insertElementAt(targetName, pos);
  321. } else {
  322. int pos = findTargetPosition(topNames, targetName);
  323. topNames.insertElementAt(targetName, pos);
  324. topDescriptions.insertElementAt(targetDescription, pos);
  325. if (targetName.length() > maxLength) {
  326. maxLength = targetName.length();
  327. }
  328. }
  329. }
  330. printTargets(topNames, topDescriptions, "Main targets:", maxLength);
  331. if( printSubTargets ) {
  332. printTargets(subNames, null, "Subtargets:", 0);
  333. }
  334. String defaultTarget = project.getDefaultTarget();
  335. if (defaultTarget != null && !"".equals(defaultTarget)) { // shouldn't need to check but...
  336. System.out.println( "Default target: " + defaultTarget );
  337. }
  338. }
  339. /**
  340. * Search for the insert position to keep names a sorted list of Strings
  341. */
  342. private static int findTargetPosition(Vector names, String name) {
  343. int res = names.size();
  344. for (int i=0; i<names.size() && res == names.size(); i++) {
  345. if (name.compareTo((String)names.elementAt(i)) < 0) {
  346. res = i;
  347. }
  348. }
  349. return res;
  350. }
  351. /**
  352. * Output a formatted list of target names with an optional description
  353. */
  354. private static void printTargets(Vector names, Vector descriptions, String heading, int maxlen) {
  355. // now, start printing the targets and their descriptions
  356. String lSep = System.getProperty("line.separator");
  357. // got a bit annoyed that I couldn't find a pad function
  358. String spaces = " ";
  359. while (spaces.length()<maxlen) {
  360. spaces += spaces;
  361. }
  362. StringBuffer msg = new StringBuffer();
  363. msg.append(heading + lSep + lSep);
  364. for (int i=0; i<names.size(); i++) {
  365. msg.append(" ");
  366. msg.append(names.elementAt(i));
  367. if (descriptions != null) {
  368. msg.append(spaces.substring(0, maxlen - ((String)names.elementAt(i)).length() + 2));
  369. msg.append(descriptions.elementAt(i));
  370. }
  371. msg.append(lSep);
  372. }
  373. System.out.println(msg.toString());
  374. }
  375. //
  376. /** The default build file name
  377. * @deprecated Use AntBean
  378. */
  379. public final static String DEFAULT_BUILD_FILENAME = "build.xml";
  380. }