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.

CallTarget.java 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2000-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.IOException;
  19. import org.apache.tools.ant.Task;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.types.PropertySet;
  22. /**
  23. * Call another target in the same project.
  24. *
  25. * <pre>
  26. * &lt;target name="foo"&gt;
  27. * &lt;antcall target="bar"&gt;
  28. * &lt;param name="property1" value="aaaaa" /&gt;
  29. * &lt;param name="foo" value="baz" /&gt;
  30. * &lt;/antcall&gt;
  31. * &lt;/target&gt;
  32. *
  33. * &lt;target name="bar" depends="init"&gt;
  34. * &lt;echo message="prop is ${property1} ${foo}" /&gt;
  35. * &lt;/target&gt;
  36. * </pre>
  37. *
  38. * <p>This only works as expected if neither property1 nor foo are
  39. * defined in the project itself.
  40. *
  41. *
  42. * @since Ant 1.2
  43. *
  44. * @ant.task name="antcall" category="control"
  45. */
  46. public class CallTarget extends Task {
  47. private Ant callee;
  48. // must match the default value of Ant#inheritAll
  49. private boolean inheritAll = true;
  50. // must match the default value of Ant#inheritRefs
  51. private boolean inheritRefs = false;
  52. private boolean targetSet = false;
  53. /**
  54. * If true, pass all properties to the new Ant project.
  55. * Defaults to true.
  56. * @param inherit <code>boolean</code> flag.
  57. */
  58. public void setInheritAll(boolean inherit) {
  59. inheritAll = inherit;
  60. }
  61. /**
  62. * If true, pass all references to the new Ant project.
  63. * Defaults to false.
  64. * @param inheritRefs <code>boolean</code> flag.
  65. */
  66. public void setInheritRefs(boolean inheritRefs) {
  67. this.inheritRefs = inheritRefs;
  68. }
  69. /**
  70. * Initialize this task by creating new instance of the ant task and
  71. * configuring it by calling its own init method.
  72. */
  73. public void init() {
  74. callee = new Ant(this);
  75. callee.init();
  76. }
  77. /**
  78. * Delegate the work to the ant task instance, after setting it up.
  79. * @throws BuildException on validation failure or if the target didn't
  80. * execute.
  81. */
  82. public void execute() throws BuildException {
  83. if (callee == null) {
  84. init();
  85. }
  86. if (!targetSet) {
  87. throw new BuildException(
  88. "Attribute target or at least one nested target is required.",
  89. getLocation());
  90. }
  91. callee.setAntfile(getProject().getProperty("ant.file"));
  92. callee.setInheritAll(inheritAll);
  93. callee.setInheritRefs(inheritRefs);
  94. callee.execute();
  95. }
  96. /**
  97. * Create a new Property to pass to the invoked target(s).
  98. * @return a <code>Property</code> object.
  99. */
  100. public Property createParam() {
  101. if (callee == null) {
  102. init();
  103. }
  104. return callee.createProperty();
  105. }
  106. /**
  107. * Reference element identifying a data type to carry
  108. * over to the invoked target.
  109. * @param r the specified <code>Ant.Reference</code>.
  110. * @since Ant 1.5
  111. */
  112. public void addReference(Ant.Reference r) {
  113. if (callee == null) {
  114. init();
  115. }
  116. callee.addReference(r);
  117. }
  118. /**
  119. * Set of properties to pass to the new project.
  120. * @param ps the <code>PropertySet</code> to pass.
  121. * @since Ant 1.6
  122. */
  123. public void addPropertyset(PropertySet ps) {
  124. if (callee == null) {
  125. init();
  126. }
  127. callee.addPropertyset(ps);
  128. }
  129. /**
  130. * Set target to execute.
  131. * @param target the name of the target to execute.
  132. */
  133. public void setTarget(String target) {
  134. if (callee == null) {
  135. init();
  136. }
  137. callee.setTarget(target);
  138. targetSet = true;
  139. }
  140. /**
  141. * Add a target to the list of targets to invoke.
  142. * @param t <code>Ant.TargetElement</code> representing the target.
  143. * @since Ant 1.6.3
  144. */
  145. public void addConfiguredTarget(Ant.TargetElement t) {
  146. if (callee == null) {
  147. init();
  148. }
  149. callee.addConfiguredTarget(t);
  150. targetSet = true;
  151. }
  152. /**
  153. * @see Task#handleOutput(String)
  154. *
  155. * @since Ant 1.5
  156. */
  157. public void handleOutput(String output) {
  158. if (callee != null) {
  159. callee.handleOutput(output);
  160. } else {
  161. super.handleOutput(output);
  162. }
  163. }
  164. /**
  165. * @see Task#handleInput(byte[], int, int)
  166. *
  167. * @since Ant 1.6
  168. */
  169. public int handleInput(byte[] buffer, int offset, int length)
  170. throws IOException {
  171. if (callee != null) {
  172. return callee.handleInput(buffer, offset, length);
  173. }
  174. return super.handleInput(buffer, offset, length);
  175. }
  176. /**
  177. * @see Task#handleFlush(String)
  178. *
  179. * @since Ant 1.5.2
  180. */
  181. public void handleFlush(String output) {
  182. if (callee != null) {
  183. callee.handleFlush(output);
  184. } else {
  185. super.handleFlush(output);
  186. }
  187. }
  188. /**
  189. * @see Task#handleErrorOutput(String)
  190. *
  191. * @since Ant 1.5
  192. */
  193. public void handleErrorOutput(String output) {
  194. if (callee != null) {
  195. callee.handleErrorOutput(output);
  196. } else {
  197. super.handleErrorOutput(output);
  198. }
  199. }
  200. /**
  201. * @see Task#handleErrorFlush(String)
  202. *
  203. * @since Ant 1.5.2
  204. */
  205. public void handleErrorFlush(String output) {
  206. if (callee != null) {
  207. callee.handleErrorFlush(output);
  208. } else {
  209. super.handleErrorFlush(output);
  210. }
  211. }
  212. }