1 | /*
|
---|
2 | * Copyright 2000-2004 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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
21 |
|
---|
22 | import org.apache.tools.ant.Task;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.types.PropertySet;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Call another target in the same project.
|
---|
28 | *
|
---|
29 | * <pre>
|
---|
30 | * <target name="foo">
|
---|
31 | * <antcall target="bar">
|
---|
32 | * <param name="property1" value="aaaaa" />
|
---|
33 | * <param name="foo" value="baz" />
|
---|
34 | * </antcall>
|
---|
35 | * </target>
|
---|
36 | *
|
---|
37 | * <target name="bar" depends="init">
|
---|
38 | * <echo message="prop is ${property1} ${foo}" />
|
---|
39 | * </target>
|
---|
40 | * </pre>
|
---|
41 | *
|
---|
42 | * <p>This only works as expected if neither property1 nor foo are
|
---|
43 | * defined in the project itself.
|
---|
44 | *
|
---|
45 | *
|
---|
46 | * @since Ant 1.2
|
---|
47 | *
|
---|
48 | * @ant.task name="antcall" category="control"
|
---|
49 | */
|
---|
50 | public class CallTarget extends Task {
|
---|
51 |
|
---|
52 | private Ant callee;
|
---|
53 | // must match the default value of Ant#inheritAll
|
---|
54 | private boolean inheritAll = true;
|
---|
55 | // must match the default value of Ant#inheritRefs
|
---|
56 | private boolean inheritRefs = false;
|
---|
57 |
|
---|
58 | private boolean targetSet = false;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * If true, pass all properties to the new Ant project.
|
---|
62 | * Defaults to true.
|
---|
63 | * @param inherit <code>boolean</code> flag.
|
---|
64 | */
|
---|
65 | public void setInheritAll(boolean inherit) {
|
---|
66 | inheritAll = inherit;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * If true, pass all references to the new Ant project.
|
---|
71 | * Defaults to false.
|
---|
72 | * @param inheritRefs <code>boolean</code> flag.
|
---|
73 | */
|
---|
74 | public void setInheritRefs(boolean inheritRefs) {
|
---|
75 | this.inheritRefs = inheritRefs;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Initialize this task by creating new instance of the ant task and
|
---|
80 | * configuring it by calling its own init method.
|
---|
81 | */
|
---|
82 | public void init() {
|
---|
83 | callee = (Ant) getProject().createTask("ant");
|
---|
84 | callee.setOwningTarget(getOwningTarget());
|
---|
85 | callee.setTaskName(getTaskName());
|
---|
86 | callee.setLocation(getLocation());
|
---|
87 | callee.init();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Delegate the work to the ant task instance, after setting it up.
|
---|
92 | * @throws BuildException on validation failure or if the target didn't
|
---|
93 | * execute.
|
---|
94 | */
|
---|
95 | public void execute() throws BuildException {
|
---|
96 | if (callee == null) {
|
---|
97 | init();
|
---|
98 | }
|
---|
99 | if (!targetSet) {
|
---|
100 | throw new BuildException(
|
---|
101 | "Attribute target or at least one nested target is required.",
|
---|
102 | getLocation());
|
---|
103 | }
|
---|
104 | callee.setAntfile(getProject().getProperty("ant.file"));
|
---|
105 | callee.setInheritAll(inheritAll);
|
---|
106 | callee.setInheritRefs(inheritRefs);
|
---|
107 | callee.execute();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Create a new Property to pass to the invoked target(s).
|
---|
112 | * @return a <code>Property</code> object.
|
---|
113 | */
|
---|
114 | public Property createParam() {
|
---|
115 | if (callee == null) {
|
---|
116 | init();
|
---|
117 | }
|
---|
118 | return callee.createProperty();
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Reference element identifying a data type to carry
|
---|
123 | * over to the invoked target.
|
---|
124 | * @param r the specified <code>Ant.Reference</code>.
|
---|
125 | * @since Ant 1.5
|
---|
126 | */
|
---|
127 | public void addReference(Ant.Reference r) {
|
---|
128 | if (callee == null) {
|
---|
129 | init();
|
---|
130 | }
|
---|
131 | callee.addReference(r);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Set of properties to pass to the new project.
|
---|
136 | * @param ps the <code>PropertySet</code> to pass.
|
---|
137 | * @since Ant 1.6
|
---|
138 | */
|
---|
139 | public void addPropertyset(PropertySet ps) {
|
---|
140 | if (callee == null) {
|
---|
141 | init();
|
---|
142 | }
|
---|
143 | callee.addPropertyset(ps);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Set target to execute.
|
---|
148 | * @param target the name of the target to execute.
|
---|
149 | */
|
---|
150 | public void setTarget(String target) {
|
---|
151 | if (callee == null) {
|
---|
152 | init();
|
---|
153 | }
|
---|
154 | callee.setTarget(target);
|
---|
155 | targetSet = true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Add a target to the list of targets to invoke.
|
---|
160 | * @param t <code>Ant.TargetElement</code> representing the target.
|
---|
161 | * @since Ant 1.6.3
|
---|
162 | */
|
---|
163 | public void addConfiguredTarget(Ant.TargetElement t) {
|
---|
164 | if (callee == null) {
|
---|
165 | init();
|
---|
166 | }
|
---|
167 | callee.addConfiguredTarget(t);
|
---|
168 | targetSet = true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * @see Task#handleOutput(String)
|
---|
173 | *
|
---|
174 | * @since Ant 1.5
|
---|
175 | */
|
---|
176 | public void handleOutput(String output) {
|
---|
177 | if (callee != null) {
|
---|
178 | callee.handleOutput(output);
|
---|
179 | } else {
|
---|
180 | super.handleOutput(output);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @see Task#handleInput(byte[], int, int)
|
---|
186 | *
|
---|
187 | * @since Ant 1.6
|
---|
188 | */
|
---|
189 | public int handleInput(byte[] buffer, int offset, int length)
|
---|
190 | throws IOException {
|
---|
191 | if (callee != null) {
|
---|
192 | return callee.handleInput(buffer, offset, length);
|
---|
193 | } else {
|
---|
194 | return super.handleInput(buffer, offset, length);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * @see Task#handleFlush(String)
|
---|
200 | *
|
---|
201 | * @since Ant 1.5.2
|
---|
202 | */
|
---|
203 | public void handleFlush(String output) {
|
---|
204 | if (callee != null) {
|
---|
205 | callee.handleFlush(output);
|
---|
206 | } else {
|
---|
207 | super.handleFlush(output);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * @see Task#handleErrorOutput(String)
|
---|
213 | *
|
---|
214 | * @since Ant 1.5
|
---|
215 | */
|
---|
216 | public void handleErrorOutput(String output) {
|
---|
217 | if (callee != null) {
|
---|
218 | callee.handleErrorOutput(output);
|
---|
219 | } else {
|
---|
220 | super.handleErrorOutput(output);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * @see Task#handleErrorFlush(String)
|
---|
226 | *
|
---|
227 | * @since Ant 1.5.2
|
---|
228 | */
|
---|
229 | public void handleErrorFlush(String output) {
|
---|
230 | if (callee != null) {
|
---|
231 | callee.handleErrorFlush(output);
|
---|
232 | } else {
|
---|
233 | super.handleErrorFlush(output);
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|