diff --git a/src/etc/testcases/taskdefs/ant.xml b/src/etc/testcases/taskdefs/ant.xml index 81f795b09..cc62d2ea1 100644 --- a/src/etc/testcases/taskdefs/ant.xml +++ b/src/etc/testcases/taskdefs/ant.xml @@ -143,4 +143,23 @@ + + + + + + + + + + + + + + + test1 is ${test1} + test2 is ${test2} + test1.x is ${test1.x} + diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java index a847b05ff..6a2edc0f0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ant.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java @@ -69,6 +69,7 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectComponent; import org.apache.tools.ant.ProjectHelper; import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.PropertySet; import org.apache.tools.ant.util.FileUtils; /** @@ -129,6 +130,9 @@ public class Ant extends Task { /** The stream to which output is to be written. */ private PrintStream out = null; + /** the sets of properties to pass to the new project */ + private Vector propertySets = new Vector(); + /** * If true, pass all properties to the new Ant project. * Defaults to true. @@ -264,23 +268,13 @@ public class Ant extends Task { } else { // set all properties from calling project + addAlmostAll(getProject().getProperties()); + } - Hashtable props = getProject().getProperties(); - e = props.keys(); - while (e.hasMoreElements()) { - String arg = e.nextElement().toString(); - if ("basedir".equals(arg) || "ant.file".equals(arg)) { - // basedir and ant.file get special treatment in execute() - continue; - } - - String value = props.get(arg).toString(); - // don't re-set user properties, avoid the warning message - if (newProject.getProperty(arg) == null){ - // no user property - newProject.setNewProperty(arg, value); - } - } + e = propertySets.elements(); + while (e.hasMoreElements()) { + PropertySet ps = (PropertySet) e.nextElement(); + addAlmostAll(ps.getProperties()); } } @@ -550,6 +544,31 @@ public class Ant extends Task { newProject.addReference(newKey, copy); } + /** + * Copies all properties from the given table to the new project - + * ommiting those that have already been set in the new project as + * well as properties named basedir or ant.file. + * + * @since Ant 1.6 + */ + private void addAlmostAll(Hashtable props) { + Enumeration e = props.keys(); + while (e.hasMoreElements()) { + String key = e.nextElement().toString(); + if ("basedir".equals(key) || "ant.file".equals(key)) { + // basedir and ant.file get special treatment in execute() + continue; + } + + String value = props.get(key).toString(); + // don't re-set user properties, avoid the warning message + if (newProject.getProperty(key) == null){ + // no user property + newProject.setNewProperty(key, value); + } + } + } + /** * The directory to use as a base directory for the new Ant project. * Defaults to the current project's basedir, unless inheritall @@ -617,6 +636,15 @@ public class Ant extends Task { references.addElement(r); } + /** + * Set of properties to pass to the new project. + * + * @since Ant 1.6 + */ + public void addPropertyset(PropertySet ps) { + propertySets.addElement(ps); + } + /** * Helper class that implements the nested <reference> * element of <ant> and <antcall>. diff --git a/src/main/org/apache/tools/ant/taskdefs/CallTarget.java b/src/main/org/apache/tools/ant/taskdefs/CallTarget.java index cce27c145..0bd56844c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/CallTarget.java +++ b/src/main/org/apache/tools/ant/taskdefs/CallTarget.java @@ -166,6 +166,18 @@ public class CallTarget extends Task { callee.addReference(r); } + /** + * Set of properties to pass to the new project. + * + * @since Ant 1.6 + */ + public void addPropertyset(org.apache.tools.ant.types.PropertySet ps) { + if (callee == null) { + init(); + } + callee.addPropertyset(ps); + } + /** * Target to execute, required. */ diff --git a/src/main/org/apache/tools/ant/taskdefs/SubAnt.java b/src/main/org/apache/tools/ant/taskdefs/SubAnt.java index 0e17386b0..292c0b18d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SubAnt.java +++ b/src/main/org/apache/tools/ant/taskdefs/SubAnt.java @@ -68,6 +68,7 @@ import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.DirSet; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.FileList; +import org.apache.tools.ant.types.PropertySet; import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.taskdefs.Ant; @@ -97,6 +98,7 @@ public class SubAnt private Vector properties = new Vector(); private Vector references = new Vector(); + private Vector propertySets = new Vector(); /** * Runs the various sub-builds. @@ -238,6 +240,14 @@ public class SubAnt references.addElement(r); } + /** + * Corresponds to <ant>'s + * nested <propertyset> element. + */ + public void addPropertyset(PropertySet ps) { + propertySets.addElement(ps); + } + /** * Adds a directory set to the implicit build path. *

@@ -345,6 +355,10 @@ public class SubAnt copyProperty(ant.createProperty(), (Property) i.nextElement()); } + for (Enumeration i = propertySets.elements(); i.hasMoreElements();) { + ant.addPropertyset((PropertySet) i.nextElement()); + } + ant.setInheritRefs(inheritRefs); for (Enumeration i = references.elements(); i.hasMoreElements();) { ant.addReference((Ant.Reference) i.nextElement()); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/AntTest.java b/src/testcases/org/apache/tools/ant/taskdefs/AntTest.java index dbc32b48e..0ccb93e83 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/AntTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/AntTest.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -295,6 +295,13 @@ public class AntTest extends BuildFileTest { "The value of test is 4"); } + public void testPropertySet() { + executeTarget("test-propertyset"); + assertTrue(getLog().indexOf("test1 is ${test1}") > -1); + assertTrue(getLog().indexOf("test2 is ${test2}") > -1); + assertTrue(getLog().indexOf("test1.x is 1") > -1); + } + private class BasedirChecker implements BuildListener { private String[] expectedBasedirs; private int calls = 0;