diff --git a/manual/Tasks/local.html b/manual/Tasks/local.html index 830cc1858..2e64ec631 100644 --- a/manual/Tasks/local.html +++ b/manual/Tasks/local.html @@ -53,6 +53,12 @@ examples section.

+

Parameters specified as nested elements

+

Name

+

As an alternative to (or in conjunction with) the name attribute, the nested text of +each of one or more nested <name> elements specifies a property name to declare in +the local scope. +

Examples

Temporarily shadow a global property's value

@@ -171,5 +177,15 @@ come up with unique names in some cases.

Each invocation gets its own property named parent and there will be no global property of that name at all.

+

Use of nested name elements

+This style declares and executes a single task, as compensation for requiring more lines of XML than +would individual invocations using @name: +
+<local>
+  <name>foo</name>
+  <name>bar</name>
+  <name>baz</name>
+</local>
+
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 { + 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 nameElements = new LinkedHashSet<>(); /** * Set the name attribute. @@ -35,13 +68,28 @@ public class Local extends Task { this.name = name; } + /** + * 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 @@ + + + + + + foo + bar + baz + + + + + + + + + + + +