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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  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 final static int MSG_ERR = 0;
  85. /** Message priority of "warning". */
  86. public final static int MSG_WARN = 1;
  87. /** Message priority of "information". */
  88. public final static int MSG_INFO = 2;
  89. /** Message priority of "verbose". */
  90. public final static int MSG_VERBOSE = 3;
  91. /** Message priority of "debug". */
  92. public final static int MSG_DEBUG = 4;
  93. /**
  94. * Constant for the "visiting" state, used when
  95. * traversing a DFS of target dependencies.
  96. */
  97. private final static String VISITING = "VISITING";
  98. /**
  99. * Constant for the "visited" state, used when
  100. * traversing a DFS of target dependencies.
  101. */
  102. private final static String VISITED = "VISITED";
  103. /** Version constant for Java 1.0 */
  104. public final static String JAVA_1_0 = JavaEnvUtils.JAVA_1_0;
  105. /** Version constant for Java 1.1 */
  106. public final static String JAVA_1_1 = JavaEnvUtils.JAVA_1_1;
  107. /** Version constant for Java 1.2 */
  108. public final static String JAVA_1_2 = JavaEnvUtils.JAVA_1_2;
  109. /** Version constant for Java 1.3 */
  110. public final static String JAVA_1_3 = JavaEnvUtils.JAVA_1_3;
  111. /** Version constant for Java 1.4 */
  112. public final static String JAVA_1_4 = JavaEnvUtils.JAVA_1_4;
  113. /** Default filter start token. */
  114. public final static String TOKEN_START = FilterSet.DEFAULT_TOKEN_START;
  115. /** Default filter end token. */
  116. public final static 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. }
  1038. else {
  1039. if (isError) {
  1040. task.handleErrorOutput(line);
  1041. }
  1042. else {
  1043. task.handleOutput(line);
  1044. }
  1045. }
  1046. }
  1047. /**
  1048. * Executes the specified target and any targets it depends on.
  1049. *
  1050. * @param targetName The name of the target to execute.
  1051. * Must not be <code>null</code>.
  1052. *
  1053. * @exception BuildException if the build failed
  1054. */
  1055. public void executeTarget(String targetName) throws BuildException {
  1056. // sanity check ourselves, if we've been asked to build nothing
  1057. // then we should complain
  1058. if (targetName == null) {
  1059. String msg = "No target specified";
  1060. throw new BuildException(msg);
  1061. }
  1062. // Sort the dependency tree, and run everything from the
  1063. // beginning until we hit our targetName.
  1064. // Sorting checks if all the targets (and dependencies)
  1065. // exist, and if there is any cycle in the dependency
  1066. // graph.
  1067. Vector sortedTargets = topoSort(targetName, targets);
  1068. int curidx = 0;
  1069. Target curtarget;
  1070. do {
  1071. curtarget = (Target) sortedTargets.elementAt(curidx++);
  1072. curtarget.performTasks();
  1073. } while (!curtarget.getName().equals(targetName));
  1074. }
  1075. /**
  1076. * Returns the canonical form of a filename.
  1077. * <p>
  1078. * If the specified file name is relative it is resolved
  1079. * with respect to the given root directory.
  1080. *
  1081. * @param fileName The name of the file to resolve.
  1082. * Must not be <code>null</code>.
  1083. *
  1084. * @param rootDir The directory to resolve relative file names with
  1085. * respect to. May be <code>null</code>, in which case
  1086. * the current directory is used.
  1087. *
  1088. * @return the resolved File.
  1089. *
  1090. * @deprecated
  1091. */
  1092. public File resolveFile(String fileName, File rootDir) {
  1093. return fileUtils.resolveFile(rootDir, fileName);
  1094. }
  1095. /**
  1096. * Returns the canonical form of a filename.
  1097. * <p>
  1098. * If the specified file name is relative it is resolved
  1099. * with respect to the project's base directory.
  1100. *
  1101. * @param fileName The name of the file to resolve.
  1102. * Must not be <code>null</code>.
  1103. *
  1104. * @return the resolved File.
  1105. *
  1106. */
  1107. public File resolveFile(String fileName) {
  1108. return fileUtils.resolveFile(baseDir, fileName);
  1109. }
  1110. /**
  1111. * Translates a path into its native (platform specific) format.
  1112. * <p>
  1113. * This method uses PathTokenizer to separate the input path
  1114. * into its components. This handles DOS style paths in a relatively
  1115. * sensible way. The file separators are then converted to their platform
  1116. * specific versions.
  1117. *
  1118. * @param toProcess The path to be translated.
  1119. * May be <code>null</code>.
  1120. *
  1121. * @return the native version of the specified path or
  1122. * an empty string if the path is <code>null</code> or empty.
  1123. *
  1124. * @see PathTokenizer
  1125. */
  1126. public static String translatePath(String toProcess) {
  1127. if ( toProcess == null || toProcess.length() == 0 ) {
  1128. return "";
  1129. }
  1130. StringBuffer path = new StringBuffer(toProcess.length() + 50);
  1131. PathTokenizer tokenizer = new PathTokenizer(toProcess);
  1132. while (tokenizer.hasMoreTokens()) {
  1133. String pathComponent = tokenizer.nextToken();
  1134. pathComponent = pathComponent.replace('/', File.separatorChar);
  1135. pathComponent = pathComponent.replace('\\', File.separatorChar);
  1136. if (path.length() != 0) {
  1137. path.append(File.pathSeparatorChar);
  1138. }
  1139. path.append(pathComponent);
  1140. }
  1141. return path.toString();
  1142. }
  1143. /**
  1144. * Convenience method to copy a file from a source to a destination.
  1145. * No filtering is performed.
  1146. *
  1147. * @param sourceFile Name of file to copy from.
  1148. * Must not be <code>null</code>.
  1149. * @param destFile Name of file to copy to.
  1150. * Must not be <code>null</code>.
  1151. *
  1152. * @exception IOException if the copying fails
  1153. *
  1154. * @deprecated
  1155. */
  1156. public void copyFile(String sourceFile, String destFile)
  1157. throws IOException {
  1158. fileUtils.copyFile(sourceFile, destFile);
  1159. }
  1160. /**
  1161. * Convenience method to copy a file from a source to a destination
  1162. * specifying if token filtering should be used.
  1163. *
  1164. * @param sourceFile Name of file to copy from.
  1165. * Must not be <code>null</code>.
  1166. * @param destFile Name of file to copy to.
  1167. * Must not be <code>null</code>.
  1168. * @param filtering Whether or not token filtering should be used during
  1169. * the copy.
  1170. *
  1171. * @exception IOException if the copying fails
  1172. *
  1173. * @deprecated
  1174. */
  1175. public void copyFile(String sourceFile, String destFile, boolean filtering)
  1176. throws IOException {
  1177. fileUtils.copyFile(sourceFile, destFile,
  1178. filtering ? globalFilters : null);
  1179. }
  1180. /**
  1181. * Convenience method to copy a file from a source to a
  1182. * destination specifying if token filtering should be used and if
  1183. * source files may overwrite newer destination files.
  1184. *
  1185. * @param sourceFile Name of file to copy from.
  1186. * Must not be <code>null</code>.
  1187. * @param destFile Name of file to copy to.
  1188. * Must not be <code>null</code>.
  1189. * @param filtering Whether or not token filtering should be used during
  1190. * the copy.
  1191. * @param overwrite Whether or not the destination file should be
  1192. * overwritten if it already exists.
  1193. *
  1194. * @exception IOException if the copying fails
  1195. *
  1196. * @deprecated
  1197. */
  1198. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1199. boolean overwrite) throws IOException {
  1200. fileUtils.copyFile(sourceFile, destFile,
  1201. filtering ? globalFilters : null, overwrite);
  1202. }
  1203. /**
  1204. * Convenience method to copy a file from a source to a
  1205. * destination specifying if token filtering should be used, if
  1206. * source files may overwrite newer destination files, and if the
  1207. * last modified time of the resulting file should be set to
  1208. * that of the source file.
  1209. *
  1210. * @param sourceFile Name of file to copy from.
  1211. * Must not be <code>null</code>.
  1212. * @param destFile Name of file to copy to.
  1213. * Must not be <code>null</code>.
  1214. * @param filtering Whether or not token filtering should be used during
  1215. * the copy.
  1216. * @param overwrite Whether or not the destination file should be
  1217. * overwritten if it already exists.
  1218. * @param preserveLastModified Whether or not the last modified time of
  1219. * the resulting file should be set to that
  1220. * of the source file.
  1221. *
  1222. * @exception IOException if the copying fails
  1223. *
  1224. * @deprecated
  1225. */
  1226. public void copyFile(String sourceFile, String destFile, boolean filtering,
  1227. boolean overwrite, boolean preserveLastModified)
  1228. throws IOException {
  1229. fileUtils.copyFile(sourceFile, destFile,
  1230. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1231. }
  1232. /**
  1233. * Convenience method to copy a file from a source to a destination.
  1234. * No filtering is performed.
  1235. *
  1236. * @param sourceFile File to copy from.
  1237. * Must not be <code>null</code>.
  1238. * @param destFile File to copy to.
  1239. * Must not be <code>null</code>.
  1240. *
  1241. * @exception IOException if the copying fails
  1242. *
  1243. * @deprecated
  1244. */
  1245. public void copyFile(File sourceFile, File destFile) throws IOException {
  1246. fileUtils.copyFile(sourceFile, destFile);
  1247. }
  1248. /**
  1249. * Convenience method to copy a file from a source to a destination
  1250. * specifying if token filtering should be used.
  1251. *
  1252. * @param sourceFile File to copy from.
  1253. * Must not be <code>null</code>.
  1254. * @param destFile File to copy to.
  1255. * Must not be <code>null</code>.
  1256. * @param filtering Whether or not token filtering should be used during
  1257. * the copy.
  1258. *
  1259. * @exception IOException if the copying fails
  1260. *
  1261. * @deprecated
  1262. */
  1263. public void copyFile(File sourceFile, File destFile, boolean filtering)
  1264. throws IOException {
  1265. fileUtils.copyFile(sourceFile, destFile,
  1266. filtering ? globalFilters : null);
  1267. }
  1268. /**
  1269. * Convenience method to copy a file from a source to a
  1270. * destination specifying if token filtering should be used and if
  1271. * source files may overwrite newer destination files.
  1272. *
  1273. * @param sourceFile File to copy from.
  1274. * Must not be <code>null</code>.
  1275. * @param destFile File to copy to.
  1276. * Must not be <code>null</code>.
  1277. * @param filtering Whether or not token filtering should be used during
  1278. * the copy.
  1279. * @param overwrite Whether or not the destination file should be
  1280. * overwritten if it already exists.
  1281. *
  1282. * @exception IOException if the file cannot be copied.
  1283. *
  1284. * @deprecated
  1285. */
  1286. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1287. boolean overwrite) throws IOException {
  1288. fileUtils.copyFile(sourceFile, destFile,
  1289. filtering ? globalFilters : null, overwrite);
  1290. }
  1291. /**
  1292. * Convenience method to copy a file from a source to a
  1293. * destination specifying if token filtering should be used, if
  1294. * source files may overwrite newer destination files, and if the
  1295. * last modified time of the resulting file should be set to
  1296. * that of the source file.
  1297. *
  1298. * @param sourceFile File to copy from.
  1299. * Must not be <code>null</code>.
  1300. * @param destFile File to copy to.
  1301. * Must not be <code>null</code>.
  1302. * @param filtering Whether or not token filtering should be used during
  1303. * the copy.
  1304. * @param overwrite Whether or not the destination file should be
  1305. * overwritten if it already exists.
  1306. * @param preserveLastModified Whether or not the last modified time of
  1307. * the resulting file should be set to that
  1308. * of the source file.
  1309. *
  1310. * @exception IOException if the file cannot be copied.
  1311. *
  1312. * @deprecated
  1313. */
  1314. public void copyFile(File sourceFile, File destFile, boolean filtering,
  1315. boolean overwrite, boolean preserveLastModified)
  1316. throws IOException {
  1317. fileUtils.copyFile(sourceFile, destFile,
  1318. filtering ? globalFilters : null, overwrite, preserveLastModified);
  1319. }
  1320. /**
  1321. * Calls File.setLastModified(long time) on Java above 1.1, and logs
  1322. * a warning on Java 1.1.
  1323. *
  1324. * @param file The file to set the last modified time on.
  1325. * Must not be <code>null</code>.
  1326. *
  1327. * @param time the required modification time.
  1328. *
  1329. * @deprecated
  1330. *
  1331. * @exception BuildException if the last modified time cannot be set
  1332. * despite running on a platform with a version
  1333. * above 1.1.
  1334. */
  1335. public void setFileLastModified(File file, long time)
  1336. throws BuildException {
  1337. if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1) {
  1338. log("Cannot change the modification time of " + file
  1339. + " in JDK 1.1", Project.MSG_WARN);
  1340. return;
  1341. }
  1342. fileUtils.setFileLastModified(file, time);
  1343. log("Setting modification time for " + file, MSG_VERBOSE);
  1344. }
  1345. /**
  1346. * Returns the boolean equivalent of a string, which is considered
  1347. * <code>true</code> if either <code>"on"</code>, <code>"true"</code>,
  1348. * or <code>"yes"</code> is found, ignoring case.
  1349. *
  1350. * @param s The string to convert to a boolean value.
  1351. * Must not be <code>null</code>.
  1352. *
  1353. * @return <code>true</code> if the given string is <code>"on"</code>,
  1354. * <code>"true"</code> or <code>"yes"</code>, or
  1355. * <code>false</code> otherwise.
  1356. */
  1357. public static boolean toBoolean(String s) {
  1358. return (s.equalsIgnoreCase("on") ||
  1359. s.equalsIgnoreCase("true") ||
  1360. s.equalsIgnoreCase("yes"));
  1361. }
  1362. /**
  1363. * Topologically sorts a set of targets.
  1364. *
  1365. * @param root The name of the root target. The sort is created in such
  1366. * a way that the sequence of Targets up to the root
  1367. * target is the minimum possible such sequence.
  1368. * Must not be <code>null</code>.
  1369. * @param targets A map of names to targets (String to Target).
  1370. * Must not be <code>null</code>.
  1371. * @return a vector of strings with the names of the targets in
  1372. * sorted order.
  1373. * @exception BuildException if there is a cyclic dependency among the
  1374. * targets, or if a named target does not exist.
  1375. */
  1376. public final Vector topoSort(String root, Hashtable targets)
  1377. throws BuildException {
  1378. Vector ret = new Vector();
  1379. Hashtable state = new Hashtable();
  1380. Stack visiting = new Stack();
  1381. // We first run a DFS based sort using the root as the starting node.
  1382. // This creates the minimum sequence of Targets to the root node.
  1383. // We then do a sort on any remaining unVISITED targets.
  1384. // This is unnecessary for doing our build, but it catches
  1385. // circular dependencies or missing Targets on the entire
  1386. // dependency tree, not just on the Targets that depend on the
  1387. // build Target.
  1388. tsort(root, targets, state, visiting, ret);
  1389. log("Build sequence for target `" + root + "' is " + ret, MSG_VERBOSE);
  1390. for (Enumeration en = targets.keys(); en.hasMoreElements();) {
  1391. String curTarget = (String)(en.nextElement());
  1392. String st = (String) state.get(curTarget);
  1393. if (st == null) {
  1394. tsort(curTarget, targets, state, visiting, ret);
  1395. }
  1396. else if (st == VISITING) {
  1397. throw new RuntimeException("Unexpected node in visiting state: "
  1398. + curTarget);
  1399. }
  1400. }
  1401. log("Complete build sequence is " + ret, MSG_VERBOSE);
  1402. return ret;
  1403. }
  1404. /**
  1405. * Performs a single step in a recursive depth-first-search traversal of
  1406. * the target dependency tree.
  1407. * <p>
  1408. * The current target is first set to the "visiting" state, and pushed
  1409. * onto the "visiting" stack.
  1410. * <p>
  1411. * An exception is then thrown if any child of the current node is in the
  1412. * visiting state, as that implies a circular dependency. The exception
  1413. * contains details of the cycle, using elements of the "visiting" stack.
  1414. * <p>
  1415. * If any child has not already been "visited", this method is called
  1416. * recursively on it.
  1417. * <p>
  1418. * The current target is then added to the ordered list of targets. Note
  1419. * that this is performed after the children have been visited in order
  1420. * to get the correct order. The current target is set to the "visited"
  1421. * state.
  1422. * <p>
  1423. * By the time this method returns, the ordered list contains the sequence
  1424. * of targets up to and including the current target.
  1425. *
  1426. * @param root The current target to inspect.
  1427. * Must not be <code>null</code>.
  1428. * @param targets A mapping from names to targets (String to Target).
  1429. * Must not be <code>null</code>.
  1430. * @param state A mapping from target names to states
  1431. * (String to String).
  1432. * The states in question are "VISITING" and "VISITED".
  1433. * Must not be <code>null</code>.
  1434. * @param visiting A stack of targets which are currently being visited.
  1435. * Must not be <code>null</code>.
  1436. * @param ret The list to add target names to. This will end up
  1437. * containing the complete list of depenencies in
  1438. * dependency order.
  1439. * Must not be <code>null</code>.
  1440. *
  1441. * @exception BuildException if a non-existent target is specified or if
  1442. * a circular dependency is detected.
  1443. */
  1444. private final void tsort(String root, Hashtable targets,
  1445. Hashtable state, Stack visiting,
  1446. Vector ret)
  1447. throws BuildException {
  1448. state.put(root, VISITING);
  1449. visiting.push(root);
  1450. Target target = (Target)(targets.get(root));
  1451. // Make sure we exist
  1452. if (target == null) {
  1453. StringBuffer sb = new StringBuffer("Target `");
  1454. sb.append(root);
  1455. sb.append("' does not exist in this project. ");
  1456. visiting.pop();
  1457. if (!visiting.empty()) {
  1458. String parent = (String)visiting.peek();
  1459. sb.append("It is used from target `");
  1460. sb.append(parent);
  1461. sb.append("'.");
  1462. }
  1463. throw new BuildException(new String(sb));
  1464. }
  1465. for (Enumeration en = target.getDependencies(); en.hasMoreElements();) {
  1466. String cur = (String) en.nextElement();
  1467. String m = (String)state.get(cur);
  1468. if (m == null) {
  1469. // Not been visited
  1470. tsort(cur, targets, state, visiting, ret);
  1471. }
  1472. else if (m == VISITING) {
  1473. // Currently visiting this node, so have a cycle
  1474. throw makeCircularException(cur, visiting);
  1475. }
  1476. }
  1477. String p = (String) visiting.pop();
  1478. if (root != p) {
  1479. throw new RuntimeException("Unexpected internal error: expected to "
  1480. + "pop " + root + " but got " + p);
  1481. }
  1482. state.put(root, VISITED);
  1483. ret.addElement(target);
  1484. }
  1485. /**
  1486. * Builds an appropriate exception detailing a specified circular
  1487. * dependency.
  1488. *
  1489. * @param end The dependency to stop at. Must not be <code>null</code>.
  1490. * @param stk A stack of dependencies. Must not be <code>null</code>.
  1491. *
  1492. * @return a BuildException detailing the specified circular dependency.
  1493. */
  1494. private static BuildException makeCircularException(String end, Stack stk) {
  1495. StringBuffer sb = new StringBuffer("Circular dependency: ");
  1496. sb.append(end);
  1497. String c;
  1498. do {
  1499. c = (String)stk.pop();
  1500. sb.append(" <- ");
  1501. sb.append(c);
  1502. } while (!c.equals(end));
  1503. return new BuildException(new String(sb));
  1504. }
  1505. /**
  1506. * Adds a reference to the project.
  1507. *
  1508. * @param name The name of the reference. Must not be <code>null</code>.
  1509. * @param value The value of the reference. Must not be <code>null</code>.
  1510. */
  1511. public void addReference(String name, Object value) {
  1512. Object old = references.get(name);
  1513. if (old == value) {
  1514. // no warning, this is not changing anything
  1515. return;
  1516. }
  1517. if (old != null) {
  1518. log("Overriding previous definition of reference to " + name,
  1519. MSG_WARN);
  1520. }
  1521. log("Adding reference: " + name + " -> " + value, MSG_DEBUG);
  1522. references.put(name, value);
  1523. }
  1524. /**
  1525. * Returns a map of the references in the project (String to Object).
  1526. * The returned hashtable is "live" and so should not be modified.
  1527. *
  1528. * @return a map of the references in the project (String to Object).
  1529. */
  1530. public Hashtable getReferences() {
  1531. return references;
  1532. }
  1533. /**
  1534. * Looks up a reference by its key (ID).
  1535. *
  1536. * @param key The key for the desired reference.
  1537. * Must not be <code>null</code>.
  1538. *
  1539. * @return the reference with the specified ID, or <code>null</code> if
  1540. * there is no such reference in the project.
  1541. */
  1542. public Object getReference(String key) {
  1543. return references.get(key);
  1544. }
  1545. /**
  1546. * Returns a description of the type of the given element, with
  1547. * special handling for instances of tasks and data types.
  1548. * <p>
  1549. * This is useful for logging purposes.
  1550. *
  1551. * @param element The element to describe.
  1552. * Must not be <code>null</code>.
  1553. *
  1554. * @return a description of the element type
  1555. *
  1556. * @since 1.95, Ant 1.5
  1557. */
  1558. public String getElementName(Object element) {
  1559. Hashtable elements = taskClassDefinitions;
  1560. Class elementClass = element.getClass();
  1561. String typeName = "task";
  1562. if (!elements.contains(elementClass)) {
  1563. elements = dataClassDefinitions;
  1564. typeName = "data type";
  1565. if (!elements.contains(elementClass)) {
  1566. elements = null;
  1567. }
  1568. }
  1569. if (elements != null) {
  1570. Enumeration e = elements.keys();
  1571. while (e.hasMoreElements()) {
  1572. String name = (String) e.nextElement();
  1573. Class clazz = (Class) elements.get(name);
  1574. if (elementClass.equals(clazz)) {
  1575. return "The <" + name + "> " + typeName;
  1576. }
  1577. }
  1578. }
  1579. return "Class " + elementClass.getName();
  1580. }
  1581. /**
  1582. * Sends a "build started" event to the build listeners for this project.
  1583. */
  1584. protected void fireBuildStarted() {
  1585. BuildEvent event = new BuildEvent(this);
  1586. for (int i = 0; i < listeners.size(); i++) {
  1587. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1588. listener.buildStarted(event);
  1589. }
  1590. }
  1591. /**
  1592. * Sends a "build finished" event to the build listeners for this project.
  1593. * @param exception an exception indicating a reason for a build
  1594. * failure. May be <code>null</code>, indicating
  1595. * a successful build.
  1596. */
  1597. protected void fireBuildFinished(Throwable exception) {
  1598. BuildEvent event = new BuildEvent(this);
  1599. event.setException(exception);
  1600. for (int i = 0; i < listeners.size(); i++) {
  1601. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1602. listener.buildFinished(event);
  1603. }
  1604. }
  1605. /**
  1606. * Sends a "target started" event to the build listeners for this project.
  1607. *
  1608. * @param target The target which is starting to build.
  1609. * Must not be <code>null</code>.
  1610. */
  1611. protected void fireTargetStarted(Target target) {
  1612. BuildEvent event = new BuildEvent(target);
  1613. for (int i = 0; i < listeners.size(); i++) {
  1614. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1615. listener.targetStarted(event);
  1616. }
  1617. }
  1618. /**
  1619. * Sends a "target finished" event to the build listeners for this
  1620. * project.
  1621. *
  1622. * @param target The target which has finished building.
  1623. * Must not be <code>null</code>.
  1624. * @param exception an exception indicating a reason for a build
  1625. * failure. May be <code>null</code>, indicating
  1626. * a successful build.
  1627. */
  1628. protected void fireTargetFinished(Target target, Throwable exception) {
  1629. BuildEvent event = new BuildEvent(target);
  1630. event.setException(exception);
  1631. for (int i = 0; i < listeners.size(); i++) {
  1632. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1633. listener.targetFinished(event);
  1634. }
  1635. }
  1636. /**
  1637. * Sends a "task started" event to the build listeners for this project.
  1638. *
  1639. * @param task The target which is starting to execute.
  1640. * Must not be <code>null</code>.
  1641. */
  1642. protected void fireTaskStarted(Task task) {
  1643. // register this as the current task on the current thread.
  1644. threadTasks.put(Thread.currentThread(), task);
  1645. BuildEvent event = new BuildEvent(task);
  1646. for (int i = 0; i < listeners.size(); i++) {
  1647. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1648. listener.taskStarted(event);
  1649. }
  1650. }
  1651. /**
  1652. * Sends a "task finished" event to the build listeners for this
  1653. * project.
  1654. *
  1655. * @param task The task which has finished executing.
  1656. * Must not be <code>null</code>.
  1657. * @param exception an exception indicating a reason for a build
  1658. * failure. May be <code>null</code>, indicating
  1659. * a successful build.
  1660. */
  1661. protected void fireTaskFinished(Task task, Throwable exception) {
  1662. threadTasks.remove(Thread.currentThread());
  1663. System.out.flush();
  1664. System.err.flush();
  1665. BuildEvent event = new BuildEvent(task);
  1666. event.setException(exception);
  1667. for (int i = 0; i < listeners.size(); i++) {
  1668. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1669. listener.taskFinished(event);
  1670. }
  1671. }
  1672. /**
  1673. * Sends a "message logged" event to the build listeners for this project.
  1674. *
  1675. * @param event The event to send. This should be built up with the
  1676. * appropriate task/target/project by the caller, so that
  1677. * this method can set the message and priority, then send
  1678. * the event. Must not be <code>null</code>.
  1679. * @param message The message to send. Should not be <code>null</code>.
  1680. * @param priority The priority of the message.
  1681. */
  1682. private void fireMessageLoggedEvent(BuildEvent event, String message,
  1683. int priority) {
  1684. event.setMessage(message, priority);
  1685. for (int i = 0; i < listeners.size(); i++) {
  1686. BuildListener listener = (BuildListener) listeners.elementAt(i);
  1687. listener.messageLogged(event);
  1688. }
  1689. }
  1690. /**
  1691. * Sends a "message logged" project level event to the build listeners for
  1692. * this project.
  1693. *
  1694. * @param project The project generating the event.
  1695. * Should not be <code>null</code>.
  1696. * @param message The message to send. Should not be <code>null</code>.
  1697. * @param priority The priority of the message.
  1698. */
  1699. protected void fireMessageLogged(Project project, String message,
  1700. int priority) {
  1701. BuildEvent event = new BuildEvent(project);
  1702. fireMessageLoggedEvent(event, message, priority);
  1703. }
  1704. /**
  1705. * Sends a "message logged" target level event to the build listeners for
  1706. * this project.
  1707. *
  1708. * @param target The target generating the event.
  1709. * Must not be <code>null</code>.
  1710. * @param message The message to send. Should not be <code>null</code>.
  1711. * @param priority The priority of the message.
  1712. */
  1713. protected void fireMessageLogged(Target target, String message,
  1714. int priority) {
  1715. BuildEvent event = new BuildEvent(target);
  1716. fireMessageLoggedEvent(event, message, priority);
  1717. }
  1718. /**
  1719. * Sends a "message logged" task level event to the build listeners for
  1720. * this project.
  1721. *
  1722. * @param task The task generating the event.
  1723. * Must not be <code>null</code>.
  1724. * @param message The message to send. Should not be <code>null</code>.
  1725. * @param priority The priority of the message.
  1726. */
  1727. protected void fireMessageLogged(Task task, String message, int priority) {
  1728. BuildEvent event = new BuildEvent(task);
  1729. fireMessageLoggedEvent(event, message, priority);
  1730. }
  1731. }