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.

Project.java 70 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  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.InputStream;
  57. import java.io.IOException;
  58. import java.util.Hashtable;
  59. import java.util.Vector;
  60. import java.util.Properties;
  61. import java.util.Enumeration;
  62. import java.util.Stack;
  63. import java.lang.reflect.Modifier;
  64. import org.apache.tools.ant.types.FilterSet;
  65. import org.apache.tools.ant.types.FilterSetCollection;
  66. import org.apache.tools.ant.util.FileUtils;
  67. import org.apache.tools.ant.util.JavaEnvUtils;
  68. /**
  69. * Central representation of an Ant project. This class defines an
  70. * Ant project with all of its targets, tasks and various other
  71. * properties. It also provides the mechanism to kick off a build using
  72. * a particular target name.
  73. * <p>
  74. * This class also encapsulates methods which allow files to be referred
  75. * to using abstract path names which are translated to native system
  76. * file paths at runtime.
  77. *
  78. * @author duncan@x180.com
  79. *
  80. * @version $Revision$
  81. */
  82. public class Project {
  83. /** Message priority of "error". */
  84. public static final int MSG_ERR = 0;
  85. /** Message priority of "warning". */
  86. public static final int MSG_WARN = 1;
  87. /** Message priority of "information". */
  88. public static final int MSG_INFO = 2;
  89. /** Message priority of "verbose". */
  90. public static final int MSG_VERBOSE = 3;
  91. /** Message priority of "debug". */
  92. public static final int MSG_DEBUG = 4;
  93. /**
  94. * Constant for the "visiting" state, used when
  95. * traversing a DFS of target dependencies.
  96. */
  97. private static final String VISITING = "VISITING";
  98. /**
  99. * Constant for the "visited" state, used when
  100. * traversing a DFS of target dependencies.
  101. */
  102. private static final String VISITED = "VISITED";
  103. /** Version constant for Java 1.0 */
  104. public static final String JAVA_1_0 = JavaEnvUtils.JAVA_1_0;
  105. /** Version constant for Java 1.1 */
  106. public static final String JAVA_1_1 = JavaEnvUtils.JAVA_1_1;
  107. /** Version constant for Java 1.2 */
  108. public static final String JAVA_1_2 = JavaEnvUtils.JAVA_1_2;
  109. /** Version constant for Java 1.3 */
  110. public static final String JAVA_1_3 = JavaEnvUtils.JAVA_1_3;
  111. /** Version constant for Java 1.4 */
  112. public static final String JAVA_1_4 = JavaEnvUtils.JAVA_1_4;
  113. /** Default filter start token. */
  114. public static final String TOKEN_START = FilterSet.DEFAULT_TOKEN_START;
  115. /** Default filter end token. */
  116. public static final String TOKEN_END = FilterSet.DEFAULT_TOKEN_END;
  117. /** Name of this project. */
  118. private String name;
  119. /** Description for this project (if any). */
  120. private String description;
  121. /** Project properties map (String to String). */
  122. private Hashtable properties = new Hashtable();
  123. /**
  124. * Map of "user" properties (as created in the Ant task, for example).
  125. * Note that these key/value pairs are also always put into the
  126. * project properties, so only the project properties need to be queried.
  127. * Mapping is String to String.
  128. */
  129. private Hashtable userProperties = new Hashtable();
  130. /** Map of references within the project (paths etc) (String to Object). */
  131. private Hashtable references = new Hashtable();
  132. /** Name of the project's default target. */
  133. private String defaultTarget;
  134. /** Map from data type names to implementing classes (String to Class). */
  135. private Hashtable dataClassDefinitions = new Hashtable();
  136. /** Map from task names to implementing classes (String to Class). */
  137. private Hashtable taskClassDefinitions = new Hashtable();
  138. /**
  139. * Map from task names to vectors of created tasks
  140. * (String to Vector of Task). This is used to invalidate tasks if
  141. * the task definition changes.
  142. */
  143. private Hashtable createdTasks = new Hashtable();
  144. /** Map from target names to targets (String to Target). */
  145. private Hashtable targets = new Hashtable();
  146. /** Set of global filters. */
  147. private FilterSet globalFilterSet = new FilterSet();
  148. /**
  149. * Wrapper around globalFilterSet. This collection only ever
  150. * contains one FilterSet, but the wrapper is needed in order to
  151. * make it easier to use the FileUtils interface.
  152. */
  153. private FilterSetCollection globalFilters
  154. = new FilterSetCollection(globalFilterSet);
  155. /** Project base directory. */
  156. private File baseDir;
  157. /** List of listeners to notify of build events. */
  158. private Vector listeners = new Vector();
  159. /**
  160. * The Ant core classloader - may be <code>null</code> if using
  161. * parent classloader.
  162. */
  163. private ClassLoader coreLoader = null;
  164. /** Records the latest task to be executed on a thread (Thread to Task). */
  165. private Hashtable threadTasks = new Hashtable();
  166. /** Instance of a utility class to use for file operations. */
  167. private FileUtils fileUtils;
  168. /**
  169. * Creates a new Ant project.
  170. */
  171. public Project() {
  172. fileUtils = FileUtils.newFileUtils();
  173. }
  174. /**
  175. * Initialises the project.
  176. *
  177. * This involves setting the default task definitions and loading the
  178. * system properties.
  179. *
  180. * @exception BuildException if the default task list cannot be loaded
  181. */
  182. public void init() throws BuildException {
  183. setJavaVersionProperty();
  184. String defs = "/org/apache/tools/ant/taskdefs/defaults.properties";
  185. try {
  186. Properties props = new Properties();
  187. InputStream in = this.getClass().getResourceAsStream(defs);
  188. if (in == null) {
  189. throw new BuildException("Can't load default task list");
  190. }
  191. props.load(in);
  192. in.close();
  193. Enumeration enum = props.propertyNames();
  194. while (enum.hasMoreElements()) {
  195. String key = (String) enum.nextElement();
  196. String value = props.getProperty(key);
  197. try {
  198. Class taskClass = Class.forName(value);
  199. addTaskDefinition(key, taskClass);
  200. } catch (NoClassDefFoundError ncdfe) {
  201. log("Could not load a dependent class ("
  202. + ncdfe.getMessage() + ") for task " + key, MSG_DEBUG);
  203. } catch (ClassNotFoundException cnfe) {
  204. log("Could not load class (" + value
  205. + ") for task " + key, MSG_DEBUG);
  206. }
  207. }
  208. } catch (IOException ioe) {
  209. throw new BuildException("Can't load default task list");
  210. }
  211. String dataDefs = "/org/apache/tools/ant/types/defaults.properties";
  212. try {
  213. Properties props = new Properties();
  214. InputStream in = this.getClass().getResourceAsStream(dataDefs);
  215. if (in == null) {
  216. throw new BuildException("Can't load default datatype list");
  217. }
  218. props.load(in);
  219. in.close();
  220. Enumeration enum = props.propertyNames();
  221. while (enum.hasMoreElements()) {
  222. String key = (String) enum.nextElement();
  223. String value = props.getProperty(key);
  224. try {
  225. Class dataClass = Class.forName(value);
  226. addDataTypeDefinition(key, dataClass);
  227. } catch (NoClassDefFoundError ncdfe) {
  228. // ignore...
  229. } catch (ClassNotFoundException cnfe) {
  230. // ignore...
  231. }
  232. }
  233. } catch (IOException ioe) {
  234. throw new BuildException("Can't load default datatype list");
  235. }
  236. setSystemProperties();
  237. }
  238. /**
  239. * Sets the core classloader for the project. If a <code>null</code>
  240. * classloader is specified, the parent classloader should be used.
  241. *
  242. * @param coreLoader The classloader to use for the project.
  243. * May be <code>null</code>.
  244. */
  245. public void setCoreLoader(ClassLoader coreLoader) {
  246. this.coreLoader = coreLoader;
  247. }
  248. /**
  249. * Returns the core classloader to use for this project.
  250. * This may be <code>null</code>, indicating that
  251. * the parent classloader should be used.
  252. *
  253. * @return the core classloader to use for this project.
  254. *
  255. */
  256. public ClassLoader getCoreLoader() {
  257. return coreLoader;
  258. }
  259. /**
  260. * Adds a build listener to the list. This listener will
  261. * be notified of build events for this project.
  262. *
  263. * @param listener The listener to add to the list.
  264. * Must not be <code>null</code>.
  265. */
  266. public void addBuildListener(BuildListener listener) {
  267. listeners.addElement(listener);
  268. }
  269. /**
  270. * Removes a build listener from the list. This listener
  271. * will no longer be notified of build events for this project.
  272. *
  273. * @param listener The listener to remove from the list.
  274. * Should not be <code>null</code>.
  275. */
  276. public void removeBuildListener(BuildListener listener) {
  277. listeners.removeElement(listener);
  278. }
  279. /**
  280. * Returns a list of build listeners for the project. The returned
  281. * vector is "live" and so should not be modified.
  282. *
  283. * @return a list of build listeners for the project
  284. */
  285. public Vector getBuildListeners() {
  286. return listeners;
  287. }
  288. /**
  289. * Writes a message to the log with the default log level
  290. * of MSG_INFO
  291. * @param msg The text to log. Should not be <code>null</code>.
  292. */
  293. public void log(String msg) {
  294. log(msg, MSG_INFO);
  295. }
  296. /**
  297. * Writes a project level message to the log with the given log level.
  298. * @param msg The text to log. Should not be <code>null</code>.
  299. * @param msgLevel The priority level to log at.
  300. */
  301. public void log(String msg, int msgLevel) {
  302. fireMessageLogged(this, msg, msgLevel);
  303. }
  304. /**
  305. * Writes a task level message to the log with the given log level.
  306. * @param task The task to use in the log. Must not be <code>null</code>.
  307. * @param msg The text to log. Should not be <code>null</code>.
  308. * @param msgLevel The priority level to log at.
  309. */
  310. public void log(Task task, String msg, int msgLevel) {
  311. fireMessageLogged(task, msg, msgLevel);
  312. }
  313. /**
  314. * Writes a target level message to the log with the given log level.
  315. * @param target The target to use in the log.
  316. * Must not be <code>null</code>.
  317. * @param msg The text to log. Should not be <code>null</code>.
  318. * @param msgLevel The priority level to log at.
  319. */
  320. public void log(Target target, String msg, int msgLevel) {
  321. fireMessageLogged(target, msg, msgLevel);
  322. }
  323. /**
  324. * Returns the set of global filters.
  325. *
  326. * @return the set of global filters
  327. */
  328. public FilterSet getGlobalFilterSet() {
  329. return globalFilterSet;
  330. }
  331. /**
  332. * Sets a property. Any existing property of the same name
  333. * is overwritten, unless it is a user property.
  334. * @param name The name of property to set.
  335. * Must not be <code>null</code>.
  336. * @param value The new value of the property.
  337. * Must not be <code>null</code>.
  338. */
  339. public void setProperty(String name, String value) {
  340. // command line properties take precedence
  341. if (null != userProperties.get(name)) {
  342. log("Override ignored for user property " + name, MSG_VERBOSE);
  343. return;
  344. }
  345. if (null != properties.get(name)) {
  346. log("Overriding previous definition of property " + name,
  347. MSG_VERBOSE);
  348. }
  349. log("Setting project property: " + name + " -> " +
  350. value, MSG_DEBUG);
  351. properties.put(name, value);
  352. }
  353. /**
  354. * Sets a property if no value currently exists. If the property
  355. * exists already, a message is logged and the method returns with
  356. * no other effect.
  357. *
  358. * @param name The name of property to set.
  359. * Must not be <code>null</code>.
  360. * @param value The new value of the property.
  361. * Must not be <code>null</code>.
  362. * @since 1.5
  363. */
  364. public void setNewProperty(String name, String value) {
  365. if (null != properties.get(name)) {
  366. log("Override ignored for property " + name, MSG_VERBOSE);
  367. return;
  368. }
  369. log("Setting project property: " + name + " -> " +
  370. value, MSG_DEBUG);
  371. properties.put(name, value);
  372. }
  373. /**
  374. * Sets a user property, which cannot be overwritten by
  375. * set/unset property calls. Any previous value is overwritten.
  376. * @param name The name of property to set.
  377. * Must not be <code>null</code>.
  378. * @param value The new value of the property.
  379. * Must not be <code>null</code>.
  380. * @see #setProperty(String,String)
  381. */
  382. public void setUserProperty(String name, String value) {
  383. log("Setting ro project property: " + name + " -> " +
  384. value, MSG_DEBUG);
  385. userProperties.put(name, value);
  386. properties.put(name, value);
  387. }
  388. /**
  389. * Sets a property unless it is already defined as a user property
  390. * (in which case the method returns silently).
  391. *
  392. * @param name The name of the property.
  393. * Must not be <code>null</code>.
  394. * @param value The property value. Must not be <code>null</code>.
  395. */
  396. private void setPropertyInternal(String name, String value) {
  397. if (null != userProperties.get(name)) {
  398. return;
  399. }
  400. properties.put(name, value);
  401. }
  402. /**
  403. * Returns the value of a property, if it is set.
  404. *
  405. * @param name The name of the property.
  406. * May be <code>null</code>, in which case
  407. * the return value is also <code>null</code>.
  408. * @return the property value, or <code>null</code> for no match
  409. * or if a <code>null</code> name is provided.
  410. */
  411. public String getProperty(String name) {
  412. if (name == null) {
  413. return null;
  414. }
  415. String property = (String) properties.get(name);
  416. return property;
  417. }
  418. /**
  419. * Replaces ${} style constructions in the given value with the
  420. * string value of the corresponding data types.
  421. *
  422. * @param value The string to be scanned for property references.
  423. * May be <code>null</code>.
  424. *
  425. * @return the given string with embedded property names replaced
  426. * by values, or <code>null</code> if the given string is
  427. * <code>null</code>.
  428. *
  429. * @exception BuildException if the given value has an unclosed
  430. * property name, e.g. <code>${xxx</code>
  431. */
  432. public String replaceProperties(String value)
  433. throws BuildException {
  434. return ProjectHelper.replaceProperties(this, value, properties);
  435. }
  436. /**
  437. * Returns the value of a user property, if it is set.
  438. *
  439. * @param name The name of the property.
  440. * May be <code>null</code>, in which case
  441. * the return value is also <code>null</code>.
  442. * @return the property value, or <code>null</code> for no match
  443. * or if a <code>null</code> name is provided.
  444. */
  445. public String getUserProperty(String name) {
  446. if (name == null) {
  447. return null;
  448. }
  449. String property = (String) userProperties.get(name);
  450. return property;
  451. }
  452. /**
  453. * Returns a copy of the properties table.
  454. * @return a hashtable containing all properties
  455. * (including user properties).
  456. */
  457. public Hashtable getProperties() {
  458. Hashtable propertiesCopy = new Hashtable();
  459. Enumeration e = properties.keys();
  460. while (e.hasMoreElements()) {
  461. Object name = e.nextElement();
  462. Object value = properties.get(name);
  463. propertiesCopy.put(name, value);
  464. }
  465. return propertiesCopy;
  466. }
  467. /**
  468. * Returns a copy of the user property hashtable
  469. * @return a hashtable containing just the user properties
  470. */
  471. public Hashtable getUserProperties() {
  472. Hashtable propertiesCopy = new Hashtable();
  473. Enumeration e = userProperties.keys();
  474. while (e.hasMoreElements()) {
  475. Object name = e.nextElement();
  476. Object value = properties.get(name);
  477. propertiesCopy.put(name, value);
  478. }
  479. return propertiesCopy;
  480. }
  481. /**
  482. * Sets the default target of the project.
  483. *
  484. * @param defaultTarget The name of the default target for this project.
  485. * May be <code>null</code>, indicating that there is
  486. * no default target.
  487. *
  488. * @deprecated use setDefault
  489. * @see #setDefault(String)
  490. */
  491. public void setDefaultTarget(String defaultTarget) {
  492. this.defaultTarget = defaultTarget;
  493. }
  494. /**
  495. * Returns the name of the default target of the project.
  496. * @return name of the default target or
  497. * <code>null</code> if no default has been set.
  498. */
  499. public String getDefaultTarget() {
  500. return defaultTarget;
  501. }
  502. /**
  503. * Sets the default target of the project.
  504. *
  505. * @param defaultTarget The name of the default target for this project.
  506. * May be <code>null</code>, indicating that there is
  507. * no default target.
  508. */
  509. public void setDefault(String defaultTarget) {
  510. this.defaultTarget = defaultTarget;
  511. }
  512. /**
  513. * Sets the name of the project, also setting the user
  514. * property <code>ant.project.name</code>.
  515. *
  516. * @param name The name of the project.
  517. * Must not be <code>null</code>.
  518. */
  519. public void setName(String name) {
  520. setUserProperty("ant.project.name", name);
  521. this.name = name;
  522. }
  523. /**
  524. * Returns the project name, if one has been set.
  525. *
  526. * @return the project name, or <code>null</code> if it hasn't been set.
  527. */
  528. public String getName() {
  529. return name;
  530. }
  531. /**
  532. * Sets the project description.
  533. *
  534. * @param description The description of the project.
  535. * May be <code>null</code>.
  536. */
  537. public void setDescription(String description) {
  538. this.description = description;
  539. }
  540. /**
  541. * Returns the project description, if one has been set.
  542. *
  543. * @return the project description, or <code>null</code> if it hasn't
  544. * been set.
  545. */
  546. public String getDescription() {
  547. return description;
  548. }
  549. /**
  550. * Adds a filter to the set of global filters.
  551. *
  552. * @param token The token to filter.
  553. * Must not be <code>null</code>.
  554. * @param value The replacement value.
  555. * Must not be <code>null</code>.
  556. * @deprecated Use getGlobalFilterSet().addFilter(token,value)
  557. *
  558. * @see #getGlobalFilterSet()
  559. * @see FilterSet#addFilter(String,String)
  560. */
  561. public void addFilter(String token, String value) {
  562. if (token == null) {
  563. return;
  564. }
  565. globalFilterSet.addFilter(new FilterSet.Filter(token, value));
  566. }
  567. /**
  568. * Returns a hashtable of global filters, mapping tokens to values.
  569. *
  570. * @return a hashtable of global filters, mapping tokens to values
  571. * (String to String).
  572. *
  573. * @deprecated Use getGlobalFilterSet().getFilterHash()
  574. *
  575. * @see #getGlobalFilterSet()
  576. * @see FilterSet#getFilterHash()
  577. */
  578. public Hashtable getFilters() {
  579. // we need to build the hashtable dynamically
  580. return globalFilterSet.getFilterHash();
  581. }
  582. /**
  583. * Sets the base directory for the project, checking that
  584. * the given filename exists and is a directory.
  585. *
  586. * @param baseD The project base directory.
  587. * Must not be <code>null</code>.
  588. *
  589. * @exception BuildException if the directory if invalid
  590. */
  591. public void setBasedir(String baseD) throws BuildException {
  592. setBaseDir(new File(baseD));
  593. }
  594. /**
  595. * Sets the base directory for the project, checking that
  596. * the given file exists and is a directory.
  597. *
  598. * @param baseDir The project base directory.
  599. * Must not be <code>null</code>.
  600. * @exception BuildException if the specified file doesn't exist or
  601. * isn't a directory
  602. */
  603. public void setBaseDir(File baseDir) throws BuildException {
  604. baseDir = fileUtils.normalize(baseDir.getAbsolutePath());
  605. if (!baseDir.exists()) {
  606. throw new BuildException("Basedir " + baseDir.getAbsolutePath()
  607. + " does not exist");
  608. }
  609. if (!baseDir.isDirectory()) {
  610. throw new BuildException("Basedir " + baseDir.getAbsolutePath()
  611. + " is not a directory");
  612. }
  613. this.baseDir = baseDir;
  614. setPropertyInternal("basedir", this.baseDir.getPath());
  615. String msg = "Project base dir set to: " + this.baseDir;
  616. log(msg, MSG_VERBOSE);
  617. }
  618. /**
  619. * Returns the base directory of the project as a file object.
  620. *
  621. * @return the project base directory, or <code>null</code> if the
  622. * base directory has not been successfully set to a valid value.
  623. */
  624. public File getBaseDir() {
  625. if (baseDir == null) {
  626. try {
  627. setBasedir(".");
  628. } catch (BuildException ex) {
  629. ex.printStackTrace();
  630. }
  631. }
  632. return baseDir;
  633. }
  634. /**
  635. * Returns the version of Java this class is running under.
  636. * @return the version of Java as a String, e.g. "1.1"
  637. * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
  638. */
  639. public static String getJavaVersion() {
  640. return JavaEnvUtils.getJavaVersion();
  641. }
  642. /**
  643. * Sets the <code>ant.java.version</code> property and tests for
  644. * unsupported JVM versions. If the version is supported,
  645. * verbose log messages are generated to record the Java version
  646. * and operating system name.
  647. *
  648. * @exception BuildException if this Java version is not supported
  649. *
  650. * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
  651. */
  652. public void setJavaVersionProperty() throws BuildException {
  653. String javaVersion = JavaEnvUtils.getJavaVersion();
  654. setPropertyInternal("ant.java.version", javaVersion);
  655. // sanity check
  656. if (javaVersion == JavaEnvUtils.JAVA_1_0) {
  657. throw new BuildException("Ant cannot work on Java 1.0");
  658. }
  659. log("Detected Java version: " + javaVersion + " in: "
  660. + System.getProperty("java.home"), MSG_VERBOSE);
  661. log("Detected OS: " + System.getProperty("os.name"), MSG_VERBOSE);
  662. }
  663. /**
  664. * Adds all system properties which aren't already defined as
  665. * user properties to the project properties.
  666. */
  667. public void setSystemProperties() {
  668. Properties systemP = System.getProperties();
  669. Enumeration e = systemP.keys();
  670. while (e.hasMoreElements()) {
  671. Object name = e.nextElement();
  672. String value = systemP.get(name).toString();
  673. this.setPropertyInternal(name.toString(), value);
  674. }
  675. }
  676. /**
  677. * Adds a new task definition to the project.
  678. * Attempting to override an existing definition with an
  679. * equivalent one (i.e. with the same classname) results in
  680. * a verbose log message. Attempting to override an existing definition
  681. * with a different one results in a warning log message and
  682. * invalidates any tasks which have already been created with the
  683. * old definition.
  684. *
  685. * @param taskName The name of the task to add.
  686. * Must not be <code>null</code>.
  687. * @param taskClass The full name of the class implementing the task.
  688. * Must not be <code>null</code>.
  689. *
  690. * @exception BuildException if the class is unsuitable for being an Ant
  691. * task. An error level message is logged before
  692. * this exception is thrown.
  693. *
  694. * @see #checkTaskClass(Class)
  695. */
  696. public void addTaskDefinition(String taskName, Class taskClass)
  697. throws BuildException {
  698. Class old = (Class) taskClassDefinitions.get(taskName);
  699. if (null != old) {
  700. if (old.equals(taskClass)) {
  701. log("Ignoring override for task " + taskName
  702. + ", it is already defined by the same class.",
  703. MSG_VERBOSE);
  704. return;
  705. } else {
  706. log("Trying to override old definition of task " + taskName,
  707. MSG_WARN);
  708. invalidateCreatedTasks(taskName);
  709. }
  710. }
  711. String msg = " +User task: " + taskName + " " + taskClass.getName();
  712. log(msg, MSG_DEBUG);
  713. checkTaskClass(taskClass);
  714. taskClassDefinitions.put(taskName, taskClass);
  715. }
  716. /**
  717. * Checks whether or not a class is suitable for serving as Ant task.
  718. * Ant task implementation classes must be public, concrete, and have
  719. * a no-arg constructor.
  720. *
  721. * @param taskClass The class to be checked.
  722. * Must not be <code>null</code>.
  723. *
  724. * @exception BuildException if the class is unsuitable for being an Ant
  725. * task. An error level message is logged before
  726. * this exception is thrown.
  727. */
  728. public void checkTaskClass(final Class taskClass) throws BuildException {
  729. if (!Modifier.isPublic(taskClass.getModifiers())) {
  730. final String message = taskClass + " is not public";
  731. log(message, Project.MSG_ERR);
  732. throw new BuildException(message);
  733. }
  734. if (Modifier.isAbstract(taskClass.getModifiers())) {
  735. final String message = taskClass + " is abstract";
  736. log(message, Project.MSG_ERR);
  737. throw new BuildException(message);
  738. }
  739. try {
  740. taskClass.getConstructor(null);
  741. // don't have to check for public, since
  742. // getConstructor finds public constructors only.
  743. } catch (NoSuchMethodException e) {
  744. final String message = "No public no-arg constructor in "
  745. + taskClass;
  746. log(message, Project.MSG_ERR);
  747. throw new BuildException(message);
  748. }
  749. if (!Task.class.isAssignableFrom(taskClass)) {
  750. TaskAdapter.checkTaskClass(taskClass, this);
  751. }
  752. }
  753. /**
  754. * Returns the current task definition hashtable. The returned hashtable is
  755. * "live" and so should not be modified.
  756. *
  757. * @return a map of from task name to implementing class
  758. * (String to Class).
  759. */
  760. public Hashtable getTaskDefinitions() {
  761. return taskClassDefinitions;
  762. }
  763. /**
  764. * Adds a new datatype definition.
  765. * Attempting to override an existing definition with an
  766. * equivalent one (i.e. with the same classname) results in
  767. * a verbose log message. Attempting to override an existing definition
  768. * with a different one results in a warning log message, but the
  769. * definition is changed.
  770. *
  771. * @param typeName The name of the datatype.
  772. * Must not be <code>null</code>.
  773. * @param typeClass The full name of the class implementing the datatype.
  774. * Must not be <code>null</code>.
  775. */
  776. public void addDataTypeDefinition(String typeName, Class typeClass) {
  777. Class old = (Class) dataClassDefinitions.get(typeName);
  778. if (null != old) {
  779. if (old.equals(typeClass)) {
  780. log("Ignoring override for datatype " + typeName
  781. + ", it is already defined by the same class.",
  782. MSG_VERBOSE);
  783. return;
  784. } else {
  785. log("Trying to override old definition of datatype "
  786. + typeName, MSG_WARN);
  787. }
  788. }
  789. String msg = " +User datatype: " + typeName + " "
  790. + typeClass.getName();
  791. log(msg, MSG_DEBUG);
  792. dataClassDefinitions.put(typeName, typeClass);
  793. }
  794. /**
  795. * Returns the current datatype definition hashtable. The returned
  796. * hashtable is "live" and so should not be modified.
  797. *
  798. * @return a map of from datatype name to implementing class
  799. * (String to Class).
  800. */
  801. public Hashtable getDataTypeDefinitions() {
  802. return dataClassDefinitions;
  803. }
  804. /**
  805. * Adds a <em>new</em> target to the project.
  806. *
  807. * @param target The target to be added to the project.
  808. * Must not be <code>null</code>.
  809. *
  810. * @exception BuildException if the target already exists in the project
  811. *
  812. * @see Project#addOrReplaceTarget
  813. */
  814. public void addTarget(Target target) throws BuildException {
  815. String name = target.getName();
  816. if (targets.get(name) != null) {
  817. throw new BuildException("Duplicate target: `" + name + "'");
  818. }
  819. addOrReplaceTarget(name, target);
  820. }
  821. /**
  822. * Adds a <em>new</em> target to the project.
  823. *
  824. * @param targetName The name to use for the target.
  825. * Must not be <code>null</code>.
  826. * @param target The target to be added to the project.
  827. * Must not be <code>null</code>.
  828. *
  829. * @exception BuildException if the target already exists in the project
  830. *
  831. * @see Project#addOrReplaceTarget
  832. */
  833. public void addTarget(String targetName, Target target)
  834. throws BuildException {
  835. if (targets.get(targetName) != null) {
  836. throw new BuildException("Duplicate target: `" + targetName + "'");
  837. }
  838. addOrReplaceTarget(targetName, target);
  839. }
  840. /**
  841. * Adds a target to the project, or replaces one with the same
  842. * name.
  843. *
  844. * @param target The target to be added or replaced in the project.
  845. * Must not be <code>null</code>.
  846. */
  847. public void addOrReplaceTarget(Target target) {
  848. addOrReplaceTarget(target.getName(), target);
  849. }
  850. /**
  851. * Adds a target to the project, or replaces one with the same
  852. * name.
  853. *
  854. * @param targetName The name to use for the target.
  855. * Must not be <code>null</code>.
  856. * @param target The target to be added or replaced in the project.
  857. * Must not be <code>null</code>.
  858. */
  859. public void addOrReplaceTarget(String targetName, Target target) {
  860. String msg = " +Target: " + targetName;
  861. log(msg, MSG_DEBUG);
  862. target.setProject(this);
  863. targets.put(targetName, target);
  864. }
  865. /**
  866. * Returns the hashtable of targets. The returned hashtable
  867. * is "live" and so should not be modified.
  868. * @return a map from name to target (String to Target).
  869. */
  870. public Hashtable getTargets() {
  871. return targets;
  872. }
  873. /**
  874. * Creates a new instance of a task.
  875. *
  876. * @param taskType The name of the task to create an instance of.
  877. * Must not be <code>null</code>.
  878. *
  879. * @return an instance of the specified task, or <code>null</code> if
  880. * the task name is not recognised.
  881. *
  882. * @exception BuildException if the task name is recognised but task
  883. * creation fails.
  884. */
  885. public Task createTask(String taskType) throws BuildException {
  886. Class c = (Class) taskClassDefinitions.get(taskType);
  887. if (c == null) {
  888. return null;
  889. }
  890. try {
  891. Object o = c.newInstance();
  892. Task task = null;
  893. if (o instanceof Task) {
  894. task = (Task) o;
  895. } else {
  896. // "Generic" Bean - use the setter pattern
  897. // and an Adapter
  898. TaskAdapter taskA = new TaskAdapter();
  899. taskA.setProxy(o);
  900. task = taskA;
  901. }
  902. task.setProject(this);
  903. task.setTaskType(taskType);
  904. // set default value, can be changed by the user
  905. task.setTaskName(taskType);
  906. String msg = " +Task: " + taskType;
  907. log (msg, MSG_DEBUG);
  908. addCreatedTask(taskType, task);
  909. return task;
  910. } catch (Throwable t) {
  911. String msg = "Could not create task of type: "
  912. + taskType + " due to " + t;
  913. throw new BuildException(msg, t);
  914. }
  915. }
  916. /**
  917. * Keeps a record of all tasks that have been created so that they
  918. * can be invalidated if a new task definition overrides the current one.
  919. *
  920. * @param type The name of the type of task which has been created.
  921. * Must not be <code>null</code>.
  922. *
  923. * @param task The freshly created task instance.
  924. * Must not be <code>null</code>.
  925. */
  926. private void addCreatedTask(String type, Task task) {
  927. synchronized (createdTasks) {
  928. Vector v = (Vector) createdTasks.get(type);
  929. if (v == null) {
  930. v = new Vector();
  931. createdTasks.put(type, v);
  932. }
  933. v.addElement(task);
  934. }
  935. }
  936. /**
  937. * Mark tasks as invalid which no longer are of the correct type
  938. * for a given taskname.
  939. *
  940. * @param type The name of the type of task to invalidate.
  941. * Must not be <code>null</code>.
  942. */
  943. private void invalidateCreatedTasks(String type) {
  944. synchronized (createdTasks) {
  945. Vector v = (Vector) createdTasks.get(type);
  946. if (v != null) {
  947. Enumeration enum = v.elements();
  948. while (enum.hasMoreElements()) {
  949. Task t = (Task) enum.nextElement();
  950. t.markInvalid();
  951. }
  952. v.removeAllElements();
  953. createdTasks.remove(type);
  954. }
  955. }
  956. }
  957. /**
  958. * Creates a new instance of a data type.
  959. *
  960. * @param typeName The name of the data type to create an instance of.
  961. * Must not be <code>null</code>.
  962. *
  963. * @return an instance of the specified data type, or <code>null</code> if
  964. * the data type name is not recognised.
  965. *
  966. * @exception BuildException if the data type name is recognised but
  967. * instance creation fails.
  968. */
  969. public Object createDataType(String typeName) throws BuildException {
  970. Class c = (Class) dataClassDefinitions.get(typeName);
  971. if (c == null) {
  972. return null;
  973. }
  974. try {
  975. java.lang.reflect.Constructor ctor = null;
  976. boolean noArg = false;
  977. // DataType can have a "no arg" constructor or take a single
  978. // Project argument.
  979. try {
  980. ctor = c.getConstructor(new Class[0]);
  981. noArg = true;
  982. } catch (NoSuchMethodException nse) {
  983. ctor = c.getConstructor(new Class[] {Project.class});
  984. noArg = false;
  985. }
  986. Object o = null;
  987. if (noArg) {
  988. o = ctor.newInstance(new Object[0]);
  989. } else {
  990. o = ctor.newInstance(new Object[] {this});
  991. }
  992. if (o instanceof ProjectComponent) {
  993. ((ProjectComponent) o).setProject(this);
  994. }
  995. String msg = " +DataType: " + typeName;
  996. log (msg, MSG_DEBUG);
  997. return o;
  998. } catch (java.lang.reflect.InvocationTargetException ite) {
  999. Throwable t = ite.getTargetException();
  1000. String msg = "Could not create datatype of type: "
  1001. + typeName + " due to " + t;
  1002. throw new BuildException(msg, t);
  1003. } catch (Throwable t) {
  1004. String msg = "Could not create datatype of type: "
  1005. + typeName + " due to " + t;
  1006. throw new BuildException(msg, t);
  1007. }
  1008. }
  1009. /**
  1010. * Execute the specified sequence of targets, and the targets
  1011. * they depend on.
  1012. *
  1013. * @param targetNames A vector of target name strings to execute.
  1014. * Must not be <code>null</code>.
  1015. *
  1016. * @exception BuildException if the build failed
  1017. */
  1018. public void executeTargets(Vector targetNames) throws BuildException {
  1019. Throwable error = null;
  1020. for (int i = 0; i < targetNames.size(); i++) {
  1021. executeTarget((String) targetNames.elementAt(i));
  1022. }
  1023. }
  1024. /**
  1025. * Demultiplexes output so that each task receives the appropriate
  1026. * messages. If the current thread is not currently executing a task,
  1027. * the message is logged directly.
  1028. *
  1029. * @param line Message to handle. Should not be <code>null</code>.
  1030. * @param isError Whether the text represents an error (<code>true</code>)
  1031. * or information (<code>false</code>).
  1032. */
  1033. public void demuxOutput(String line, boolean isError) {
  1034. Task task = (Task) threadTasks.get(Thread.currentThread());
  1035. if (task == null) {
  1036. fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO);
  1037. } else {
  1038. if (isError) {
  1039. task.handleErrorOutput(line);
  1040. } else {
  1041. task.handleOutput(line);
  1042. }
  1043. }
  1044. }
  1045. /**
  1046. * Executes the specified target and any targets it depends on.
  1047. *
  1048. * @param targetName The name of the target to execute.
  1049. * Must not be <code>null</code>.
  1050. *
  1051. * @exception BuildException if the build failed
  1052. */
  1053. public void executeTarget(String targetName) throws BuildException {
  1054. // sanity check ourselves, if we've been asked to build nothing
  1055. // then we should complain
  1056. if (targetName == null) {
  1057. String msg = "No target specified";
  1058. throw new BuildException(msg);
  1059. }
  1060. // Sort the dependency tree, and run everything from the
  1061. // beginning until we hit our targetName.
  1062. // Sorting checks if all the targets (and dependencies)
  1063. // exist, and if there is any cycle in the dependency
  1064. // graph.
  1065. Vector sortedTargets = topoSort(targetName, targets);
  1066. int curidx = 0;
  1067. Target curtarget;
  1068. do {
  1069. curtarget = (Target) sortedTargets.elementAt(curidx++);
  1070. curtarget.performTasks();
  1071. } while (!curtarget.getName().equals(targetName));
  1072. }
  1073. /**
  1074. * Returns the canonical form of a filename.
  1075. * <p>
  1076. * If the specified file name is relative it is resolved
  1077. * with respect to the given root directory.
  1078. *
  1079. * @param fileName The name of the file to resolve.
  1080. * Must not be <code>null</code>.
  1081. *
  1082. * @param rootDir The directory to resolve relative file names with
  1083. * respect to. May be <code>null</code>, in which case
  1084. * the current directory is used.
  1085. *
  1086. * @return the resolved File.
  1087. *
  1088. * @deprecated
  1089. */
  1090. public File resolveFile(String fileName, File rootDir) {
  1091. return fileUtils.resolveFile(rootDir, fileName);
  1092. }
  1093. /**
  1094. * Returns the canonical form of a filename.
  1095. * <p>
  1096. * If the specified file name is relative it is resolved
  1097. * with respect to the project's base directory.
  1098. *
  1099. * @param fileName The name of the file to resolve.
  1100. * Must not be <code>null</code>.
  1101. *
  1102. * @return the resolved File.
  1103. *
  1104. */
  1105. public File resolveFile(String fileName) {
  1106. return fileUtils.resolveFile(baseDir, fileName);
  1107. }
  1108. /**
  1109. * Translates a path into its native (platform specific) format.
  1110. * <p>
  1111. * This method uses PathTokenizer to separate the input path
  1112. * into its components. This handles DOS style paths in a relatively
  1113. * sensible way. The file separators are then converted to their platform
  1114. * specific versions.
  1115. *
  1116. * @param toProcess The path to be translated.
  1117. * May be <code>null</code>.
  1118. *
  1119. * @return the native version of the specified path or
  1120. * an empty string if the path is <code>null</code> or empty.
  1121. *
  1122. * @see PathTokenizer
  1123. */
  1124. public static String translatePath(String toProcess) {
  1125. if (toProcess == null || toProcess.length() == 0) {
  1126. return "";
  1127. }
  1128. StringBuffer path = new StringBuffer(toProcess.length() + 50);
  1129. PathTokenizer tokenizer = new PathTokenizer(toProcess);
  1130. while (tokenizer.hasMoreTokens()) {
  1131. String pathComponent = tokenizer.nextToken();
  1132. pathComponent = pathComponent.replace('/', File.separatorChar);
  1133. pathComponent = pathComponent.replace('\\', File.separatorChar);
  1134. if (path.length() != 0) {
  1135. path.append(File.pathSeparatorChar);
  1136. }
  1137. path.append(pathComponent);
  1138. }
  1139. return path.toString();
  1140. }
  1141. /**
  1142. * Convenience method to copy a file from a source to a destination.
  1143. * No filtering is performed.
  1144. *
  1145. * @param sourceFile Name of file to copy from.
  1146. * Must not be <code>null</code>.
  1147. * @param destFile Name of file to copy to.
  1148. * Must not be <code>null</code>.
  1149. *
  1150. * @exception IOException if the copying fails
  1151. *
  1152. * @deprecated
  1153. */
  1154. public void copyFile(String sourceFile, String destFile)
  1155. throws IOException {
  1156. fileUtils.copyFile(sourceFile, destFile);
  1157. }
  1158. /**
  1159. * Convenience method to copy a file from a source to a destination
  1160. * specifying if token filtering should be used.
  1161. *
  1162. * @param sourceFile Name of file to copy from.
  1163. * Must not be <code>null</code>.
  1164. * @param destFile Name of file to copy to.
  1165. * Must not be <code>null</code>.
  1166. * @param filtering Whether or not token filtering should be used during
  1167. * the copy.
  1168. *
  1169. * @exception IOException if the copying fails
  1170. *
  1171. * @deprecated
  1172. */
  1173. public void copyFile(String sourceFile, String destFile, boolean filtering)
  1174. throws IOException {
  1175. fileUtils.copyFile(sourceFile, destFile,
  1176. filtering ? globalFilters : null);
  1177. }
  1178. /**
  1179. * Convenience method to copy a file from a source to a
  1180. * destination specifying if token filtering should be used and if
  1181. * source files may overwrite newer destination files.
  1182. *
  1183. * @param sourceFile Name of file to copy from.
  1184. * Must not be <code>null</code>.
  1185. * @param destFile Name of file to copy to.
  1186. * Must not be <code>null</code>.
  1187. * @param filtering Whether or not token filtering should be used during
  1188. * the copy.
  1189. * @param overwrite Whether or not the destination file should be
  1190. * overwritten if it already exists.
  1191. *
  1192. * @exception IOException if the copying fails
  1193. *
  1194. * @deprecated
  1195. */
  1196. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1197. boolean overwrite) throws IOException {
  1198. fileUtils.copyFile(sourceFile, destFile,
  1199. filtering ? globalFilters : null, overwrite);
  1200. }
  1201. /**
  1202. * Convenience method to copy a file from a source to a
  1203. * destination specifying if token filtering should be used, if
  1204. * source files may overwrite newer destination files, and if the
  1205. * last modified time of the resulting file should be set to
  1206. * that of the source file.
  1207. *
  1208. * @param sourceFile Name of file to copy from.
  1209. * Must not be <code>null</code>.
  1210. * @param destFile Name of file to copy to.
  1211. * Must not be <code>null</code>.
  1212. * @param filtering Whether or not token filtering should be used during
  1213. * the copy.
  1214. * @param overwrite Whether or not the destination file should be
  1215. * overwritten if it already exists.
  1216. * @param preserveLastModified Whether or not the last modified time of
  1217. * the resulting file should be set to that
  1218. * of the source file.
  1219. *
  1220. * @exception IOException if the copying fails
  1221. *
  1222. * @deprecated
  1223. */
  1224. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1225. boolean overwrite, boolean preserveLastModified)
  1226. throws IOException {
  1227. fileUtils.copyFile(sourceFile, destFile,
  1228. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1229. }
  1230. /**
  1231. * Convenience method to copy a file from a source to a destination.
  1232. * No filtering is performed.
  1233. *
  1234. * @param sourceFile File to copy from.
  1235. * Must not be <code>null</code>.
  1236. * @param destFile File to copy to.
  1237. * Must not be <code>null</code>.
  1238. *
  1239. * @exception IOException if the copying fails
  1240. *
  1241. * @deprecated
  1242. */
  1243. public void copyFile(File sourceFile, File destFile) throws IOException {
  1244. fileUtils.copyFile(sourceFile, destFile);
  1245. }
  1246. /**
  1247. * Convenience method to copy a file from a source to a destination
  1248. * specifying if token filtering should be used.
  1249. *
  1250. * @param sourceFile File to copy from.
  1251. * Must not be <code>null</code>.
  1252. * @param destFile File to copy to.
  1253. * Must not be <code>null</code>.
  1254. * @param filtering Whether or not token filtering should be used during
  1255. * the copy.
  1256. *
  1257. * @exception IOException if the copying fails
  1258. *
  1259. * @deprecated
  1260. */
  1261. public void copyFile(File sourceFile, File destFile, boolean filtering)
  1262. throws IOException {
  1263. fileUtils.copyFile(sourceFile, destFile,
  1264. filtering ? globalFilters : null);
  1265. }
  1266. /**
  1267. * Convenience method to copy a file from a source to a
  1268. * destination specifying if token filtering should be used and if
  1269. * source files may overwrite newer destination files.
  1270. *
  1271. * @param sourceFile File to copy from.
  1272. * Must not be <code>null</code>.
  1273. * @param destFile File to copy to.
  1274. * Must not be <code>null</code>.
  1275. * @param filtering Whether or not token filtering should be used during
  1276. * the copy.
  1277. * @param overwrite Whether or not the destination file should be
  1278. * overwritten if it already exists.
  1279. *
  1280. * @exception IOException if the file cannot be copied.
  1281. *
  1282. * @deprecated
  1283. */
  1284. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1285. boolean overwrite) throws IOException {
  1286. fileUtils.copyFile(sourceFile, destFile,
  1287. filtering ? globalFilters : null, overwrite);
  1288. }
  1289. /**
  1290. * Convenience method to copy a file from a source to a
  1291. * destination specifying if token filtering should be used, if
  1292. * source files may overwrite newer destination files, and if the
  1293. * last modified time of the resulting file should be set to
  1294. * that of the source file.
  1295. *
  1296. * @param sourceFile File to copy from.
  1297. * Must not be <code>null</code>.
  1298. * @param destFile File to copy to.
  1299. * Must not be <code>null</code>.
  1300. * @param filtering Whether or not token filtering should be used during
  1301. * the copy.
  1302. * @param overwrite Whether or not the destination file should be
  1303. * overwritten if it already exists.
  1304. * @param preserveLastModified Whether or not the last modified time of
  1305. * the resulting file should be set to that
  1306. * of the source file.
  1307. *
  1308. * @exception IOException if the file cannot be copied.
  1309. *
  1310. * @deprecated
  1311. */
  1312. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1313. boolean overwrite, boolean preserveLastModified)
  1314. throws IOException {
  1315. fileUtils.copyFile(sourceFile, destFile,
  1316. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1317. }
  1318. /**
  1319. * Calls File.setLastModified(long time) on Java above 1.1, and logs
  1320. * a warning on Java 1.1.
  1321. *
  1322. * @param file The file to set the last modified time on.
  1323. * Must not be <code>null</code>.
  1324. *
  1325. * @param time the required modification time.
  1326. *
  1327. * @deprecated
  1328. *
  1329. * @exception BuildException if the last modified time cannot be set
  1330. * despite running on a platform with a version
  1331. * above 1.1.
  1332. */
  1333. public void setFileLastModified(File file, long time)
  1334. throws BuildException {
  1335. if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1) {
  1336. log("Cannot change the modification time of " + file
  1337. + " in JDK 1.1", Project.MSG_WARN);
  1338. return;
  1339. }
  1340. fileUtils.setFileLastModified(file, time);
  1341. log("Setting modification time for " + file, MSG_VERBOSE);
  1342. }
  1343. /**
  1344. * Returns the boolean equivalent of a string, which is considered
  1345. * <code>true</code> if either <code>"on"</code>, <code>"true"</code>,
  1346. * or <code>"yes"</code> is found, ignoring case.
  1347. *
  1348. * @param s The string to convert to a boolean value.
  1349. * Must not be <code>null</code>.
  1350. *
  1351. * @return <code>true</code> if the given string is <code>"on"</code>,
  1352. * <code>"true"</code> or <code>"yes"</code>, or
  1353. * <code>false</code> otherwise.
  1354. */
  1355. public static boolean toBoolean(String s) {
  1356. return (s.equalsIgnoreCase("on") ||
  1357. s.equalsIgnoreCase("true") ||
  1358. s.equalsIgnoreCase("yes"));
  1359. }
  1360. /**
  1361. * Topologically sorts a set of targets.
  1362. *
  1363. * @param root The name of the root target. The sort is created in such
  1364. * a way that the sequence of Targets up to the root
  1365. * target is the minimum possible such sequence.
  1366. * Must not be <code>null</code>.
  1367. * @param targets A map of names to targets (String to Target).
  1368. * Must not be <code>null</code>.
  1369. * @return a vector of strings with the names of the targets in
  1370. * sorted order.
  1371. * @exception BuildException if there is a cyclic dependency among the
  1372. * targets, or if a named target does not exist.
  1373. */
  1374. public final Vector topoSort(String root, Hashtable targets)
  1375. throws BuildException {
  1376. Vector ret = new Vector();
  1377. Hashtable state = new Hashtable();
  1378. Stack visiting = new Stack();
  1379. // We first run a DFS based sort using the root as the starting node.
  1380. // This creates the minimum sequence of Targets to the root node.
  1381. // We then do a sort on any remaining unVISITED targets.
  1382. // This is unnecessary for doing our build, but it catches
  1383. // circular dependencies or missing Targets on the entire
  1384. // dependency tree, not just on the Targets that depend on the
  1385. // build Target.
  1386. tsort(root, targets, state, visiting, ret);
  1387. log("Build sequence for target `" + root + "' is " + ret, MSG_VERBOSE);
  1388. for (Enumeration en = targets.keys(); en.hasMoreElements();) {
  1389. String curTarget = (String) en.nextElement();
  1390. String st = (String) state.get(curTarget);
  1391. if (st == null) {
  1392. tsort(curTarget, targets, state, visiting, ret);
  1393. } else if (st == VISITING) {
  1394. throw new RuntimeException("Unexpected node in visiting state: "
  1395. + curTarget);
  1396. }
  1397. }
  1398. log("Complete build sequence is " + ret, MSG_VERBOSE);
  1399. return ret;
  1400. }
  1401. /**
  1402. * Performs a single step in a recursive depth-first-search traversal of
  1403. * the target dependency tree.
  1404. * <p>
  1405. * The current target is first set to the "visiting" state, and pushed
  1406. * onto the "visiting" stack.
  1407. * <p>
  1408. * An exception is then thrown if any child of the current node is in the
  1409. * visiting state, as that implies a circular dependency. The exception
  1410. * contains details of the cycle, using elements of the "visiting" stack.
  1411. * <p>
  1412. * If any child has not already been "visited", this method is called
  1413. * recursively on it.
  1414. * <p>
  1415. * The current target is then added to the ordered list of targets. Note
  1416. * that this is performed after the children have been visited in order
  1417. * to get the correct order. The current target is set to the "visited"
  1418. * state.
  1419. * <p>
  1420. * By the time this method returns, the ordered list contains the sequence
  1421. * of targets up to and including the current target.
  1422. *
  1423. * @param root The current target to inspect.
  1424. * Must not be <code>null</code>.
  1425. * @param targets A mapping from names to targets (String to Target).
  1426. * Must not be <code>null</code>.
  1427. * @param state A mapping from target names to states
  1428. * (String to String).
  1429. * The states in question are "VISITING" and "VISITED".
  1430. * Must not be <code>null</code>.
  1431. * @param visiting A stack of targets which are currently being visited.
  1432. * Must not be <code>null</code>.
  1433. * @param ret The list to add target names to. This will end up
  1434. * containing the complete list of depenencies in
  1435. * dependency order.
  1436. * Must not be <code>null</code>.
  1437. *
  1438. * @exception BuildException if a non-existent target is specified or if
  1439. * a circular dependency is detected.
  1440. */
  1441. private final void tsort(String root, Hashtable targets,
  1442. Hashtable state, Stack visiting,
  1443. Vector ret)
  1444. throws BuildException {
  1445. state.put(root, VISITING);
  1446. visiting.push(root);
  1447. Target target = (Target) targets.get(root);
  1448. // Make sure we exist
  1449. if (target == null) {
  1450. StringBuffer sb = new StringBuffer("Target `");
  1451. sb.append(root);
  1452. sb.append("' does not exist in this project. ");
  1453. visiting.pop();
  1454. if (!visiting.empty()) {
  1455. String parent = (String) visiting.peek();
  1456. sb.append("It is used from target `");
  1457. sb.append(parent);
  1458. sb.append("'.");
  1459. }
  1460. throw new BuildException(new String(sb));
  1461. }
  1462. for (Enumeration en = target.getDependencies(); en.hasMoreElements();) {
  1463. String cur = (String) en.nextElement();
  1464. String m = (String) state.get(cur);
  1465. if (m == null) {
  1466. // Not been visited
  1467. tsort(cur, targets, state, visiting, ret);
  1468. } else if (m == VISITING) {
  1469. // Currently visiting this node, so have a cycle
  1470. throw makeCircularException(cur, visiting);
  1471. }
  1472. }
  1473. String p = (String) visiting.pop();
  1474. if (root != p) {
  1475. throw new RuntimeException("Unexpected internal error: expected to "
  1476. + "pop " + root + " but got " + p);
  1477. }
  1478. state.put(root, VISITED);
  1479. ret.addElement(target);
  1480. }
  1481. /**
  1482. * Builds an appropriate exception detailing a specified circular
  1483. * dependency.
  1484. *
  1485. * @param end The dependency to stop at. Must not be <code>null</code>.
  1486. * @param stk A stack of dependencies. Must not be <code>null</code>.
  1487. *
  1488. * @return a BuildException detailing the specified circular dependency.
  1489. */
  1490. private static BuildException makeCircularException(String end, Stack stk) {
  1491. StringBuffer sb = new StringBuffer("Circular dependency: ");
  1492. sb.append(end);
  1493. String c;
  1494. do {
  1495. c = (String) stk.pop();
  1496. sb.append(" <- ");
  1497. sb.append(c);
  1498. } while (!c.equals(end));
  1499. return new BuildException(new String(sb));
  1500. }
  1501. /**
  1502. * Adds a reference to the project.
  1503. *
  1504. * @param name The name of the reference. Must not be <code>null</code>.
  1505. * @param value The value of the reference. Must not be <code>null</code>.
  1506. */
  1507. public void addReference(String name, Object value) {
  1508. Object old = references.get(name);
  1509. if (old == value) {
  1510. // no warning, this is not changing anything
  1511. return;
  1512. }
  1513. if (old != null) {
  1514. log("Overriding previous definition of reference to " + name,
  1515. MSG_WARN);
  1516. }
  1517. log("Adding reference: " + name + " -> " + value, MSG_DEBUG);
  1518. references.put(name, value);
  1519. }
  1520. /**
  1521. * Returns a map of the references in the project (String to Object).
  1522. * The returned hashtable is "live" and so should not be modified.
  1523. *
  1524. * @return a map of the references in the project (String to Object).
  1525. */
  1526. public Hashtable getReferences() {
  1527. return references;
  1528. }
  1529. /**
  1530. * Looks up a reference by its key (ID).
  1531. *
  1532. * @param key The key for the desired reference.
  1533. * Must not be <code>null</code>.
  1534. *
  1535. * @return the reference with the specified ID, or <code>null</code> if
  1536. * there is no such reference in the project.
  1537. */
  1538. public Object getReference(String key) {
  1539. return references.get(key);
  1540. }
  1541. /**
  1542. * Returns a description of the type of the given element, with
  1543. * special handling for instances of tasks and data types.
  1544. * <p>
  1545. * This is useful for logging purposes.
  1546. *
  1547. * @param element The element to describe.
  1548. * Must not be <code>null</code>.
  1549. *
  1550. * @return a description of the element type
  1551. *
  1552. * @since 1.95, Ant 1.5
  1553. */
  1554. public String getElementName(Object element) {
  1555. Hashtable elements = taskClassDefinitions;
  1556. Class elementClass = element.getClass();
  1557. String typeName = "task";
  1558. if (!elements.contains(elementClass)) {
  1559. elements = dataClassDefinitions;
  1560. typeName = "data type";
  1561. if (!elements.contains(elementClass)) {
  1562. elements = null;
  1563. }
  1564. }
  1565. if (elements != null) {
  1566. Enumeration e = elements.keys();
  1567. while (e.hasMoreElements()) {
  1568. String name = (String) e.nextElement();
  1569. Class clazz = (Class) elements.get(name);
  1570. if (elementClass.equals(clazz)) {
  1571. return "The <" + name + "> " + typeName;
  1572. }
  1573. }
  1574. }
  1575. return "Class " + elementClass.getName();
  1576. }
  1577. /**
  1578. * Sends a "build started" event to the build listeners for this project.
  1579. */
  1580. protected void fireBuildStarted() {
  1581. BuildEvent event = new BuildEvent(this);
  1582. for (int i = 0; i < listeners.size(); i++) {
  1583. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1584. listener.buildStarted(event);
  1585. }
  1586. }
  1587. /**
  1588. * Sends a "build finished" event to the build listeners for this project.
  1589. * @param exception an exception indicating a reason for a build
  1590. * failure. May be <code>null</code>, indicating
  1591. * a successful build.
  1592. */
  1593. protected void fireBuildFinished(Throwable exception) {
  1594. BuildEvent event = new BuildEvent(this);
  1595. event.setException(exception);
  1596. for (int i = 0; i < listeners.size(); i++) {
  1597. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1598. listener.buildFinished(event);
  1599. }
  1600. }
  1601. /**
  1602. * Sends a "target started" event to the build listeners for this project.
  1603. *
  1604. * @param target The target which is starting to build.
  1605. * Must not be <code>null</code>.
  1606. */
  1607. protected void fireTargetStarted(Target target) {
  1608. BuildEvent event = new BuildEvent(target);
  1609. for (int i = 0; i < listeners.size(); i++) {
  1610. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1611. listener.targetStarted(event);
  1612. }
  1613. }
  1614. /**
  1615. * Sends a "target finished" event to the build listeners for this
  1616. * project.
  1617. *
  1618. * @param target The target which has finished building.
  1619. * Must not be <code>null</code>.
  1620. * @param exception an exception indicating a reason for a build
  1621. * failure. May be <code>null</code>, indicating
  1622. * a successful build.
  1623. */
  1624. protected void fireTargetFinished(Target target, Throwable exception) {
  1625. BuildEvent event = new BuildEvent(target);
  1626. event.setException(exception);
  1627. for (int i = 0; i < listeners.size(); i++) {
  1628. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1629. listener.targetFinished(event);
  1630. }
  1631. }
  1632. /**
  1633. * Sends a "task started" event to the build listeners for this project.
  1634. *
  1635. * @param task The target which is starting to execute.
  1636. * Must not be <code>null</code>.
  1637. */
  1638. protected void fireTaskStarted(Task task) {
  1639. // register this as the current task on the current thread.
  1640. registerThreadTask(Thread.currentThread(), task);
  1641. BuildEvent event = new BuildEvent(task);
  1642. for (int i = 0; i < listeners.size(); i++) {
  1643. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1644. listener.taskStarted(event);
  1645. }
  1646. }
  1647. /**
  1648. * Sends a "task finished" event to the build listeners for this
  1649. * project.
  1650. *
  1651. * @param task The task which has finished executing.
  1652. * Must not be <code>null</code>.
  1653. * @param exception an exception indicating a reason for a build
  1654. * failure. May be <code>null</code>, indicating
  1655. * a successful build.
  1656. */
  1657. protected void fireTaskFinished(Task task, Throwable exception) {
  1658. registerThreadTask(Thread.currentThread(), null);
  1659. System.out.flush();
  1660. System.err.flush();
  1661. BuildEvent event = new BuildEvent(task);
  1662. event.setException(exception);
  1663. for (int i = 0; i < listeners.size(); i++) {
  1664. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1665. listener.taskFinished(event);
  1666. }
  1667. }
  1668. /**
  1669. * Sends a "message logged" event to the build listeners for this project.
  1670. *
  1671. * @param event The event to send. This should be built up with the
  1672. * appropriate task/target/project by the caller, so that
  1673. * this method can set the message and priority, then send
  1674. * the event. Must not be <code>null</code>.
  1675. * @param message The message to send. Should not be <code>null</code>.
  1676. * @param priority The priority of the message.
  1677. */
  1678. private void fireMessageLoggedEvent(BuildEvent event, String message,
  1679. int priority) {
  1680. event.setMessage(message, priority);
  1681. for (int i = 0; i < listeners.size(); i++) {
  1682. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1683. listener.messageLogged(event);
  1684. }
  1685. }
  1686. /**
  1687. * Sends a "message logged" project level event to the build listeners for
  1688. * this project.
  1689. *
  1690. * @param project The project generating the event.
  1691. * Should not be <code>null</code>.
  1692. * @param message The message to send. Should not be <code>null</code>.
  1693. * @param priority The priority of the message.
  1694. */
  1695. protected void fireMessageLogged(Project project, String message,
  1696. int priority) {
  1697. BuildEvent event = new BuildEvent(project);
  1698. fireMessageLoggedEvent(event, message, priority);
  1699. }
  1700. /**
  1701. * Sends a "message logged" target level event to the build listeners for
  1702. * this project.
  1703. *
  1704. * @param target The target generating the event.
  1705. * Must not be <code>null</code>.
  1706. * @param message The message to send. Should not be <code>null</code>.
  1707. * @param priority The priority of the message.
  1708. */
  1709. protected void fireMessageLogged(Target target, String message,
  1710. int priority) {
  1711. BuildEvent event = new BuildEvent(target);
  1712. fireMessageLoggedEvent(event, message, priority);
  1713. }
  1714. /**
  1715. * Sends a "message logged" task level event to the build listeners for
  1716. * this project.
  1717. *
  1718. * @param task The task generating the event.
  1719. * Must not be <code>null</code>.
  1720. * @param message The message to send. Should not be <code>null</code>.
  1721. * @param priority The priority of the message.
  1722. */
  1723. protected void fireMessageLogged(Task task, String message, int priority) {
  1724. BuildEvent event = new BuildEvent(task);
  1725. fireMessageLoggedEvent(event, message, priority);
  1726. }
  1727. /**
  1728. * Register a task as the current task for a thread.
  1729. * If the task is null, the thread's entry is removed.
  1730. *
  1731. * @param thread the thread on which the task is registered.
  1732. * @param task the task to be registered.
  1733. * @since 1.102, Ant 1.5
  1734. */
  1735. public void registerThreadTask(Thread thread, Task task) {
  1736. if (task != null) {
  1737. threadTasks.put(thread, task);
  1738. } else {
  1739. threadTasks.remove(thread);
  1740. }
  1741. }
  1742. /**
  1743. * Get the current task assopciated with a thread, if any
  1744. *
  1745. * @param thread the thread for which the task is required.
  1746. * @return the task which is currently registered for the given thread or
  1747. * null if no task is registered.
  1748. */
  1749. public Task getThreadTask(Thread thread) {
  1750. return (Task) threadTasks.get(thread);
  1751. }
  1752. }