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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 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", "Tomcat", 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.taskdefs;
  55. import org.apache.tools.ant.*;
  56. import java.io.*;
  57. import java.util.*;
  58. /**
  59. * Call Ant in a sub-project
  60. *
  61. * <pre>
  62. * <target name="foo" depends="init">
  63. * <ant antfile="build.xml" target="bar" >
  64. * <property name="property1" value="aaaaa" />
  65. * <property name="foo" value="baz" />
  66. * </ant>
  67. * </target>
  68. *
  69. * <target name="bar" depends="init">
  70. * <echo message="prop is ${property1} ${foo}" />
  71. * </target>
  72. * </pre>
  73. *
  74. *
  75. * @author costin@dnt.ro
  76. */
  77. public class Ant extends Task {
  78. private File dir = null;
  79. private String antFile = null;
  80. private String target = null;
  81. private String output = null;
  82. Vector properties=new Vector();
  83. Project p1;
  84. public void init() {
  85. p1 = new Project();
  86. p1.setJavaVersionProperty();
  87. p1.addTaskDefinition("property",
  88. (Class)project.getTaskDefinitions().get("property"));
  89. }
  90. private void reinit() {
  91. init();
  92. for (int i=0; i<properties.size(); i++) {
  93. Property p = (Property) properties.elementAt(i);
  94. Property newP = (Property) p1.createTask("property");
  95. newP.setName(p.getName());
  96. if (p.getValue() != null) {
  97. newP.setValue(p.getValue());
  98. }
  99. if (p.getFile() != null) {
  100. newP.setFile(p.getFile());
  101. }
  102. if (p.getResource() != null) {
  103. newP.setResource(p.getResource());
  104. }
  105. properties.setElementAt(newP, i);
  106. }
  107. }
  108. private void initializeProject() {
  109. Vector listeners = project.getBuildListeners();
  110. for (int i = 0; i < listeners.size(); i++) {
  111. p1.addBuildListener((BuildListener)listeners.elementAt(i));
  112. }
  113. if (output != null) {
  114. try {
  115. PrintStream out = new PrintStream(new FileOutputStream(output));
  116. DefaultLogger logger = new DefaultLogger();
  117. logger.setMessageOutputLevel(Project.MSG_INFO);
  118. logger.setOutputPrintStream(out);
  119. logger.setErrorPrintStream(out);
  120. p1.addBuildListener(logger);
  121. }
  122. catch( IOException ex ) {
  123. log( "Ant: Can't set output to " + output );
  124. }
  125. }
  126. Hashtable taskdefs = project.getTaskDefinitions();
  127. Enumeration et = taskdefs.keys();
  128. while (et.hasMoreElements()) {
  129. String taskName = (String) et.nextElement();
  130. Class taskClass = (Class) taskdefs.get(taskName);
  131. p1.addTaskDefinition(taskName, taskClass);
  132. }
  133. Hashtable typedefs = project.getDataTypeDefinitions();
  134. Enumeration e = typedefs.keys();
  135. while (e.hasMoreElements()) {
  136. String typeName = (String) e.nextElement();
  137. Class typeClass = (Class) typedefs.get(typeName);
  138. p1.addDataTypeDefinition(typeName, typeClass);
  139. }
  140. // set user-define properties
  141. Hashtable prop1 = project.getProperties();
  142. e = prop1.keys();
  143. while (e.hasMoreElements()) {
  144. String arg = (String) e.nextElement();
  145. String value = (String) prop1.get(arg);
  146. p1.setProperty(arg, value);
  147. }
  148. }
  149. /**
  150. * Do the execution.
  151. */
  152. public void execute() throws BuildException {
  153. try {
  154. if (p1 == null) {
  155. reinit();
  156. }
  157. if(dir == null)
  158. dir = project.getBaseDir();
  159. initializeProject();
  160. p1.setBaseDir(dir);
  161. p1.setUserProperty("basedir" , dir.getAbsolutePath());
  162. // Override with local-defined properties
  163. Enumeration e = properties.elements();
  164. while (e.hasMoreElements()) {
  165. Property p=(Property) e.nextElement();
  166. p.execute();
  167. }
  168. if (antFile == null)
  169. antFile = "build.xml";
  170. File file = new File(antFile);
  171. if (!file.isAbsolute()) {
  172. antFile = (new File(dir, antFile)).getAbsolutePath();
  173. file = (new File(antFile)) ;
  174. if( ! file.isFile() ) {
  175. throw new BuildException("Build file " + file + " not found.");
  176. }
  177. }
  178. p1.setUserProperty( "ant.file" , antFile );
  179. ProjectHelper.configureProject(p1, new File(antFile));
  180. if (target == null) {
  181. target = p1.getDefaultTarget();
  182. }
  183. // Are we trying to call the target in which we are defined?
  184. if (p1.getBaseDir().equals(project.getBaseDir()) &&
  185. p1.getProperty("ant.file").equals(project.getProperty("ant.file")) &&
  186. target.equals(this.getOwningTarget().getName())) {
  187. throw new BuildException("ant task calling its own parent target");
  188. }
  189. p1.executeTarget(target);
  190. } finally {
  191. // help the gc
  192. p1 = null;
  193. }
  194. }
  195. public void setDir(File d) {
  196. this.dir = d;
  197. }
  198. public void setAntfile(String s) {
  199. this.antFile = s;
  200. }
  201. public void setTarget(String s) {
  202. this.target = s;
  203. }
  204. public void setOutput(String s) {
  205. this.output = s;
  206. }
  207. public Property createProperty() {
  208. if (p1 == null) {
  209. reinit();
  210. }
  211. Property p=(Property)p1.createTask("property");
  212. p.setUserProperty(true);
  213. properties.addElement( p );
  214. return p;
  215. }
  216. }