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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // ---------------------------------------------------------------------
  2. // (c)2000 Apache Software Foundation
  3. //
  4. // ---------------------------------------------------------------------
  5. package org.apache.ant;
  6. import java.io.*;
  7. import java.util.*;
  8. /**
  9. * In memory container for an Ant project.
  10. *
  11. * @author James Duncan Davidson (duncan@apache.org)
  12. */
  13. public class Project {
  14. // -----------------------------------------------------------------
  15. // PRIVATE DATA MEMBERS
  16. // -----------------------------------------------------------------
  17. /**
  18. *
  19. */
  20. //private Ant ant;
  21. /**
  22. * Base directory of this project. Usually this value is the directory
  23. * where the project file was found, but can be different.
  24. */
  25. private File baseDir;
  26. /**
  27. *
  28. */
  29. private String defaultTargetName;
  30. /**
  31. * Short description of the project.
  32. */
  33. private String description;
  34. /**
  35. * Front end that this project communicates to.
  36. */
  37. private FrontEnd frontEnd;
  38. /**
  39. * Properties of this project.
  40. */
  41. private Properties properties = new Properties();
  42. /**
  43. * Parent project to this project, if one exists.
  44. */
  45. private Project parentProject = null;
  46. /**
  47. *
  48. */
  49. private String name;
  50. /**
  51. * Hashtable containing all of the targets that are part of this
  52. * project. Targets are stored in this hashtable using the name
  53. * of the target as the key and the Target object for the target
  54. * as the value.
  55. */
  56. private Hashtable targets = new Hashtable();
  57. /**
  58. * TaskManager for this project.
  59. */
  60. private TaskManager taskManager;
  61. // -----------------------------------------------------------------
  62. // CONSTRUCTORS
  63. // -----------------------------------------------------------------
  64. /**
  65. * Creates a new Project object with the given FrontEnd and TaskManager
  66. */
  67. public Project(FrontEnd frontEnd, TaskManager taskManager) {
  68. this.frontEnd = frontEnd;
  69. this.taskManager = taskManager;
  70. }
  71. // -----------------------------------------------------------------
  72. // PUBLIC METHODS
  73. // -----------------------------------------------------------------
  74. /**
  75. * Adds a target to this project.
  76. */
  77. public void addTarget(Target target) {
  78. // XXX check out for name, if null, reject!
  79. targets.put(target.getName(), target);
  80. }
  81. /**
  82. * Returns the base directory of this project.
  83. */
  84. public File getBaseDir() {
  85. return baseDir;
  86. }
  87. /**
  88. * Returns the default target for this project, if there is one. Otherwise
  89. * it returns null.
  90. */
  91. public String getDefaultTargetName() {
  92. return defaultTargetName;
  93. }
  94. /**
  95. * Returns a short description of this project, if any. If not, returns
  96. * null.
  97. */
  98. public String getDescription() {
  99. return description;
  100. }
  101. /**
  102. * Gets the front end that is running this project.
  103. */
  104. public FrontEnd getFrontEnd() {
  105. return frontEnd;
  106. }
  107. /**
  108. * Returns the parent Project object to this Project if a parent
  109. * project exists. If there is not a parent Project object, null
  110. * is returned.
  111. */
  112. public Project getParent() {
  113. return parentProject;
  114. }
  115. /**
  116. * Returns the target identified with the given name. If no target
  117. * is known by the given name, then null is returned.
  118. */
  119. public Target getTarget(String name) {
  120. return (Target)targets.get(name);
  121. }
  122. /**
  123. * Gets an exumeration of all the targets that are part of this project.
  124. */
  125. public Enumeration getTargets() {
  126. return targets.elements();
  127. }
  128. /**
  129. * Gets the name of this project.
  130. */
  131. public String getName() {
  132. return name;
  133. }
  134. /**
  135. * Returns the value of a property. Returns null if the property does
  136. * not exist.
  137. */
  138. public String getProperty(String propertyName) {
  139. return properties.getProperty(propertyName);
  140. }
  141. /**
  142. *
  143. */
  144. //public void setAnt(Ant ant) {
  145. // this.ant = ant;
  146. //}
  147. /**
  148. * Sets the base dir for this project.
  149. */
  150. public void setBaseDir(File dir) {
  151. // XXX should check this to make sure it's a dir!
  152. baseDir = dir;
  153. }
  154. /**
  155. * Sets the default target for this project.
  156. */
  157. public void setDefaultTargetName(String targetName) {
  158. defaultTargetName = targetName;
  159. }
  160. /**
  161. * Sets the description for this project.
  162. */
  163. public void setDescription(String description) {
  164. this.description = description;
  165. }
  166. /**
  167. * Sets the front end for this project.
  168. */
  169. public void setFrontEnd(FrontEnd frontEnd) {
  170. this.frontEnd = frontEnd;
  171. }
  172. /**
  173. * Sets the name of this project.
  174. */
  175. public void setName(String name) {
  176. this.name = name;
  177. }
  178. /**
  179. * Sets a property on this project. If the property is already
  180. * set, this method will override it.
  181. */
  182. public void setProperty(String propertyName, String propertyValue) {
  183. properties.put(propertyName, propertyValue);
  184. }
  185. /**
  186. * Starts a build of this project using the default target if one
  187. * is set.
  188. */
  189. public void startBuild() throws AntException {
  190. // XXX need to do something if the default target isn't set..
  191. // maybe look for target name 'default', then bail?
  192. startBuild(defaultTargetName);
  193. }
  194. /**
  195. * Starts a build of this project with the entry point at the given
  196. * target.
  197. */
  198. public void startBuild(String targetName) throws AntException {
  199. // notify FrontEnd that we are starting a build on a project
  200. frontEnd.notifyProjectStart(this);
  201. Target target = getTarget(targetName);
  202. frontEnd.notifyTargetStart(target);
  203. // XXX don't forget to execute dependancies first!
  204. Enumeration enum = target.getTasks().elements();
  205. while (enum.hasMoreElements()) {
  206. Task task = (Task)enum.nextElement();
  207. frontEnd.notifyTaskStart(task);
  208. try {
  209. AbstractTask aTask = taskManager.getTaskInstance(task.getType());
  210. aTask.setProject(this);
  211. aTask.setAttributes(task.getAttributes());
  212. boolean b = aTask.execute();
  213. if (!b) {
  214. String msg = "Task " + task.getType() + " failed";
  215. AntException ae = new AntException(msg);
  216. throw ae;
  217. }
  218. } catch (Exception e) {
  219. AntException ae;
  220. if (!(e instanceof AntException)) {
  221. ae = new AntException(e);
  222. } else {
  223. ae = (AntException)e;
  224. }
  225. ae.setProject(this);
  226. ae.setTarget(target);
  227. ae.setTask(task);
  228. throw ae;
  229. }
  230. frontEnd.notifyTaskEnd(task);
  231. }
  232. // notify frontEnd that we are done
  233. frontEnd.notifyTargetEnd(target);
  234. frontEnd.notifyProjectEnd(this);
  235. }
  236. /**
  237. * Givens a string representation of this object. Useful for debugging.
  238. */
  239. public String toString() {
  240. return "Project name=" + name;
  241. }
  242. }