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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 File buildfile;
  22. /**
  23. * The front end running this.
  24. */
  25. private AntFrontEnd frontEnd;
  26. /**
  27. * Manager of tasks.
  28. */
  29. private TaskManager taskManager = new TaskManager();
  30. /**
  31. *
  32. */
  33. private Project project;
  34. // -----------------------------------------------------------------
  35. // CONSTRUCTORS
  36. // -----------------------------------------------------------------
  37. /**
  38. * Constructs a new Ant instance.
  39. */
  40. public Ant(AntFrontEnd frontEnd) {
  41. this.frontEnd = frontEnd;
  42. setUpTaskPath();
  43. }
  44. // -----------------------------------------------------------------
  45. // PUBLIC METHODS
  46. // -----------------------------------------------------------------
  47. /**
  48. * Sets additional path nodes onto the task lookup path.
  49. */
  50. public void addTaskPathNode(File node) {
  51. taskManager.addTaskPathNode(node);
  52. }
  53. /**
  54. * Builds a target.
  55. */
  56. public void buildTarget(String targetName) throws AntException {
  57. // notify FrontEnd that we are starting a build on a project
  58. frontEnd.notifyProjectStart(project);
  59. Target target = project.getTarget(targetName);
  60. frontEnd.notifyTargetStart(target);
  61. // XXX don't forget to execute dependancies first!
  62. Enumeration enum = target.getTasks().elements();
  63. while (enum.hasMoreElements()) {
  64. Task task = (Task)enum.nextElement();
  65. frontEnd.notifyTaskStart(task);
  66. AbstractTask aTask = taskManager.getTaskInstance(task.getType());
  67. try {
  68. aTask.setProject(project);
  69. aTask.setAttributes(task.getAttributes());
  70. boolean b = aTask.execute();
  71. if (!b) {
  72. throw new AntException("STOP: Task " + task +
  73. " did not succeed");
  74. }
  75. } catch (Exception e) {
  76. // XXX yes yes yes, this shouldn't be a catch all...
  77. throw new AntException("ERR: " + e);
  78. }
  79. frontEnd.notifyTaskEnd(task);
  80. }
  81. // notify frontEnd that we are done
  82. frontEnd.notifyTargetEnd(target);
  83. frontEnd.notifyProjectEnd(project);
  84. }
  85. /**
  86. *
  87. */
  88. public Project getProject() {
  89. return project;
  90. }
  91. /**
  92. * Sets the buildfile to be used. This action triggers a parse of
  93. * the build file and assembles a Project object from it.
  94. */
  95. public void setBuildfile(File file) throws AntException {
  96. buildfile = file;
  97. ProjectBuilder builder = new ProjectBuilder();
  98. project = builder.buildFromFile(file);
  99. project.setAnt(this);
  100. System.out.println("Loaded Project: " + project.getName());
  101. // XXX remove the dump after comfort level is reached
  102. System.out.println("Dump of Project:");
  103. Enumeration enum = project.getTargets();
  104. while (enum.hasMoreElements()) {
  105. Target target = (Target)enum.nextElement();
  106. System.out.println(" Target: " + target.getName());
  107. Enumeration enum2 = target.getTasks().elements();
  108. while (enum2.hasMoreElements()) {
  109. Task task = (Task)enum2.nextElement();
  110. System.out.println(" Task: " + task.getType());
  111. Enumeration enum3 = task.getAttributeNames();
  112. while (enum3.hasMoreElements()) {
  113. String atName = (String)enum3.nextElement();
  114. String atValue = task.getAttribute(atName);
  115. System.out.println(" Att: " + atName + " = " +
  116. atValue);
  117. }
  118. }
  119. }
  120. }
  121. // -----------------------------------------------------------------
  122. // PRIVATE METHODS
  123. // -----------------------------------------------------------------
  124. /**
  125. * Sets up the taskpath based on the currently running operating
  126. * system. In general, the ordering of the taskpath is: user directory,
  127. * system directory, and then installation. This allows users or
  128. * system admins to override or add tasks.
  129. */
  130. private void setUpTaskPath() {
  131. // 1st, add user's home dir.
  132. File f;
  133. String userHome = System.getProperty("user.home");
  134. // generic unix
  135. f = new File(userHome + ".ant", "tasks");
  136. if (f.exists() && f.isDirectory()) {
  137. taskManager.addTaskPathNode(f);
  138. }
  139. // macos x
  140. f = new File(userHome + "/Library/Ant", "Tasks");
  141. if (f.exists() && f.isDirectory()) {
  142. taskManager.addTaskPathNode(f);
  143. }
  144. // windows -- todo
  145. // 2nd, add system local dir.
  146. // generic unix
  147. f = new File("/usr/local/ant/tasks");
  148. if (f.exists() && f.isDirectory()) {
  149. taskManager.addTaskPathNode(f);
  150. }
  151. // macos x
  152. f = new File("/Library/Ant/Tasks");
  153. if (f.exists() && f.isDirectory()) {
  154. taskManager.addTaskPathNode(f);
  155. }
  156. // windows -- todo
  157. // 3rd, add installation local dir.
  158. //System.out.println("BASE: " + this.getClass().getResource("/"));
  159. // XXX ---- not really sure how the best way of getting this info is...
  160. // hafta think about it.
  161. }
  162. }