summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatt Benson <mbenson@apache.org>2022-02-25 18:48:19 -0600
committerMatt Benson <mbenson@apache.org>2022-02-25 18:48:19 -0600
commite7c48c8f8677cd3dff58f3b836b41543f8e37032 (patch)
treef1d25db68d4c4a0fca4d8c2ba9a9f3f4cb56a38a /src
parentc4552aaaf2932144e2d689a838a9a0db02c64741 (diff)
downloadant-e7c48c8f8677cd3dff58f3b836b41543f8e37032.tar.gz
local += nested name elements
Diffstat (limited to 'src')
-rw-r--r--src/main/org/apache/tools/ant/taskdefs/Local.java56
-rw-r--r--src/tests/antunit/taskdefs/local-test.xml20
2 files changed, 72 insertions, 4 deletions
diff --git a/src/main/org/apache/tools/ant/taskdefs/Local.java b/src/main/org/apache/tools/ant/taskdefs/Local.java
index 890571dfb..834b3e844 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Local.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Local.java
@@ -17,15 +17,48 @@
*/
package org.apache.tools.ant.taskdefs;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.function.Consumer;
+
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.property.LocalProperties;
+import org.apache.tools.ant.util.StringUtils;
/**
- * Task to create a local property in the current scope.
+ * Task to create local properties in the current scope.
*/
public class Local extends Task {
+ /**
+ * Nested {@code name} element.
+ */
+ public static class Name implements Consumer<LocalProperties> {
+ private String text;
+
+ /**
+ * Set the property name.
+ * @param text
+ */
+ public void addText(String text) {
+ this.text = text;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void accept(LocalProperties localProperties) {
+ if (text == null) {
+ throw new BuildException("nested name element is missing text");
+ }
+ localProperties.addLocal(text);
+ }
+ }
+
private String name;
+
+ private final Set<Name> nameElements = new LinkedHashSet<>();
/**
* Set the name attribute.
@@ -36,12 +69,27 @@ public class Local extends Task {
}
/**
+ * Create a nested {@code name} element.
+ * @return {@link Name}
+ */
+ public Name createName() {
+ final Name result = new Name();
+ nameElements.add(result);
+ return result;
+ }
+
+ /**
* Run the task.
*/
public void execute() {
- if (name == null) {
- throw new BuildException("Missing attribute name");
+ if (name == null && nameElements.isEmpty()) {
+ throw new BuildException("Found no configured local property names");
+ }
+ final LocalProperties localProperties = LocalProperties.get(getProject());
+
+ if (name != null) {
+ localProperties.addLocal(name);
}
- LocalProperties.get(getProject()).addLocal(name);
+ nameElements.forEach(n -> n.accept(localProperties));
}
}
diff --git a/src/tests/antunit/taskdefs/local-test.xml b/src/tests/antunit/taskdefs/local-test.xml
index e8af945b2..8c4c51baa 100644
--- a/src/tests/antunit/taskdefs/local-test.xml
+++ b/src/tests/antunit/taskdefs/local-test.xml
@@ -93,4 +93,24 @@
<au:assertPropertyEquals name="foo" value="foo" />
</target>
+ <target name="testMulti">
+ <property name="bar" value="bar" />
+ <property name="baz" value="baz" />
+ <sequential>
+ <local>
+ <name>foo</name>
+ <name>bar</name>
+ <name>baz</name>
+ </local>
+ <property name="foo" value="FOO" />
+ <property name="bar" value="BAR" />
+ <property name="baz" value="BAZ" />
+ <au:assertPropertyEquals name="foo" value="FOO" />
+ <au:assertPropertyEquals name="bar" value="BAR" />
+ <au:assertPropertyEquals name="baz" value="BAZ" />
+ </sequential>
+ <au:assertPropertyEquals name="foo" value="foo" />
+ <au:assertPropertyEquals name="bar" value="bar" />
+ <au:assertPropertyEquals name="baz" value="baz" />
+ </target>
</project>