From dcf31da592d1ef3c7fad80f729b83f500372bed9 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
Date: Tue, 11 Nov 2008 09:36:51 +0000
Subject: [PATCH] add 'as' attribute to in order to control the
imported target's prefixes
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@713005 13f79535-47bb-0310-9956-ffa450edef68
---
WHATSNEW | 3 ++
docs/manual/CoreTasks/import.html | 15 +++++++++
.../org/apache/tools/ant/ProjectHelper.java | 28 +++++++++++++++++
.../tools/ant/helper/ProjectHelper2.java | 29 +++++++++++------
.../apache/tools/ant/taskdefs/ImportTask.java | 19 ++++++++++--
src/tests/antunit/taskdefs/import-test.xml | 31 +++++++++++++++++++
src/tests/antunit/taskdefs/importtests/a.xml | 23 ++++++++++++++
src/tests/antunit/taskdefs/importtests/b.xml | 23 ++++++++++++++
8 files changed, 159 insertions(+), 12 deletions(-)
create mode 100644 src/tests/antunit/taskdefs/import-test.xml
create mode 100644 src/tests/antunit/taskdefs/importtests/a.xml
create mode 100644 src/tests/antunit/taskdefs/importtests/b.xml
diff --git a/WHATSNEW b/WHATSNEW
index 59600904a..da7536ee6 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -516,6 +516,9 @@ Other changes:
STARTTLS.
Bugzilla Report 46063.
+ * has a new attribute "as" that can be used to control the
+ prefix prepended to the imported target's names.
+
Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================
diff --git a/docs/manual/CoreTasks/import.html b/docs/manual/CoreTasks/import.html
index 64541a922..1a3adf9c7 100644
--- a/docs/manual/CoreTasks/import.html
+++ b/docs/manual/CoreTasks/import.html
@@ -62,6 +62,10 @@ made available by the name "builddocs.docs".
This enables the new implementation to call the old target, thus
enhancing it with tasks called before or after it.
+If you use the as attribute of the task, its value will be
+ used to prefix the overriden target's name instead of the name
+ attribute of the project tag.
+
Special Properties
Imported files are treated as they are present in the main
@@ -152,6 +156,17 @@ project).
No |
+
+
+ as
+ |
+
+ Specifies the prefix prepended to the target names. If
+ ommitted, the name attribute of the project tag of the
+ imported file will be used.
+ |
+ No |
+
diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java
index 5bf56f197..db4654a2b 100644
--- a/src/main/org/apache/tools/ant/ProjectHelper.java
+++ b/src/main/org/apache/tools/ant/ProjectHelper.java
@@ -115,6 +115,34 @@ public class ProjectHelper {
return importStack;
}
+ private final static ThreadLocal targetPrefix = new ThreadLocal() {
+ protected Object initialValue() {
+ return (String) null;
+ }
+ };
+
+ /**
+ * The prefix to prepend to imported target names.
+ *
+ * May be set by <import>'s as attribute.
+ *
+ * @return the configured prefix or null
+ *
+ * @since ant 1.8.0
+ */
+ public static String getCurrentTargetPrefix() {
+ return (String) targetPrefix.get();
+ }
+
+ /**
+ * Sets the prefix to prepend to imported target names.
+ *
+ * @since ant 1.8.0
+ */
+ public static void setCurrentTargetPrefix(String prefix) {
+ targetPrefix.set(prefix);
+ }
+
// -------------------- Parse method --------------------
/**
* Parses the project file, configuring the project as it goes.
diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
index 2906fa712..14e8910ba 100644
--- a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
+++ b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
@@ -612,11 +612,6 @@ public class ProjectHelper2 extends ProjectHelper {
&& (uri.equals("") || uri.equals(ANT_CORE_URI))) {
return ProjectHelper2.projectHandler;
}
-// if (context.importlevel > 0) {
-// // we are in an imported file. Allow top-level .
-// if (qname.equals( "target" ) )
-// return ProjectHelper2.targetHandler;
-// }
if (name.equals(qname)) {
throw new SAXParseException("Unexpected element \"{" + uri
+ "}" + name + "\" {" + ANT_CORE_URI + "}" + name, context.getLocator());
@@ -869,11 +864,12 @@ public class ProjectHelper2 extends ProjectHelper {
if (depends.length() > 0) {
target.setDepends(depends);
}
- if (context.isIgnoringProjectTag() && context.getCurrentProjectName() != null
- && context.getCurrentProjectName().length() != 0) {
+ String prefix = null;
+ if (context.isIgnoringProjectTag()
+ && (prefix = getTargetPrefix(context)) != null) {
// In an impored file (and not completely
- // ignoring the project tag)
- String newName = context.getCurrentProjectName() + "." + name;
+ // ignoring the project tag or having a preconfigured prefix)
+ String newName = prefix + "." + name;
Target newTarget = usedTarget ? new Target(target) : target;
newTarget.setName(newName);
context.getCurrentTargets().put(newName, newTarget);
@@ -881,6 +877,21 @@ public class ProjectHelper2 extends ProjectHelper {
}
}
+ private String getTargetPrefix(AntXMLContext context) {
+ String configuredValue = getCurrentTargetPrefix();
+ if (configuredValue != null && configuredValue.length() == 0) {
+ configuredValue = null;
+ }
+ if (configuredValue != null) {
+ return configuredValue;
+ }
+ String projectName = context.getCurrentProjectName();
+ if (projectName != null && projectName.length() == 0) {
+ projectName = null;
+ }
+ return projectName;
+ }
+
/**
* Handles the start of an element within a target.
*
diff --git a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
index 702a3ad78..b029eef1d 100644
--- a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
+++ b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
@@ -34,9 +34,9 @@ import java.util.Vector;
* into the same Project.
*
*
- * Important: we have not finalized how relative file references
- * will be resolved in deep/complex build hierarchies - such as what happens
- * when an imported file imports another file. Use absolute references for
+ * Important: Trying to understand how relative file references
+ * resolved in deep/complex build hierarchies - such as what happens
+ * when an imported file imports another file can be difficult. Use absolute references for
* enhanced build file stability, especially in the imported files.
*
* Examples:
@@ -55,6 +55,7 @@ import java.util.Vector;
public class ImportTask extends Task {
private String file;
private boolean optional;
+ private String targetPrefix;
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
/**
@@ -78,6 +79,15 @@ public class ImportTask extends Task {
this.file = file;
}
+ /**
+ * The prefix to use when prefixing the imported target names.
+ *
+ * @since Ant 1.8.0
+ */
+ public void setAs(String prefix) {
+ targetPrefix = prefix;
+ }
+
/**
* This relies on the task order model.
*
@@ -143,10 +153,13 @@ public class ImportTask extends Task {
}
try {
+ ProjectHelper.setCurrentTargetPrefix(targetPrefix);
helper.parse(getProject(), importedFile);
} catch (BuildException ex) {
throw ProjectHelper.addLocationToBuildException(
ex, getLocation());
+ } finally {
+ ProjectHelper.setCurrentTargetPrefix(null);
}
}
diff --git a/src/tests/antunit/taskdefs/import-test.xml b/src/tests/antunit/taskdefs/import-test.xml
new file mode 100644
index 000000000..5f9650294
--- /dev/null
+++ b/src/tests/antunit/taskdefs/import-test.xml
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/tests/antunit/taskdefs/importtests/a.xml b/src/tests/antunit/taskdefs/importtests/a.xml
new file mode 100644
index 000000000..6082990f1
--- /dev/null
+++ b/src/tests/antunit/taskdefs/importtests/a.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
diff --git a/src/tests/antunit/taskdefs/importtests/b.xml b/src/tests/antunit/taskdefs/importtests/b.xml
new file mode 100644
index 000000000..546badf5a
--- /dev/null
+++ b/src/tests/antunit/taskdefs/importtests/b.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+