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.

Ant.java 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // -------------------------------------------------------------------------------
  2. // Copyright (c)2000 Apache Software Foundation
  3. // -------------------------------------------------------------------------------
  4. package org.apache.ant;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.util.zip.*;
  8. /**
  9. * Central class of Ant. This is the core 'kernel' of ant. Interfaces into
  10. * ant talk to Ant through this class.
  11. *
  12. * @author James Duncan Davidson (duncan@apache.org)
  13. */
  14. public class Ant {
  15. // -----------------------------------------------------------------
  16. // PRIVATE DATA MEMBERS
  17. // -----------------------------------------------------------------
  18. /**
  19. *
  20. */
  21. private Hashtable abstractTaskClasses = new Hashtable();
  22. /**
  23. *
  24. */
  25. private Vector taskPathNodes = new Vector();
  26. /**
  27. *
  28. */
  29. private File buildfile;
  30. /**
  31. *
  32. */
  33. private Project project;
  34. // -----------------------------------------------------------------
  35. // CONSTRUCTORS
  36. // -----------------------------------------------------------------
  37. /**
  38. * Constructs a new Ant instance.
  39. */
  40. public Ant() {
  41. setUpTaskPath();
  42. }
  43. // -----------------------------------------------------------------
  44. // PUBLIC METHODS
  45. // -----------------------------------------------------------------
  46. /**
  47. * Sets additional path nodes onto the task lookup path. These nodes
  48. * take precendence over all previously set path nodes.
  49. */
  50. public void addTaskPathNode(File node) {
  51. taskPathNodes.insertElementAt(node, 0);
  52. }
  53. /**
  54. *
  55. */
  56. public void buildTarget(String targetName) throws AntException {
  57. try {
  58. loadTasks();
  59. } catch (IOException ioe) {
  60. throw new AntException(ioe.getMessage());
  61. }
  62. Target target = project.getTarget(targetName);
  63. // XXX don't forget to execute dependancies first!
  64. Enumeration enum = target.getTasks().elements();
  65. while (enum.hasMoreElements()) {
  66. Task task = (Task)enum.nextElement();
  67. Object o = abstractTaskClasses.get(task.getType());
  68. if (o != null) {
  69. Class c = (Class)o;
  70. try {
  71. AbstractTask aTask = (AbstractTask)c.newInstance();
  72. aTask.setAttributes(task.getAttributes());
  73. aTask.setProject(project);
  74. boolean b = aTask.execute();
  75. if (!b) {
  76. throw new AntException("STOP: Task " + task +
  77. " did not succeed");
  78. }
  79. } catch (Exception e) {
  80. // XXX yes yes yes, this shouldn't be a catch all...
  81. throw new AntException("ERR: " + e);
  82. }
  83. } else {
  84. throw new AntException("Don't have a class for task type: " + task);
  85. }
  86. }
  87. }
  88. /**
  89. *
  90. */
  91. public Project getProject() {
  92. return project;
  93. }
  94. /**
  95. * Sets the buildfile to be used. This action triggers a parse of
  96. * the build file and assembles a Project object from it.
  97. */
  98. public void setBuildfile(File file) throws AntException {
  99. buildfile = file;
  100. ProjectBuilder builder = new ProjectBuilder();
  101. project = builder.buildFromFile(file);
  102. project.setAnt(this);
  103. System.out.println("Loaded Project: " + project.getName());
  104. // XXX remove the dump after comfort level is reached
  105. System.out.println("Dump of Project:");
  106. Enumeration enum = project.getTargets();
  107. while (enum.hasMoreElements()) {
  108. Target target = (Target)enum.nextElement();
  109. System.out.println(" Target: " + target.getName());
  110. Enumeration enum2 = target.getTasks().elements();
  111. while (enum2.hasMoreElements()) {
  112. Task task = (Task)enum2.nextElement();
  113. System.out.println(" Task: " + task.getType());
  114. Enumeration enum3 = task.getAttributeNames();
  115. while (enum3.hasMoreElements()) {
  116. String atName = (String)enum3.nextElement();
  117. String atValue = task.getAttribute(atName);
  118. System.out.println(" Att: " + atName + " = " +
  119. atValue);
  120. }
  121. }
  122. }
  123. }
  124. // -----------------------------------------------------------------
  125. // PRIVATE METHODS
  126. // -----------------------------------------------------------------
  127. /**
  128. * Searches through the taskpath and loads up the taskImpl hashtable
  129. *
  130. * XXX we also need to lookup a taskdef.properties file out of a few
  131. * strategic locations on disk to allow generic classes to be pulled
  132. * from the classpath
  133. */
  134. private void loadTasks() throws IOException {
  135. Enumeration enum = taskPathNodes.elements();
  136. while (enum.hasMoreElements()) {
  137. File dir = (File)enum.nextElement();
  138. String[] files = dir.list();
  139. for (int i = 0; i < files.length; i++) {
  140. if (files[i].endsWith(".jar")) {
  141. File f = new File(dir, files[i]);
  142. ZipFile zf = new ZipFile(f);
  143. ZipEntry ze = zf.getEntry("/taskdef.properties");
  144. if (ze != null) {
  145. InputStream is = zf.getInputStream(ze);
  146. Properties props = new Properties();
  147. props.load(is);
  148. is.close();
  149. //System.out.println("Props: " + props);
  150. String s = props.getProperty("tasks");
  151. StringTokenizer tok = new StringTokenizer(s, ",", false);
  152. while (tok.hasMoreTokens()) {
  153. String taskType = tok.nextToken();
  154. String taskClassName = props.getProperty(taskType +
  155. ".class");
  156. //System.out.println("TASK: " + taskType + " class: " +
  157. // taskClassName);
  158. ClassLoader pcl = this.getClass().getClassLoader();
  159. TaskClassLoader tcl = new TaskClassLoader(pcl, zf);
  160. try {
  161. Class clazz = tcl.findClass(taskClassName);
  162. abstractTaskClasses.put(taskType, clazz);
  163. } catch (ClassNotFoundException cnfe) {
  164. System.out.println(cnfe);
  165. System.out.println(cnfe.getMessage());
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. /**
  174. * Sets up the taskpath based on the currently running operating
  175. * system. In general, the ordering of the taskpath is: user directory,
  176. * system directory, and then installation. This allows users or
  177. * system admins to override or add tasks.
  178. */
  179. private void setUpTaskPath() {
  180. // 1st, add user's home dir.
  181. File f;
  182. String userHome = System.getProperty("user.home");
  183. // generic unix
  184. f = new File(userHome + ".ant", "tasks");
  185. if (f.exists() && f.isDirectory()) {
  186. taskPathNodes.addElement(f);
  187. }
  188. // macos x
  189. f = new File(userHome + "/Library/Ant", "Tasks");
  190. if (f.exists() && f.isDirectory()) {
  191. taskPathNodes.addElement(f);
  192. }
  193. // windows -- todo
  194. // 2nd, add system local dir.
  195. // generic unix
  196. f = new File("/usr/local/ant/tasks");
  197. if (f.exists() && f.isDirectory()) {
  198. taskPathNodes.addElement(f);
  199. }
  200. // macos x
  201. f = new File("/Library/Ant/Tasks");
  202. if (f.exists() && f.isDirectory()) {
  203. taskPathNodes.addElement(f);
  204. }
  205. // windows -- todo
  206. // 3rd, add installation local dir.
  207. //System.out.println("BASE: " + this.getClass().getResource("/"));
  208. // XXX ---- not really sure how the best way of getting this info is...
  209. // hafta think about it.
  210. }
  211. }