1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.PrintStream;
  24. import java.util.Enumeration;
  25. import java.util.Properties;
  26. import java.util.Vector;
  27. import org.apache.tools.ant.input.DefaultInputHandler;
  28. import org.apache.tools.ant.input.InputHandler;
  29. import org.apache.tools.ant.util.JavaEnvUtils;
  30. import org.apache.tools.ant.launch.AntMain;
  31. /**
  32. * Command line entry point into Ant. This class is entered via the
  33. * canonical `public static void main` entry point and reads the
  34. * command line arguments. It then assembles and executes an Ant
  35. * project.
  36. * <p>
  37. * If you integrating Ant into some other tool, this is not the class
  38. * to use as an entry point. Please see the source code of this
  39. * class to see how it manipulates the Ant project classes.
  40. *
  41. */
  42. public class Main implements AntMain {
  43. /** The default build file name. */
  44. public static final String DEFAULT_BUILD_FILENAME = "build.xml";
  45. /** Our current message output status. Follows Project.MSG_XXX. */
  46. private int msgOutputLevel = Project.MSG_INFO;
  47. /** File that we are using for configuration. */
  48. private File buildFile; /* null */
  49. /** Stream to use for logging. */
  50. private static PrintStream out = System.out;
  51. /** Stream that we are using for logging error messages. */
  52. private static PrintStream err = System.err;
  53. /** The build targets. */
  54. private Vector targets = new Vector();
  55. /** Set of properties that can be used by tasks. */
  56. private Properties definedProps = new Properties();
  57. /** Names of classes to add as listeners to project. */
  58. private Vector listeners = new Vector(1);
  59. /** File names of property files to load on startup. */
  60. private Vector propertyFiles = new Vector(1);
  61. /** Indicates whether this build is to support interactive input */
  62. private boolean allowInput = true;
  63. /** keep going mode */
  64. private boolean keepGoingMode = false;
  65. /**
  66. * The Ant logger class. There may be only one logger. It will have
  67. * the right to use the 'out' PrintStream. The class must implements the
  68. * BuildLogger interface.
  69. */
  70. private String loggerClassname = null;
  71. /**
  72. * The Ant InputHandler class. There may be only one input
  73. * handler.
  74. */
  75. private String inputHandlerClassname = null;
  76. /**
  77. * Whether or not output to the log is to be unadorned.
  78. */
  79. private boolean emacsMode = false;
  80. /**
  81. * Whether or not this instance has successfully been
  82. * constructed and is ready to run.
  83. */
  84. private boolean readyToRun = false;
  85. /**
  86. * Whether or not we should only parse and display the project help
  87. * information.
  88. */
  89. private boolean projectHelp = false;
  90. /**
  91. * Whether or not a logfile is being used. This is used to
  92. * check if the output streams must be closed.
  93. */
  94. private static boolean isLogFileUsed = false;
  95. /**
  96. * optional thread priority
  97. */
  98. private Integer threadPriority=null;
  99. /**
  100. * Prints the message of the Throwable if it (the message) is not
  101. * <code>null</code>.
  102. *
  103. * @param t Throwable to print the message of.
  104. * Must not be <code>null</code>.
  105. */
  106. private static void printMessage(Throwable t) {
  107. String message = t.getMessage();
  108. if (message != null) {
  109. System.err.println(message);
  110. }
  111. }
  112. /**
  113. * Creates a new instance of this class using the
  114. * arguments specified, gives it any extra user properties which have been
  115. * specified, and then runs the build using the classloader provided.
  116. *
  117. * @param args Command line arguments. Must not be <code>null</code>.
  118. * @param additionalUserProperties Any extra properties to use in this
  119. * build. May be <code>null</code>, which is the equivalent to
  120. * passing in an empty set of properties.
  121. * @param coreLoader Classloader used for core classes. May be
  122. * <code>null</code> in which case the system classloader is used.
  123. */
  124. public static void start(String[] args, Properties additionalUserProperties,
  125. ClassLoader coreLoader) {
  126. Main m = new Main();
  127. m.startAnt(args, additionalUserProperties, coreLoader);
  128. }
  129. /**
  130. * Start Ant
  131. * @param args command line args
  132. * @param additionalUserProperties properties to set beyond those that
  133. * may be specified on the args list
  134. * @param coreLoader - not used
  135. *
  136. * @since Ant 1.6
  137. */
  138. public void startAnt(String[] args, Properties additionalUserProperties,
  139. ClassLoader coreLoader) {
  140. try {
  141. Diagnostics.validateVersion();
  142. processArgs(args);
  143. } catch (Throwable exc) {
  144. handleLogfile();
  145. printMessage(exc);
  146. System.exit(1);
  147. }
  148. if (additionalUserProperties != null) {
  149. for (Enumeration e = additionalUserProperties.keys();
  150. e.hasMoreElements();) {
  151. String key = (String) e.nextElement();
  152. String property = additionalUserProperties.getProperty(key);
  153. definedProps.put(key, property);
  154. }
  155. }
  156. // expect the worst
  157. int exitCode = 1;
  158. try {
  159. try {
  160. runBuild(coreLoader);
  161. exitCode = 0;
  162. } catch (ExitStatusException ese) {
  163. exitCode = ese.getStatus();
  164. if (exitCode != 0) {
  165. throw ese;
  166. }
  167. }
  168. } catch (BuildException be) {
  169. if (err != System.err) {
  170. printMessage(be);
  171. }
  172. } catch (Throwable exc) {
  173. exc.printStackTrace();
  174. printMessage(exc);
  175. } finally {
  176. handleLogfile();
  177. }
  178. System.exit(exitCode);
  179. }
  180. /**
  181. * Close logfiles, if we have been writing to them.
  182. *
  183. * @since Ant 1.6
  184. */
  185. private static void handleLogfile() {
  186. if (isLogFileUsed) {
  187. if (out != null) {
  188. try {
  189. out.close();
  190. } catch (final Exception e) {
  191. //ignore
  192. }
  193. }
  194. if (err != null) {
  195. try {
  196. err.close();
  197. } catch (final Exception e) {
  198. //ignore
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * Command line entry point. This method kicks off the building
  205. * of a project object and executes a build using either a given
  206. * target or the default target.
  207. *
  208. * @param args Command line arguments. Must not be <code>null</code>.
  209. */
  210. public static void main(String[] args) {
  211. start(args, null, null);
  212. }
  213. /**
  214. * Constructor used when creating Main for later arg processing
  215. * and startup
  216. */
  217. public Main() {
  218. }
  219. /**
  220. * Sole constructor, which parses and deals with command line
  221. * arguments.
  222. *
  223. * @param args Command line arguments. Must not be <code>null</code>.
  224. *
  225. * @exception BuildException if the specified build file doesn't exist
  226. * or is a directory.
  227. *
  228. * @deprecated
  229. */
  230. protected Main(String[] args) throws BuildException {
  231. processArgs(args);
  232. }
  233. /**
  234. * Process command line arguments.
  235. * When ant is started from Launcher, the -lib argument does not get
  236. * passed through to this routine.
  237. *
  238. * @param args the command line arguments.
  239. *
  240. * @since Ant 1.6
  241. */
  242. private void processArgs(String[] args) {
  243. String searchForThis = null;
  244. PrintStream logTo = null;
  245. // cycle through given args
  246. for (int i = 0; i < args.length; i++) {
  247. String arg = args[i];
  248. if (arg.equals("-help") || arg.equals("-h")) {
  249. printUsage();
  250. return;
  251. } else if (arg.equals("-version")) {
  252. printVersion();
  253. return;
  254. } else if (arg.equals("-diagnostics")) {
  255. Diagnostics.doReport(System.out);
  256. return;
  257. } else if (arg.equals("-quiet") || arg.equals("-q")) {
  258. msgOutputLevel = Project.MSG_WARN;
  259. } else if (arg.equals("-verbose") || arg.equals("-v")) {
  260. printVersion();
  261. msgOutputLevel = Project.MSG_VERBOSE;
  262. } else if (arg.equals("-debug") || arg.equals("-d")) {
  263. printVersion();
  264. msgOutputLevel = Project.MSG_DEBUG;
  265. } else if (arg.equals("-noinput")) {
  266. allowInput = false;
  267. } else if (arg.equals("-logfile") || arg.equals("-l")) {
  268. try {
  269. File logFile = new File(args[i + 1]);
  270. i++;
  271. logTo = new PrintStream(new FileOutputStream(logFile));
  272. isLogFileUsed = true;
  273. } catch (IOException ioe) {
  274. String msg = "Cannot write on the specified log file. "
  275. + "Make sure the path exists and you have write "
  276. + "permissions.";
  277. throw new BuildException(msg);
  278. } catch (ArrayIndexOutOfBoundsException aioobe) {
  279. String msg = "You must specify a log file when "
  280. + "using the -log argument";
  281. throw new BuildException(msg);
  282. }
  283. } else if (arg.equals("-buildfile") || arg.equals("-file")
  284. || arg.equals("-f")) {
  285. try {
  286. buildFile = new File(args[i + 1].replace('/', File.separatorChar));
  287. i++;
  288. } catch (ArrayIndexOutOfBoundsException aioobe) {
  289. String msg = "You must specify a buildfile when "
  290. + "using the -buildfile argument";
  291. throw new BuildException(msg);
  292. }
  293. } else if (arg.equals("-listener")) {
  294. try {
  295. listeners.addElement(args[i + 1]);
  296. i++;
  297. } catch (ArrayIndexOutOfBoundsException aioobe) {
  298. String msg = "You must specify a classname when "
  299. + "using the -listener argument";
  300. throw new BuildException(msg);
  301. }
  302. } else if (arg.startsWith("-D")) {
  303. /* Interestingly enough, we get to here when a user
  304. * uses -Dname=value. However, in some cases, the OS
  305. * goes ahead and parses this out to args
  306. * {"-Dname", "value"}
  307. * so instead of parsing on "=", we just make the "-D"
  308. * characters go away and skip one argument forward.
  309. *
  310. * I don't know how to predict when the JDK is going
  311. * to help or not, so we simply look for the equals sign.
  312. */
  313. String name = arg.substring(2, arg.length());
  314. String value = null;
  315. int posEq = name.indexOf("=");
  316. if (posEq > 0) {
  317. value = name.substring(posEq + 1);
  318. name = name.substring(0, posEq);
  319. } else if (i < args.length - 1) {
  320. value = args[++i];
  321. } else {
  322. throw new BuildException("Missing value for property "
  323. + name);
  324. }
  325. definedProps.put(name, value);
  326. } else if (arg.equals("-logger")) {
  327. if (loggerClassname != null) {
  328. throw new BuildException("Only one logger class may "
  329. + " be specified.");
  330. }
  331. try {
  332. loggerClassname = args[++i];
  333. } catch (ArrayIndexOutOfBoundsException aioobe) {
  334. throw new BuildException("You must specify a classname when"
  335. + " using the -logger argument");
  336. }
  337. } else if (arg.equals("-inputhandler")) {
  338. if (inputHandlerClassname != null) {
  339. throw new BuildException("Only one input handler class may "
  340. + "be specified.");
  341. }
  342. try {
  343. inputHandlerClassname = args[++i];
  344. } catch (ArrayIndexOutOfBoundsException aioobe) {
  345. throw new BuildException("You must specify a classname when"
  346. + " using the -inputhandler"
  347. + " argument");
  348. }
  349. } else if (arg.equals("-emacs") || arg.equals("-e")) {
  350. emacsMode = true;
  351. } else if (arg.equals("-projecthelp") || arg.equals("-p")) {
  352. // set the flag to display the targets and quit
  353. projectHelp = true;
  354. } else if (arg.equals("-find") || arg.equals("-s")) {
  355. // eat up next arg if present, default to build.xml
  356. if (i < args.length - 1) {
  357. searchForThis = args[++i];
  358. } else {
  359. searchForThis = DEFAULT_BUILD_FILENAME;
  360. }
  361. } else if (arg.startsWith("-propertyfile")) {
  362. try {
  363. propertyFiles.addElement(args[i + 1]);
  364. i++;
  365. } catch (ArrayIndexOutOfBoundsException aioobe) {
  366. String msg = "You must specify a property filename when "
  367. + "using the -propertyfile argument";
  368. throw new BuildException(msg);
  369. }
  370. } else if (arg.equals("-k") || arg.equals("-keep-going")) {
  371. keepGoingMode = true;
  372. } else if (arg.equals("-nice")) {
  373. try {
  374. threadPriority=Integer.decode(args[i + 1]);
  375. } catch (ArrayIndexOutOfBoundsException aioobe) {
  376. throw new BuildException(
  377. "You must supply a niceness value (1-10)"+
  378. " after the -nice option");
  379. } catch (NumberFormatException e) {
  380. throw new BuildException("Unrecognized niceness value: " +
  381. args[i + 1]);
  382. }
  383. i++;
  384. if(threadPriority.intValue()<Thread.MIN_PRIORITY ||
  385. threadPriority.intValue()>Thread.MAX_PRIORITY) {
  386. throw new BuildException(
  387. "Niceness value is out of the range 1-10");
  388. }
  389. } else if (arg.startsWith("-")) {
  390. // we don't have any more args to recognize!
  391. String msg = "Unknown argument: " + arg;
  392. System.out.println(msg);
  393. printUsage();
  394. throw new BuildException("");
  395. } else {
  396. // if it's no other arg, it may be the target
  397. targets.addElement(arg);
  398. }
  399. }
  400. // if buildFile was not specified on the command line,
  401. if (buildFile == null) {
  402. // but -find then search for it
  403. if (searchForThis != null) {
  404. buildFile = findBuildFile(System.getProperty("user.dir"),
  405. searchForThis);
  406. } else {
  407. buildFile = new File(DEFAULT_BUILD_FILENAME);
  408. }
  409. }
  410. // make sure buildfile exists
  411. if (!buildFile.exists()) {
  412. System.out.println("Buildfile: " + buildFile + " does not exist!");
  413. throw new BuildException("Build failed");
  414. }
  415. // make sure it's not a directory (this falls into the ultra
  416. // paranoid lets check everything category
  417. if (buildFile.isDirectory()) {
  418. System.out.println("What? Buildfile: " + buildFile + " is a dir!");
  419. throw new BuildException("Build failed");
  420. }
  421. // Load the property files specified by -propertyfile
  422. for (int propertyFileIndex = 0;
  423. propertyFileIndex < propertyFiles.size();
  424. propertyFileIndex++) {
  425. String filename
  426. = (String) propertyFiles.elementAt(propertyFileIndex);
  427. Properties props = new Properties();
  428. FileInputStream fis = null;
  429. try {
  430. fis = new FileInputStream(filename);
  431. props.load(fis);
  432. } catch (IOException e) {
  433. System.out.println("Could not load property file "
  434. + filename + ": " + e.getMessage());
  435. } finally {
  436. if (fis != null) {
  437. try {
  438. fis.close();
  439. } catch (IOException e) {
  440. // ignore
  441. }
  442. }
  443. }
  444. // ensure that -D properties take precedence
  445. Enumeration propertyNames = props.propertyNames();
  446. while (propertyNames.hasMoreElements()) {
  447. String name = (String) propertyNames.nextElement();
  448. if (definedProps.getProperty(name) == null) {
  449. definedProps.put(name, props.getProperty(name));
  450. }
  451. }
  452. }
  453. if (msgOutputLevel >= Project.MSG_INFO) {
  454. System.out.println("Buildfile: " + buildFile);
  455. }
  456. if (logTo != null) {
  457. out = logTo;
  458. err = logTo;
  459. System.setOut(out);
  460. System.setErr(err);
  461. }
  462. readyToRun = true;
  463. }
  464. /**
  465. * Helper to get the parent file for a given file.
  466. * <p>
  467. * Added to simulate File.getParentFile() from JDK 1.2.
  468. * @deprecated
  469. *
  470. * @param file File to find parent of. Must not be <code>null</code>.
  471. * @return Parent file or null if none
  472. */
  473. private File getParentFile(File file) {
  474. File parent = file.getParentFile();
  475. if (parent != null && msgOutputLevel >= Project.MSG_VERBOSE) {
  476. System.out.println("Searching in " + parent.getAbsolutePath());
  477. }
  478. return parent;
  479. }
  480. /**
  481. * Search parent directories for the build file.
  482. * <p>
  483. * Takes the given target as a suffix to append to each
  484. * parent directory in search of a build file. Once the
  485. * root of the file-system has been reached an exception
  486. * is thrown.
  487. *
  488. * @param start Leaf directory of search.
  489. * Must not be <code>null</code>.
  490. * @param suffix Suffix filename to look for in parents.
  491. * Must not be <code>null</code>.
  492. *
  493. * @return A handle to the build file if one is found
  494. *
  495. * @exception BuildException if no build file is found
  496. */
  497. private File findBuildFile(String start, String suffix)
  498. throws BuildException {
  499. if (msgOutputLevel >= Project.MSG_INFO) {
  500. System.out.println("Searching for " + suffix + " ...");
  501. }
  502. File parent = new File(new File(start).getAbsolutePath());
  503. File file = new File(parent, suffix);
  504. // check if the target file exists in the current directory
  505. while (!file.exists()) {
  506. // change to parent directory
  507. parent = getParentFile(parent);
  508. // if parent is null, then we are at the root of the fs,
  509. // complain that we can't find the build file.
  510. if (parent == null) {
  511. throw new BuildException("Could not locate a build file!");
  512. }
  513. // refresh our file handle
  514. file = new File(parent, suffix);
  515. }
  516. return file;
  517. }
  518. /**
  519. * Executes the build. If the constructor for this instance failed
  520. * (e.g. returned after issuing a warning), this method returns
  521. * immediately.
  522. *
  523. * @param coreLoader The classloader to use to find core classes.
  524. * May be <code>null</code>, in which case the
  525. * system classloader is used.
  526. *
  527. * @exception BuildException if the build fails
  528. */
  529. private void runBuild(ClassLoader coreLoader) throws BuildException {
  530. if (!readyToRun) {
  531. return;
  532. }
  533. final Project project = new Project();
  534. project.setCoreLoader(coreLoader);
  535. Throwable error = null;
  536. try {
  537. addBuildListeners(project);
  538. addInputHandler(project);
  539. PrintStream err = System.err;
  540. PrintStream out = System.out;
  541. InputStream in = System.in;
  542. // use a system manager that prevents from System.exit()
  543. // only in JDK > 1.1
  544. SecurityManager oldsm = null;
  545. if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_0)
  546. && !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
  547. oldsm = System.getSecurityManager();
  548. //SecurityManager can not be installed here for backwards
  549. //compatibility reasons (PD). Needs to be loaded prior to
  550. //ant class if we are going to implement it.
  551. //System.setSecurityManager(new NoExitSecurityManager());
  552. }
  553. try {
  554. if (allowInput) {
  555. project.setDefaultInputStream(System.in);
  556. }
  557. System.setIn(new DemuxInputStream(project));
  558. System.setOut(new PrintStream(new DemuxOutputStream(project, false)));
  559. System.setErr(new PrintStream(new DemuxOutputStream(project, true)));
  560. if (!projectHelp) {
  561. project.fireBuildStarted();
  562. }
  563. // set the thread priorities
  564. if (threadPriority != null) {
  565. try {
  566. project.log("Setting Ant's thread priority to "
  567. + threadPriority,Project.MSG_VERBOSE);
  568. Thread.currentThread().setPriority(threadPriority.intValue());
  569. } catch (SecurityException swallowed) {
  570. //we cannot set the priority here.
  571. project.log("A security manager refused to set the -nice value");
  572. }
  573. }
  574. project.init();
  575. project.setUserProperty("ant.version", getAntVersion());
  576. // set user-define properties
  577. Enumeration e = definedProps.keys();
  578. while (e.hasMoreElements()) {
  579. String arg = (String) e.nextElement();
  580. String value = (String) definedProps.get(arg);
  581. project.setUserProperty(arg, value);
  582. }
  583. project.setUserProperty("ant.file",
  584. buildFile.getAbsolutePath());
  585. project.setKeepGoingMode(keepGoingMode);
  586. ProjectHelper.configureProject(project, buildFile);
  587. if (projectHelp) {
  588. printDescription(project);
  589. printTargets(project, msgOutputLevel > Project.MSG_INFO);
  590. return;
  591. }
  592. // make sure that we have a target to execute
  593. if (targets.size() == 0) {
  594. if (project.getDefaultTarget() != null) {
  595. targets.addElement(project.getDefaultTarget());
  596. }
  597. }
  598. project.executeTargets(targets);
  599. } finally {
  600. // put back the original security manager
  601. //The following will never eval to true. (PD)
  602. if (oldsm != null) {
  603. System.setSecurityManager(oldsm);
  604. }
  605. System.setOut(out);
  606. System.setErr(err);
  607. System.setIn(in);
  608. }
  609. } catch (RuntimeException exc) {
  610. error = exc;
  611. throw exc;
  612. } catch (Error err) {
  613. error = err;
  614. throw err;
  615. } finally {
  616. if (!projectHelp) {
  617. project.fireBuildFinished(error);
  618. } else if (error != null) {
  619. project.log(error.toString(), Project.MSG_ERR);
  620. }
  621. }
  622. }
  623. /**
  624. * Adds the listeners specified in the command line arguments,
  625. * along with the default listener, to the specified project.
  626. *
  627. * @param project The project to add listeners to.
  628. * Must not be <code>null</code>.
  629. */
  630. protected void addBuildListeners(Project project) {
  631. // Add the default listener
  632. project.addBuildListener(createLogger());
  633. for (int i = 0; i < listeners.size(); i++) {
  634. String className = (String) listeners.elementAt(i);
  635. try {
  636. BuildListener listener =
  637. (BuildListener) Class.forName(className).newInstance();
  638. if (project != null) {
  639. project.setProjectReference(listener);
  640. }
  641. project.addBuildListener(listener);
  642. } catch (Throwable exc) {
  643. throw new BuildException("Unable to instantiate listener "
  644. + className, exc);
  645. }
  646. }
  647. }
  648. /**
  649. * Creates the InputHandler and adds it to the project.
  650. *
  651. * @param project the project instance.
  652. *
  653. * @exception BuildException if a specified InputHandler
  654. * implementation could not be loaded.
  655. */
  656. private void addInputHandler(Project project) throws BuildException {
  657. InputHandler handler = null;
  658. if (inputHandlerClassname == null) {
  659. handler = new DefaultInputHandler();
  660. } else {
  661. try {
  662. handler = (InputHandler)
  663. (Class.forName(inputHandlerClassname).newInstance());
  664. if (project != null) {
  665. project.setProjectReference(handler);
  666. }
  667. } catch (ClassCastException e) {
  668. String msg = "The specified input handler class "
  669. + inputHandlerClassname
  670. + " does not implement the InputHandler interface";
  671. throw new BuildException(msg);
  672. } catch (Exception e) {
  673. String msg = "Unable to instantiate specified input handler "
  674. + "class " + inputHandlerClassname + " : "
  675. + e.getClass().getName();
  676. throw new BuildException(msg);
  677. }
  678. }
  679. project.setInputHandler(handler);
  680. }
  681. // XXX: (Jon Skeet) Any reason for writing a message and then using a bare
  682. // RuntimeException rather than just using a BuildException here? Is it
  683. // in case the message could end up being written to no loggers (as the
  684. // loggers could have failed to be created due to this failure)?
  685. /**
  686. * Creates the default build logger for sending build events to the ant
  687. * log.
  688. *
  689. * @return the logger instance for this build.
  690. */
  691. private BuildLogger createLogger() {
  692. BuildLogger logger = null;
  693. if (loggerClassname != null) {
  694. try {
  695. Class loggerClass = Class.forName(loggerClassname);
  696. logger = (BuildLogger) (loggerClass.newInstance());
  697. } catch (ClassCastException e) {
  698. System.err.println("The specified logger class "
  699. + loggerClassname
  700. + " does not implement the BuildLogger interface");
  701. throw new RuntimeException();
  702. } catch (Exception e) {
  703. System.err.println("Unable to instantiate specified logger "
  704. + "class " + loggerClassname + " : "
  705. + e.getClass().getName());
  706. throw new RuntimeException();
  707. }
  708. } else {
  709. logger = new DefaultLogger();
  710. }
  711. logger.setMessageOutputLevel(msgOutputLevel);
  712. logger.setOutputPrintStream(out);
  713. logger.setErrorPrintStream(err);
  714. logger.setEmacsMode(emacsMode);
  715. return logger;
  716. }
  717. /**
  718. * Prints the usage information for this class to <code>System.out</code>.
  719. */
  720. private static void printUsage() {
  721. String lSep = System.getProperty("line.separator");
  722. StringBuffer msg = new StringBuffer();
  723. msg.append("ant [options] [target [target2 [target3] ...]]" + lSep);
  724. msg.append("Options: " + lSep);
  725. msg.append(" -help, -h print this message" + lSep);
  726. msg.append(" -projecthelp, -p print project help information" + lSep);
  727. msg.append(" -version print the version information and exit" + lSep);
  728. msg.append(" -diagnostics print information that might be helpful to" + lSep);
  729. msg.append(" diagnose or report problems." + lSep);
  730. msg.append(" -quiet, -q be extra quiet" + lSep);
  731. msg.append(" -verbose, -v be extra verbose" + lSep);
  732. msg.append(" -debug, -d print debugging information" + lSep);
  733. msg.append(" -emacs, -e produce logging information without adornments" + lSep);
  734. msg.append(" -lib <path> specifies a path to search for jars and classes" + lSep);
  735. msg.append(" -logfile <file> use given file for log" + lSep);
  736. msg.append(" -l <file> ''" + lSep);
  737. msg.append(" -logger <classname> the class which is to perform logging" + lSep);
  738. msg.append(" -listener <classname> add an instance of class as a project listener" + lSep);
  739. msg.append(" -noinput do not allow interactive input" + lSep);
  740. msg.append(" -buildfile <file> use given buildfile" + lSep);
  741. msg.append(" -file <file> ''" + lSep);
  742. msg.append(" -f <file> ''" + lSep);
  743. msg.append(" -D<property>=<value> use value for given property" + lSep);
  744. msg.append(" -keep-going, -k execute all targets that do not depend" + lSep);
  745. msg.append(" on failed target(s)" + lSep);
  746. msg.append(" -propertyfile <name> load all properties from file with -D" + lSep);
  747. msg.append(" properties taking precedence" + lSep);
  748. msg.append(" -inputhandler <class> the class which will handle input requests" + lSep);
  749. msg.append(" -find <file> (s)earch for buildfile towards the root of" + lSep);
  750. msg.append(" -s <file> the filesystem and use it" + lSep);
  751. msg.append(" -nice number A niceness value for the main thread:" + lSep +
  752. " 1 (lowest) to 10 (highest); 5 is the default" + lSep);
  753. System.out.println(msg.toString());
  754. }
  755. /**
  756. * Prints the Ant version information to <code>System.out</code>.
  757. *
  758. * @exception BuildException if the version information is unavailable
  759. */
  760. private static void printVersion() throws BuildException {
  761. System.out.println(getAntVersion());
  762. }
  763. /**
  764. * Cache of the Ant version information when it has been loaded.
  765. */
  766. private static String antVersion = null;
  767. /**
  768. * Returns the Ant version information, if available. Once the information
  769. * has been loaded once, it's cached and returned from the cache on future
  770. * calls.
  771. *
  772. * @return the Ant version information as a String
  773. * (always non-<code>null</code>)
  774. *
  775. * @exception BuildException if the version information is unavailable
  776. */
  777. public static synchronized String getAntVersion() throws BuildException {
  778. if (antVersion == null) {
  779. try {
  780. Properties props = new Properties();
  781. InputStream in =
  782. Main.class.getResourceAsStream("/org/apache/tools/ant/version.txt");
  783. props.load(in);
  784. in.close();
  785. StringBuffer msg = new StringBuffer();
  786. msg.append("Apache Ant version ");
  787. msg.append(props.getProperty("VERSION"));
  788. msg.append(" compiled on ");
  789. msg.append(props.getProperty("DATE"));
  790. antVersion = msg.toString();
  791. } catch (IOException ioe) {
  792. throw new BuildException("Could not load the version information:"
  793. + ioe.getMessage());
  794. } catch (NullPointerException npe) {
  795. throw new BuildException("Could not load the version information.");
  796. }
  797. }
  798. return antVersion;
  799. }
  800. /**
  801. * Prints the description of a project (if there is one) to
  802. * <code>System.out</code>.
  803. *
  804. * @param project The project to display a description of.
  805. * Must not be <code>null</code>.
  806. */
  807. private static void printDescription(Project project) {
  808. if (project.getDescription() != null) {
  809. project.log(project.getDescription());
  810. }
  811. }
  812. /**
  813. * Prints a list of all targets in the specified project to
  814. * <code>System.out</code>, optionally including subtargets.
  815. *
  816. * @param project The project to display a description of.
  817. * Must not be <code>null</code>.
  818. * @param printSubTargets Whether or not subtarget names should also be
  819. * printed.
  820. */
  821. private static void printTargets(Project project, boolean printSubTargets) {
  822. // find the target with the longest name
  823. int maxLength = 0;
  824. Enumeration ptargets = project.getTargets().elements();
  825. String targetName;
  826. String targetDescription;
  827. Target currentTarget;
  828. // split the targets in top-level and sub-targets depending
  829. // on the presence of a description
  830. Vector topNames = new Vector();
  831. Vector topDescriptions = new Vector();
  832. Vector subNames = new Vector();
  833. while (ptargets.hasMoreElements()) {
  834. currentTarget = (Target) ptargets.nextElement();
  835. targetName = currentTarget.getName();
  836. if (targetName.equals("")) {
  837. continue;
  838. }
  839. targetDescription = currentTarget.getDescription();
  840. // maintain a sorted list of targets
  841. if (targetDescription == null) {
  842. int pos = findTargetPosition(subNames, targetName);
  843. subNames.insertElementAt(targetName, pos);
  844. } else {
  845. int pos = findTargetPosition(topNames, targetName);
  846. topNames.insertElementAt(targetName, pos);
  847. topDescriptions.insertElementAt(targetDescription, pos);
  848. if (targetName.length() > maxLength) {
  849. maxLength = targetName.length();
  850. }
  851. }
  852. }
  853. printTargets(project, topNames, topDescriptions, "Main targets:",
  854. maxLength);
  855. //if there were no main targets, we list all subtargets
  856. //as it means nothing has a description
  857. if (topNames.size() == 0) {
  858. printSubTargets = true;
  859. }
  860. if (printSubTargets) {
  861. printTargets(project, subNames, null, "Other targets:", 0);
  862. }
  863. String defaultTarget = project.getDefaultTarget();
  864. if (defaultTarget != null && !"".equals(defaultTarget)) {
  865. // shouldn't need to check but...
  866. project.log("Default target: " + defaultTarget);
  867. }
  868. }
  869. /**
  870. * Searches for the correct place to insert a name into a list so as
  871. * to keep the list sorted alphabetically.
  872. *
  873. * @param names The current list of names. Must not be <code>null</code>.
  874. * @param name The name to find a place for.
  875. * Must not be <code>null</code>.
  876. *
  877. * @return the correct place in the list for the given name
  878. */
  879. private static int findTargetPosition(Vector names, String name) {
  880. int res = names.size();
  881. for (int i = 0; i < names.size() && res == names.size(); i++) {
  882. if (name.compareTo((String) names.elementAt(i)) < 0) {
  883. res = i;
  884. }
  885. }
  886. return res;
  887. }
  888. /**
  889. * Writes a formatted list of target names to <code>System.out</code>
  890. * with an optional description.
  891. *
  892. *
  893. * @param project the project instance.
  894. * @param names The names to be printed.
  895. * Must not be <code>null</code>.
  896. * @param descriptions The associated target descriptions.
  897. * May be <code>null</code>, in which case
  898. * no descriptions are displayed.
  899. * If non-<code>null</code>, this should have
  900. * as many elements as <code>names</code>.
  901. * @param heading The heading to display.
  902. * Should not be <code>null</code>.
  903. * @param maxlen The maximum length of the names of the targets.
  904. * If descriptions are given, they are padded to this
  905. * position so they line up (so long as the names really
  906. * <i>are</i> shorter than this).
  907. */
  908. private static void printTargets(Project project, Vector names,
  909. Vector descriptions, String heading,
  910. int maxlen) {
  911. // now, start printing the targets and their descriptions
  912. String lSep = System.getProperty("line.separator");
  913. // got a bit annoyed that I couldn't find a pad function
  914. String spaces = " ";
  915. while (spaces.length() <= maxlen) {
  916. spaces += spaces;
  917. }
  918. StringBuffer msg = new StringBuffer();
  919. msg.append(heading + lSep + lSep);
  920. for (int i = 0; i < names.size(); i++) {
  921. msg.append(" ");
  922. msg.append(names.elementAt(i));
  923. if (descriptions != null) {
  924. msg.append(spaces.substring(0, maxlen - ((String) names.elementAt(i)).length() + 2));
  925. msg.append(descriptions.elementAt(i));
  926. }
  927. msg.append(lSep);
  928. }
  929. project.log(msg.toString());
  930. }
  931. }