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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.FileOutputStream;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.io.PrintStream;
|
---|
24 | import java.lang.reflect.Method;
|
---|
25 | import java.util.Enumeration;
|
---|
26 | import java.util.Hashtable;
|
---|
27 | import java.util.Iterator;
|
---|
28 | import java.util.Vector;
|
---|
29 | import java.util.Set;
|
---|
30 | import java.util.HashSet;
|
---|
31 | import org.apache.tools.ant.BuildException;
|
---|
32 | import org.apache.tools.ant.BuildListener;
|
---|
33 | import org.apache.tools.ant.DefaultLogger;
|
---|
34 | import org.apache.tools.ant.Executor;
|
---|
35 | import org.apache.tools.ant.Project;
|
---|
36 | import org.apache.tools.ant.ProjectComponent;
|
---|
37 | import org.apache.tools.ant.ProjectHelper;
|
---|
38 | import org.apache.tools.ant.Target;
|
---|
39 | import org.apache.tools.ant.Task;
|
---|
40 | import org.apache.tools.ant.helper.SingleCheckExecutor;
|
---|
41 | import org.apache.tools.ant.types.PropertySet;
|
---|
42 | import org.apache.tools.ant.util.FileUtils;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Build a sub-project.
|
---|
46 | *
|
---|
47 | * <pre>
|
---|
48 | * <target name="foo" depends="init">
|
---|
49 | * <ant antfile="build.xml" target="bar" >
|
---|
50 | * <property name="property1" value="aaaaa" />
|
---|
51 | * <property name="foo" value="baz" />
|
---|
52 | * </ant></span>
|
---|
53 | * </target></span>
|
---|
54 | *
|
---|
55 | * <target name="bar" depends="init">
|
---|
56 | * <echo message="prop is ${property1} ${foo}" />
|
---|
57 | * </target>
|
---|
58 | * </pre>
|
---|
59 | *
|
---|
60 | *
|
---|
61 | * @since Ant 1.1
|
---|
62 | *
|
---|
63 | * @ant.task category="control"
|
---|
64 | */
|
---|
65 | public class Ant extends Task {
|
---|
66 |
|
---|
67 | /** the basedir where is executed the build file */
|
---|
68 | private File dir = null;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * the build.xml file (can be absolute) in this case dir will be
|
---|
72 | * ignored
|
---|
73 | */
|
---|
74 | private String antFile = null;
|
---|
75 |
|
---|
76 | /** the output */
|
---|
77 | private String output = null;
|
---|
78 |
|
---|
79 | /** should we inherit properties from the parent ? */
|
---|
80 | private boolean inheritAll = true;
|
---|
81 |
|
---|
82 | /** should we inherit references from the parent ? */
|
---|
83 | private boolean inheritRefs = false;
|
---|
84 |
|
---|
85 | /** the properties to pass to the new project */
|
---|
86 | private Vector properties = new Vector();
|
---|
87 |
|
---|
88 | /** the references to pass to the new project */
|
---|
89 | private Vector references = new Vector();
|
---|
90 |
|
---|
91 | /** the temporary project created to run the build file */
|
---|
92 | private Project newProject;
|
---|
93 |
|
---|
94 | /** The stream to which output is to be written. */
|
---|
95 | private PrintStream out = null;
|
---|
96 |
|
---|
97 | /** the sets of properties to pass to the new project */
|
---|
98 | private Vector propertySets = new Vector();
|
---|
99 |
|
---|
100 | /** the targets to call on the new project */
|
---|
101 | private Vector targets = new Vector();
|
---|
102 |
|
---|
103 | /** whether the target attribute was specified **/
|
---|
104 | private boolean targetAttributeSet = false;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * If true, pass all properties to the new Ant project.
|
---|
108 | * Defaults to true.
|
---|
109 | * @param value if true pass all properties to the new Ant project.
|
---|
110 | */
|
---|
111 | public void setInheritAll(boolean value) {
|
---|
112 | inheritAll = value;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * If true, pass all references to the new Ant project.
|
---|
117 | * Defaults to false.
|
---|
118 | * @param value if true, pass all references to the new Ant project
|
---|
119 | */
|
---|
120 | public void setInheritRefs(boolean value) {
|
---|
121 | inheritRefs = value;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Creates a Project instance for the project to call.
|
---|
126 | */
|
---|
127 | public void init() {
|
---|
128 | newProject = new Project();
|
---|
129 | newProject.setDefaultInputStream(getProject().getDefaultInputStream());
|
---|
130 | newProject.setJavaVersionProperty();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Called in execute or createProperty if newProject is null.
|
---|
135 | *
|
---|
136 | * <p>This can happen if the same instance of this task is run
|
---|
137 | * twice as newProject is set to null at the end of execute (to
|
---|
138 | * save memory and help the GC).</p>
|
---|
139 | * <p>calls init() again</p>
|
---|
140 | *
|
---|
141 | */
|
---|
142 | private void reinit() {
|
---|
143 | init();
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Attaches the build listeners of the current project to the new
|
---|
148 | * project, configures a possible logfile, transfers task and
|
---|
149 | * data-type definitions, transfers properties (either all or just
|
---|
150 | * the ones specified as user properties to the current project,
|
---|
151 | * depending on inheritall), transfers the input handler.
|
---|
152 | */
|
---|
153 | private void initializeProject() {
|
---|
154 | newProject.setInputHandler(getProject().getInputHandler());
|
---|
155 |
|
---|
156 | Iterator iter = getBuildListeners();
|
---|
157 | while (iter.hasNext()) {
|
---|
158 | newProject.addBuildListener((BuildListener) iter.next());
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (output != null) {
|
---|
162 | File outfile = null;
|
---|
163 | if (dir != null) {
|
---|
164 | outfile = FileUtils.newFileUtils().resolveFile(dir, output);
|
---|
165 | } else {
|
---|
166 | outfile = getProject().resolveFile(output);
|
---|
167 | }
|
---|
168 | try {
|
---|
169 | out = new PrintStream(new FileOutputStream(outfile));
|
---|
170 | DefaultLogger logger = new DefaultLogger();
|
---|
171 | logger.setMessageOutputLevel(Project.MSG_INFO);
|
---|
172 | logger.setOutputPrintStream(out);
|
---|
173 | logger.setErrorPrintStream(out);
|
---|
174 | newProject.addBuildListener(logger);
|
---|
175 | } catch (IOException ex) {
|
---|
176 | log("Ant: Can't set output to " + output);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | getProject().initSubProject(newProject);
|
---|
181 |
|
---|
182 | // set user-defined properties
|
---|
183 | getProject().copyUserProperties(newProject);
|
---|
184 |
|
---|
185 | if (!inheritAll) {
|
---|
186 | // set Java built-in properties separately,
|
---|
187 | // b/c we won't inherit them.
|
---|
188 | newProject.setSystemProperties();
|
---|
189 |
|
---|
190 | } else {
|
---|
191 | // set all properties from calling project
|
---|
192 | addAlmostAll(getProject().getProperties());
|
---|
193 | }
|
---|
194 |
|
---|
195 | Enumeration e = propertySets.elements();
|
---|
196 | while (e.hasMoreElements()) {
|
---|
197 | PropertySet ps = (PropertySet) e.nextElement();
|
---|
198 | addAlmostAll(ps.getProperties());
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * @see Task#handleOutput(String)
|
---|
204 | *
|
---|
205 | * @since Ant 1.5
|
---|
206 | */
|
---|
207 | public void handleOutput(String output) {
|
---|
208 | if (newProject != null) {
|
---|
209 | newProject.demuxOutput(output, false);
|
---|
210 | } else {
|
---|
211 | super.handleOutput(output);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * @see Task#handleInput(byte[], int, int)
|
---|
217 | *
|
---|
218 | * @since Ant 1.6
|
---|
219 | */
|
---|
220 | public int handleInput(byte[] buffer, int offset, int length)
|
---|
221 | throws IOException {
|
---|
222 | if (newProject != null) {
|
---|
223 | return newProject.demuxInput(buffer, offset, length);
|
---|
224 | } else {
|
---|
225 | return super.handleInput(buffer, offset, length);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * @see Task#handleFlush(String)
|
---|
231 | *
|
---|
232 | * @since Ant 1.5.2
|
---|
233 | */
|
---|
234 | public void handleFlush(String output) {
|
---|
235 | if (newProject != null) {
|
---|
236 | newProject.demuxFlush(output, false);
|
---|
237 | } else {
|
---|
238 | super.handleFlush(output);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * @see Task#handleErrorOutput(String)
|
---|
244 | *
|
---|
245 | * @since Ant 1.5
|
---|
246 | */
|
---|
247 | public void handleErrorOutput(String output) {
|
---|
248 | if (newProject != null) {
|
---|
249 | newProject.demuxOutput(output, true);
|
---|
250 | } else {
|
---|
251 | super.handleErrorOutput(output);
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * @see Task#handleErrorFlush(String)
|
---|
257 | *
|
---|
258 | * @since Ant 1.5.2
|
---|
259 | */
|
---|
260 | public void handleErrorFlush(String output) {
|
---|
261 | if (newProject != null) {
|
---|
262 | newProject.demuxFlush(output, true);
|
---|
263 | } else {
|
---|
264 | super.handleErrorFlush(output);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Do the execution.
|
---|
270 | * @throws BuildException if a target tries to call itself;
|
---|
271 | * probably also if a BuildException is thrown by the new project.
|
---|
272 | */
|
---|
273 | public void execute(String targetName, String targetAddress) throws BuildException {
|
---|
274 | File savedDir = dir;
|
---|
275 | String savedAntFile = antFile;
|
---|
276 | Vector locals = new Vector(targets);
|
---|
277 | try {
|
---|
278 | if (newProject == null) {
|
---|
279 | reinit();
|
---|
280 | }
|
---|
281 |
|
---|
282 | if ((dir == null) && (inheritAll)) {
|
---|
283 | dir = getProject().getBaseDir();
|
---|
284 | }
|
---|
285 |
|
---|
286 | initializeProject();
|
---|
287 |
|
---|
288 | if (dir != null) {
|
---|
289 | newProject.setBaseDir(dir);
|
---|
290 | if (savedDir != null) {
|
---|
291 | // has been set explicitly
|
---|
292 | newProject.setInheritedProperty("basedir" ,
|
---|
293 | dir.getAbsolutePath());
|
---|
294 | }
|
---|
295 | } else {
|
---|
296 | dir = getProject().getBaseDir();
|
---|
297 | }
|
---|
298 |
|
---|
299 | overrideProperties();
|
---|
300 |
|
---|
301 | if (antFile == null) {
|
---|
302 | antFile = "build.xml";
|
---|
303 | }
|
---|
304 |
|
---|
305 | File file = FileUtils.newFileUtils().resolveFile(dir, antFile);
|
---|
306 | antFile = file.getAbsolutePath();
|
---|
307 |
|
---|
308 | log("calling target(s) "
|
---|
309 | + ((locals.size() > 0) ? locals.toString() : "[default]")
|
---|
310 | + " in build file " + antFile, Project.MSG_VERBOSE);
|
---|
311 | newProject.setUserProperty("ant.file" , antFile);
|
---|
312 |
|
---|
313 | String thisAntFile = getProject().getProperty("ant.file");
|
---|
314 | // Are we trying to call the target in which we are defined (or
|
---|
315 | // the build file if this is a top level task)?
|
---|
316 | if (thisAntFile != null
|
---|
317 | && newProject.resolveFile(newProject.getProperty("ant.file"))
|
---|
318 | .equals(getProject().resolveFile(thisAntFile))
|
---|
319 | && getOwningTarget() != null) {
|
---|
320 |
|
---|
321 | if (getOwningTarget().getName().equals("")) {
|
---|
322 | if (getTaskName().equals("antcall")) {
|
---|
323 | throw new BuildException("antcall must not be used at"
|
---|
324 | + " the top level.");
|
---|
325 | } else {
|
---|
326 | throw new BuildException(getTaskName() + " task at the"
|
---|
327 | + " top level must not invoke"
|
---|
328 | + " its own build file.");
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | try {
|
---|
334 | ProjectHelper.configureProject(newProject, new File(antFile));
|
---|
335 | } catch (BuildException ex) {
|
---|
336 | throw ProjectHelper.addLocationToBuildException(
|
---|
337 | ex, getLocation());
|
---|
338 | }
|
---|
339 |
|
---|
340 | if (locals.size() == 0) {
|
---|
341 | String defaultTarget = newProject.getDefaultTarget();
|
---|
342 | if (defaultTarget != null) {
|
---|
343 | locals.add(defaultTarget);
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | if (newProject.getProperty("ant.file")
|
---|
348 | .equals(getProject().getProperty("ant.file"))
|
---|
349 | && getOwningTarget() != null) {
|
---|
350 |
|
---|
351 | String owningTargetName = getOwningTarget().getName();
|
---|
352 |
|
---|
353 | if (locals.contains(owningTargetName)) {
|
---|
354 | throw new BuildException(getTaskName() + " task calling "
|
---|
355 | + "its own parent target.");
|
---|
356 | } else {
|
---|
357 | boolean circular = false;
|
---|
358 | for (Iterator it = locals.iterator(); !circular && it.hasNext();) {
|
---|
359 | Target other = (Target) (getProject().getTargets().get(
|
---|
360 | (String) (it.next())));
|
---|
361 | circular |= (other != null
|
---|
362 | && other.dependsOn(owningTargetName));
|
---|
363 | }
|
---|
364 | if (circular) {
|
---|
365 | throw new BuildException(getTaskName()
|
---|
366 | + " task calling a target"
|
---|
367 | + " that depends on"
|
---|
368 | + " its parent target \'"
|
---|
369 | + owningTargetName
|
---|
370 | + "\'.");
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | addReferences();
|
---|
376 |
|
---|
377 | if (locals.size() > 0 && !(locals.size() == 1 && locals.get(0) == "")) {
|
---|
378 | Throwable t = null;
|
---|
379 | try {
|
---|
380 | log("Entering " + antFile + "...", Project.MSG_VERBOSE);
|
---|
381 | //set the address on the target
|
---|
382 | Target calleeTarget = (Target)newProject.getTargets().get(targetName);
|
---|
383 | if( false ) {
|
---|
384 | throw new BuildException(
|
---|
385 | "\n targetName " + targetName + "\n calleeTarget: " + calleeTarget,
|
---|
386 | getLocation());
|
---|
387 | }
|
---|
388 | calleeTarget.setAddress(targetAddress);
|
---|
389 |
|
---|
390 | newProject.fireSubBuildStarted();
|
---|
391 | newProject.executeTargets(locals);
|
---|
392 | } catch (BuildException ex) {
|
---|
393 | t = ProjectHelper
|
---|
394 | .addLocationToBuildException(ex, getLocation());
|
---|
395 | throw (BuildException) t;
|
---|
396 | } finally {
|
---|
397 | log("Exiting " + antFile + ".", Project.MSG_VERBOSE);
|
---|
398 | newProject.fireSubBuildFinished(t);
|
---|
399 | }
|
---|
400 | }
|
---|
401 | } finally {
|
---|
402 | // help the gc
|
---|
403 | newProject = null;
|
---|
404 | Enumeration e = properties.elements();
|
---|
405 | while (e.hasMoreElements()) {
|
---|
406 | Property p = (Property) e.nextElement();
|
---|
407 | p.setProject(null);
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (output != null && out != null) {
|
---|
411 | try {
|
---|
412 | out.close();
|
---|
413 | } catch (final Exception ex) {
|
---|
414 | //ignore
|
---|
415 | }
|
---|
416 | }
|
---|
417 | dir = savedDir;
|
---|
418 | antFile = savedAntFile;
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Do the execution.
|
---|
425 | * @throws BuildException if a target tries to call itself;
|
---|
426 | * probably also if a BuildException is thrown by the new project.
|
---|
427 | */
|
---|
428 | public void execute() throws BuildException {
|
---|
429 | File savedDir = dir;
|
---|
430 | String savedAntFile = antFile;
|
---|
431 | Vector locals = new Vector(targets);
|
---|
432 | try {
|
---|
433 | if (newProject == null) {
|
---|
434 | reinit();
|
---|
435 | }
|
---|
436 |
|
---|
437 | if ((dir == null) && (inheritAll)) {
|
---|
438 | dir = getProject().getBaseDir();
|
---|
439 | }
|
---|
440 |
|
---|
441 | initializeProject();
|
---|
442 |
|
---|
443 | if (dir != null) {
|
---|
444 | newProject.setBaseDir(dir);
|
---|
445 | if (savedDir != null) {
|
---|
446 | // has been set explicitly
|
---|
447 | newProject.setInheritedProperty("basedir" ,
|
---|
448 | dir.getAbsolutePath());
|
---|
449 | }
|
---|
450 | } else {
|
---|
451 | dir = getProject().getBaseDir();
|
---|
452 | }
|
---|
453 |
|
---|
454 | overrideProperties();
|
---|
455 |
|
---|
456 | if (antFile == null) {
|
---|
457 | antFile = "build.xml";
|
---|
458 | }
|
---|
459 |
|
---|
460 | File file = FileUtils.newFileUtils().resolveFile(dir, antFile);
|
---|
461 | antFile = file.getAbsolutePath();
|
---|
462 |
|
---|
463 | log("calling target(s) "
|
---|
464 | + ((locals.size() > 0) ? locals.toString() : "[default]")
|
---|
465 | + " in build file " + antFile, Project.MSG_VERBOSE);
|
---|
466 | newProject.setUserProperty("ant.file" , antFile);
|
---|
467 |
|
---|
468 | String thisAntFile = getProject().getProperty("ant.file");
|
---|
469 | // Are we trying to call the target in which we are defined (or
|
---|
470 | // the build file if this is a top level task)?
|
---|
471 | if (thisAntFile != null
|
---|
472 | && newProject.resolveFile(newProject.getProperty("ant.file"))
|
---|
473 | .equals(getProject().resolveFile(thisAntFile))
|
---|
474 | && getOwningTarget() != null) {
|
---|
475 |
|
---|
476 | if (getOwningTarget().getName().equals("")) {
|
---|
477 | if (getTaskName().equals("antcall")) {
|
---|
478 | throw new BuildException("antcall must not be used at"
|
---|
479 | + " the top level.");
|
---|
480 | } else {
|
---|
481 | throw new BuildException(getTaskName() + " task at the"
|
---|
482 | + " top level must not invoke"
|
---|
483 | + " its own build file.");
|
---|
484 | }
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | try {
|
---|
489 | ProjectHelper.configureProject(newProject, new File(antFile));
|
---|
490 | } catch (BuildException ex) {
|
---|
491 | throw ProjectHelper.addLocationToBuildException(
|
---|
492 | ex, getLocation());
|
---|
493 | }
|
---|
494 |
|
---|
495 | if (locals.size() == 0) {
|
---|
496 | String defaultTarget = newProject.getDefaultTarget();
|
---|
497 | if (defaultTarget != null) {
|
---|
498 | locals.add(defaultTarget);
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (newProject.getProperty("ant.file")
|
---|
503 | .equals(getProject().getProperty("ant.file"))
|
---|
504 | && getOwningTarget() != null) {
|
---|
505 |
|
---|
506 | String owningTargetName = getOwningTarget().getName();
|
---|
507 |
|
---|
508 | if (locals.contains(owningTargetName)) {
|
---|
509 | throw new BuildException(getTaskName() + " task calling "
|
---|
510 | + "its own parent target.");
|
---|
511 | } else {
|
---|
512 | boolean circular = false;
|
---|
513 | for (Iterator it = locals.iterator(); !circular && it.hasNext();) {
|
---|
514 | Target other = (Target) (getProject().getTargets().get(
|
---|
515 | (String) (it.next())));
|
---|
516 | circular |= (other != null
|
---|
517 | && other.dependsOn(owningTargetName));
|
---|
518 | }
|
---|
519 | if (circular) {
|
---|
520 | throw new BuildException(getTaskName()
|
---|
521 | + " task calling a target"
|
---|
522 | + " that depends on"
|
---|
523 | + " its parent target \'"
|
---|
524 | + owningTargetName
|
---|
525 | + "\'.");
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | addReferences();
|
---|
531 |
|
---|
532 | if (locals.size() > 0 && !(locals.size() == 1 && locals.get(0) == "")) {
|
---|
533 | Throwable t = null;
|
---|
534 | try {
|
---|
535 | log("Entering " + antFile + "...", Project.MSG_VERBOSE);
|
---|
536 | newProject.fireSubBuildStarted();
|
---|
537 | newProject.executeTargets(locals);
|
---|
538 | } catch (BuildException ex) {
|
---|
539 | t = ProjectHelper
|
---|
540 | .addLocationToBuildException(ex, getLocation());
|
---|
541 | throw (BuildException) t;
|
---|
542 | } finally {
|
---|
543 | log("Exiting " + antFile + ".", Project.MSG_VERBOSE);
|
---|
544 | newProject.fireSubBuildFinished(t);
|
---|
545 | }
|
---|
546 | }
|
---|
547 | } finally {
|
---|
548 | // help the gc
|
---|
549 | newProject = null;
|
---|
550 | Enumeration e = properties.elements();
|
---|
551 | while (e.hasMoreElements()) {
|
---|
552 | Property p = (Property) e.nextElement();
|
---|
553 | p.setProject(null);
|
---|
554 | }
|
---|
555 |
|
---|
556 | if (output != null && out != null) {
|
---|
557 | try {
|
---|
558 | out.close();
|
---|
559 | } catch (final Exception ex) {
|
---|
560 | //ignore
|
---|
561 | }
|
---|
562 | }
|
---|
563 | dir = savedDir;
|
---|
564 | antFile = savedAntFile;
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * Override the properties in the new project with the one
|
---|
570 | * explicitly defined as nested elements here.
|
---|
571 | * @throws BuildException under unknown circumstances.
|
---|
572 | */
|
---|
573 | private void overrideProperties() throws BuildException {
|
---|
574 | // remove duplicate properties - last property wins
|
---|
575 | // Needed for backward compatibility
|
---|
576 | Set set = new HashSet();
|
---|
577 | for (int i = properties.size() - 1; i >= 0; --i) {
|
---|
578 | Property p = (Property) properties.get(i);
|
---|
579 | if (p.getName() != null && !p.getName().equals("")) {
|
---|
580 | if (set.contains(p.getName())) {
|
---|
581 | properties.remove(i);
|
---|
582 | } else {
|
---|
583 | set.add(p.getName());
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 | Enumeration e = properties.elements();
|
---|
588 | while (e.hasMoreElements()) {
|
---|
589 | Property p = (Property) e.nextElement();
|
---|
590 | p.setProject(newProject);
|
---|
591 | p.execute();
|
---|
592 | }
|
---|
593 | getProject().copyInheritedProperties(newProject);
|
---|
594 | }
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * Add the references explicitly defined as nested elements to the
|
---|
598 | * new project. Also copy over all references that don't override
|
---|
599 | * existing references in the new project if inheritrefs has been
|
---|
600 | * requested.
|
---|
601 | * @throws BuildException if a reference does not have a refid.
|
---|
602 | */
|
---|
603 | private void addReferences() throws BuildException {
|
---|
604 | Hashtable thisReferences
|
---|
605 | = (Hashtable) getProject().getReferences().clone();
|
---|
606 | Hashtable newReferences = newProject.getReferences();
|
---|
607 | Enumeration e;
|
---|
608 | if (references.size() > 0) {
|
---|
609 | for (e = references.elements(); e.hasMoreElements();) {
|
---|
610 | Reference ref = (Reference) e.nextElement();
|
---|
611 | String refid = ref.getRefId();
|
---|
612 | if (refid == null) {
|
---|
613 | throw new BuildException("the refid attribute is required"
|
---|
614 | + " for reference elements");
|
---|
615 | }
|
---|
616 | if (!thisReferences.containsKey(refid)) {
|
---|
617 | log("Parent project doesn't contain any reference '"
|
---|
618 | + refid + "'",
|
---|
619 | Project.MSG_WARN);
|
---|
620 | continue;
|
---|
621 | }
|
---|
622 |
|
---|
623 | thisReferences.remove(refid);
|
---|
624 | String toRefid = ref.getToRefid();
|
---|
625 | if (toRefid == null) {
|
---|
626 | toRefid = refid;
|
---|
627 | }
|
---|
628 | copyReference(refid, toRefid);
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | // Now add all references that are not defined in the
|
---|
633 | // subproject, if inheritRefs is true
|
---|
634 | if (inheritRefs) {
|
---|
635 | for (e = thisReferences.keys(); e.hasMoreElements();) {
|
---|
636 | String key = (String) e.nextElement();
|
---|
637 | if (newReferences.containsKey(key)) {
|
---|
638 | continue;
|
---|
639 | }
|
---|
640 | copyReference(key, key);
|
---|
641 | }
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | /**
|
---|
646 | * Try to clone and reconfigure the object referenced by oldkey in
|
---|
647 | * the parent project and add it to the new project with the key newkey.
|
---|
648 | *
|
---|
649 | * <p>If we cannot clone it, copy the referenced object itself and
|
---|
650 | * keep our fingers crossed.</p>
|
---|
651 | * @param oldKey the reference id in the current project.
|
---|
652 | * @param newKey the reference id in the new project.
|
---|
653 | */
|
---|
654 | private void copyReference(String oldKey, String newKey) {
|
---|
655 | Object orig = getProject().getReference(oldKey);
|
---|
656 | if (orig == null) {
|
---|
657 | log("No object referenced by " + oldKey + ". Can't copy to "
|
---|
658 | + newKey,
|
---|
659 | Project.MSG_WARN);
|
---|
660 | return;
|
---|
661 | }
|
---|
662 |
|
---|
663 | Class c = orig.getClass();
|
---|
664 | Object copy = orig;
|
---|
665 | try {
|
---|
666 | Method cloneM = c.getMethod("clone", new Class[0]);
|
---|
667 | if (cloneM != null) {
|
---|
668 | copy = cloneM.invoke(orig, new Object[0]);
|
---|
669 | log("Adding clone of reference " + oldKey, Project.MSG_DEBUG);
|
---|
670 | }
|
---|
671 | } catch (Exception e) {
|
---|
672 | // not Clonable
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 | if (copy instanceof ProjectComponent) {
|
---|
677 | ((ProjectComponent) copy).setProject(newProject);
|
---|
678 | } else {
|
---|
679 | try {
|
---|
680 | Method setProjectM =
|
---|
681 | c.getMethod("setProject", new Class[] {Project.class});
|
---|
682 | if (setProjectM != null) {
|
---|
683 | setProjectM.invoke(copy, new Object[] {newProject});
|
---|
684 | }
|
---|
685 | } catch (NoSuchMethodException e) {
|
---|
686 | // ignore this if the class being referenced does not have
|
---|
687 | // a set project method.
|
---|
688 | } catch (Exception e2) {
|
---|
689 | String msg = "Error setting new project instance for "
|
---|
690 | + "reference with id " + oldKey;
|
---|
691 | throw new BuildException(msg, e2, getLocation());
|
---|
692 | }
|
---|
693 | }
|
---|
694 | newProject.addReference(newKey, copy);
|
---|
695 | }
|
---|
696 |
|
---|
697 | /**
|
---|
698 | * Copies all properties from the given table to the new project -
|
---|
699 | * omitting those that have already been set in the new project as
|
---|
700 | * well as properties named basedir or ant.file.
|
---|
701 | * @param props properties <code>Hashtable</code> to copy to the new project.
|
---|
702 | * @since Ant 1.6
|
---|
703 | */
|
---|
704 | private void addAlmostAll(Hashtable props) {
|
---|
705 | Enumeration e = props.keys();
|
---|
706 | while (e.hasMoreElements()) {
|
---|
707 | String key = e.nextElement().toString();
|
---|
708 | if ("basedir".equals(key) || "ant.file".equals(key)) {
|
---|
709 | // basedir and ant.file get special treatment in execute()
|
---|
710 | continue;
|
---|
711 | }
|
---|
712 |
|
---|
713 | String value = props.get(key).toString();
|
---|
714 | // don't re-set user properties, avoid the warning message
|
---|
715 | if (newProject.getProperty(key) == null) {
|
---|
716 | // no user property
|
---|
717 | newProject.setNewProperty(key, value);
|
---|
718 | }
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * The directory to use as a base directory for the new Ant project.
|
---|
724 | * Defaults to the current project's basedir, unless inheritall
|
---|
725 | * has been set to false, in which case it doesn't have a default
|
---|
726 | * value. This will override the basedir setting of the called project.
|
---|
727 | * @param d new directory as <code>File</code>.
|
---|
728 | */
|
---|
729 | public void setDir(File d) {
|
---|
730 | this.dir = d;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * The build file to use. Defaults to "build.xml". This file is expected
|
---|
735 | * to be a filename relative to the dir attribute given.
|
---|
736 | * @param s the <code>String</code> build file name.
|
---|
737 | */
|
---|
738 | public void setAntfile(String s) {
|
---|
739 | // @note: it is a string and not a file to handle relative/absolute
|
---|
740 | // otherwise a relative file will be resolved based on the current
|
---|
741 | // basedir.
|
---|
742 | this.antFile = s;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * The target of the new Ant project to execute.
|
---|
747 | * Defaults to the new project's default target.
|
---|
748 | * @param s the name of the target to invoke.
|
---|
749 | */
|
---|
750 | public void setTarget(String s) {
|
---|
751 | if (s.equals("")) {
|
---|
752 | throw new BuildException("target attribute must not be empty");
|
---|
753 | }
|
---|
754 | targets.add(s);
|
---|
755 | targetAttributeSet = true;
|
---|
756 | }
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * Set the filename to write the output to. This is relative to the value
|
---|
760 | * of the dir attribute if it has been set or to the base directory of the
|
---|
761 | * current project otherwise.
|
---|
762 | * @param s the name of the file to which the output should go.
|
---|
763 | */
|
---|
764 | public void setOutput(String s) {
|
---|
765 | this.output = s;
|
---|
766 | }
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * Property to pass to the new project.
|
---|
770 | * The property is passed as a 'user property'.
|
---|
771 | * @return the created <code>Property</code> object.
|
---|
772 | */
|
---|
773 | public Property createProperty() {
|
---|
774 | if (newProject == null) {
|
---|
775 | reinit();
|
---|
776 | }
|
---|
777 | Property p = new Property(true, getProject());
|
---|
778 | p.setProject(newProject);
|
---|
779 | p.setTaskName("property");
|
---|
780 | properties.addElement(p);
|
---|
781 | return p;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * Add a Reference element identifying a data type to carry
|
---|
786 | * over to the new project.
|
---|
787 | * @param r <code>Reference</code> to add.
|
---|
788 | */
|
---|
789 | public void addReference(Reference r) {
|
---|
790 | references.addElement(r);
|
---|
791 | }
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Add a target to this Ant invocation.
|
---|
795 | * @param t the <CODE>TargetElement</CODE> to add.
|
---|
796 | * @since Ant 1.6.3
|
---|
797 | */
|
---|
798 | public void addConfiguredTarget(TargetElement t) {
|
---|
799 | if (targetAttributeSet) {
|
---|
800 | throw new BuildException(
|
---|
801 | "nested target is incompatible with the target attribute");
|
---|
802 | }
|
---|
803 | String name = t.getName();
|
---|
804 | if (name.equals("")) {
|
---|
805 | throw new BuildException("target name must not be empty");
|
---|
806 | }
|
---|
807 | targets.add(name);
|
---|
808 | }
|
---|
809 |
|
---|
810 | /**
|
---|
811 | * Add a set of properties to pass to the new project.
|
---|
812 | *
|
---|
813 | * @param ps <code>PropertySet</code> to add.
|
---|
814 | * @since Ant 1.6
|
---|
815 | */
|
---|
816 | public void addPropertyset(PropertySet ps) {
|
---|
817 | propertySets.addElement(ps);
|
---|
818 | }
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * @since Ant 1.6.2
|
---|
822 | */
|
---|
823 | private Iterator getBuildListeners() {
|
---|
824 | return getProject().getBuildListeners().iterator();
|
---|
825 | }
|
---|
826 |
|
---|
827 | /**
|
---|
828 | * Helper class that implements the nested <reference>
|
---|
829 | * element of <ant> and <antcall>.
|
---|
830 | */
|
---|
831 | public static class Reference
|
---|
832 | extends org.apache.tools.ant.types.Reference {
|
---|
833 |
|
---|
834 | /** Creates a reference to be configured by Ant. */
|
---|
835 | public Reference() {
|
---|
836 | super();
|
---|
837 | }
|
---|
838 |
|
---|
839 | private String targetid = null;
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * Set the id that this reference to be stored under in the
|
---|
843 | * new project.
|
---|
844 | *
|
---|
845 | * @param targetid the id under which this reference will be passed to
|
---|
846 | * the new project. */
|
---|
847 | public void setToRefid(String targetid) {
|
---|
848 | this.targetid = targetid;
|
---|
849 | }
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * Get the id under which this reference will be stored in the new
|
---|
853 | * project.
|
---|
854 | *
|
---|
855 | * @return the id of the reference in the new project.
|
---|
856 | */
|
---|
857 | public String getToRefid() {
|
---|
858 | return targetid;
|
---|
859 | }
|
---|
860 | }
|
---|
861 |
|
---|
862 | /**
|
---|
863 | * Helper class that implements the nested <target>
|
---|
864 | * element of <ant> and <antcall>.
|
---|
865 | * @since Ant 1.6.3
|
---|
866 | */
|
---|
867 | public static class TargetElement {
|
---|
868 | private String name;
|
---|
869 | private String address;
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Default constructor.
|
---|
873 | */
|
---|
874 | public TargetElement() {
|
---|
875 | }
|
---|
876 |
|
---|
877 | /**
|
---|
878 | * Set the name of this TargetElement.
|
---|
879 | * @param name the <CODE>String</CODE> target name.
|
---|
880 | */
|
---|
881 | public void setName(String name) {
|
---|
882 | this.name = name;
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Get the name of this TargetElement.
|
---|
887 | * @return <CODE>String</CODE>.
|
---|
888 | */
|
---|
889 | public String getName() {
|
---|
890 | return name;
|
---|
891 | }
|
---|
892 |
|
---|
893 | }
|
---|
894 | }
|
---|