diff --git a/WHATSNEW b/WHATSNEW index 32de7e391..f5b6c62ba 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -516,8 +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. + * has new attributes "as" and "prefixSeparator" that can be + used to control the prefix prepended to the imported target's + names. * a new task provides an alternative to that should be preferred when you don't want to override any targets. diff --git a/docs/manual/CoreTasks/import.html b/docs/manual/CoreTasks/import.html index ad48ef1a9..2337495ec 100644 --- a/docs/manual/CoreTasks/import.html +++ b/docs/manual/CoreTasks/import.html @@ -175,6 +175,16 @@ project).

No + + + prefixSeparator + + + Specifies the separator to be used between the prefix and the + target name. Defaults to ".". + + No + diff --git a/docs/manual/CoreTasks/include.html b/docs/manual/CoreTasks/include.html index 794c23415..5afa06748 100644 --- a/docs/manual/CoreTasks/include.html +++ b/docs/manual/CoreTasks/include.html @@ -69,6 +69,11 @@ that are not possible with entity includes: all target names are prefixed as well. This makes the included file self-contained.

+

Note that prefixes nest, so if a build file includes a file with + prefix "a" and the included file includes another file with prefix + "b", then the targets of that last build file will be prefixed by + "a.b.".

+

Special Properties

Included files are treated as they are present in the main @@ -171,6 +176,16 @@ project).

Yes, if the included file's project tag doesn't specify a name attribute. + + + prefixSeparator + + + Specifies the separator to be used between the prefix and the + target name. Defaults to ".". + + No + diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index 99a7d292b..13520a94e 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -143,6 +143,32 @@ public class ProjectHelper { targetPrefix.set(prefix); } + private final static ThreadLocal prefixSeparator = new ThreadLocal() { + protected Object initialValue() { + return "."; + } + }; + + /** + * The separator between the prefix and the target name. + * + *

May be set by <import>'s prefixSeperator attribute.

+ * + * @since Ant 1.8.0 + */ + public static String getCurrentPrefixSeparator() { + return (String) prefixSeparator.get(); + } + + /** + * Sets the separator between the prefix and the target name. + * + * @since Ant 1.8.0 + */ + public static void setCurrentPrefixSeparator(String sep) { + prefixSeparator.set(sep); + } + private final static ThreadLocal inIncludeMode = new ThreadLocal() { protected Object initialValue() { return Boolean.FALSE; diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java index e2706c306..831a7d685 100644 --- a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java +++ b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java @@ -849,6 +849,8 @@ public class ProjectHelper2 extends ProjectHelper { String prefix = null; boolean isInIncludeMode = context.isIgnoringProjectTag() && isInIncludeMode(); + String sep = getCurrentPrefixSeparator(); + if (isInIncludeMode) { prefix = getTargetPrefix(context); if (prefix == null) { @@ -858,7 +860,7 @@ public class ProjectHelper2 extends ProjectHelper { + " and the project tag doesn't" + " specify a name attribute"); } - name = prefix + "." + name; + name = prefix + sep + name; } // Check if this target is in the current build file @@ -878,6 +880,7 @@ public class ProjectHelper2 extends ProjectHelper { project.addOrReplaceTarget(name, target); usedTarget = true; } + if (depends.length() > 0) { if (!isInIncludeMode) { target.setDepends(depends); @@ -885,7 +888,7 @@ public class ProjectHelper2 extends ProjectHelper { for (Iterator iter = Target.parseDepends(depends, name).iterator(); iter.hasNext(); ) { - target.addDependency(prefix + "." + iter.next()); + target.addDependency(prefix + sep + iter.next()); } } } @@ -893,7 +896,7 @@ public class ProjectHelper2 extends ProjectHelper { && (prefix = getTargetPrefix(context)) != null) { // In an imported file (and not completely // ignoring the project tag or having a preconfigured prefix) - String newName = prefix + "." + name; + String newName = prefix + sep + name; Target newTarget = usedTarget ? new Target(target) : target; newTarget.setName(newName); context.getCurrentTargets().put(newName, newTarget); @@ -909,10 +912,17 @@ public class ProjectHelper2 extends ProjectHelper { if (configuredValue != null) { return configuredValue; } + String projectName = context.getCurrentProjectName(); - if (projectName != null && projectName.length() == 0) { + if ("".equals(projectName)) { projectName = null; } + + // help nested include tasks + if (projectName != null) { + setCurrentTargetPrefix(projectName); + } + return projectName; } diff --git a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java index 897f189ce..485307e1f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ImportTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ImportTask.java @@ -56,6 +56,7 @@ public class ImportTask extends Task { private String file; private boolean optional; private String targetPrefix; + private String prefixSeparator = "."; private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** @@ -89,9 +90,15 @@ public class ImportTask extends Task { } /** - * This relies on the task order model. + * The separator to use between prefix and target name, default is + * ".". * + * @since Ant 1.8.0 */ + public void setPrefixSeparator(String s) { + prefixSeparator = s; + } + public void execute() { if (file == null) { throw new BuildException("import requires file attribute"); @@ -156,16 +163,22 @@ public class ImportTask extends Task { // importing another one String oldPrefix = ProjectHelper.getCurrentTargetPrefix(); boolean oldIncludeMode = ProjectHelper.isInIncludeMode(); + String oldSep = ProjectHelper.getCurrentPrefixSeparator(); try { - ProjectHelper.setCurrentTargetPrefix(targetPrefix); - ProjectHelper.setInIncludeMode(isInIncludeMode()); + String prefix = targetPrefix; + if (isInIncludeMode() && oldPrefix != null + && targetPrefix != null) { + prefix = oldPrefix + oldSep + targetPrefix; + } + setProjectHelperProps(prefix, prefixSeparator, + isInIncludeMode()); + helper.parse(getProject(), importedFile); } catch (BuildException ex) { throw ProjectHelper.addLocationToBuildException( ex, getLocation()); } finally { - ProjectHelper.setCurrentTargetPrefix(oldPrefix); - ProjectHelper.setInIncludeMode(oldIncludeMode); + setProjectHelperProps(oldPrefix, oldSep, oldIncludeMode); } } @@ -187,4 +200,16 @@ public class ImportTask extends Task { return "include".equals(getTaskType()); } + /** + * Sets a bunch of Thread-local ProjectHelper properties. + * + * @since Ant 1.8.0 + */ + private static void setProjectHelperProps(String prefix, + String prefixSep, + boolean inIncludeMode) { + ProjectHelper.setCurrentTargetPrefix(prefix); + ProjectHelper.setCurrentPrefixSeparator(prefixSep); + ProjectHelper.setInIncludeMode(inIncludeMode); + } } diff --git a/src/tests/antunit/taskdefs/importtests/nested.xml b/src/tests/antunit/taskdefs/importtests/nested.xml new file mode 100644 index 000000000..28d7ad4b8 --- /dev/null +++ b/src/tests/antunit/taskdefs/importtests/nested.xml @@ -0,0 +1,21 @@ + + + + + + diff --git a/src/tests/antunit/taskdefs/include-test.xml b/src/tests/antunit/taskdefs/include-test.xml index 83c8a3f9e..b906254c8 100644 --- a/src/tests/antunit/taskdefs/include-test.xml +++ b/src/tests/antunit/taskdefs/include-test.xml @@ -38,4 +38,10 @@ + + + + +