From 3d66f4ef91b4caeba139914e097e68800c38d941 Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Mon, 16 May 2005 18:59:58 +0000 Subject: [PATCH] Allways create qualified targets in imported build files PR: 28444 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278272 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ src/etc/testcases/taskdefs/import/a.xml | 3 ++ src/etc/testcases/taskdefs/import/b.xml | 4 ++ src/etc/testcases/taskdefs/import/c.xml | 4 ++ src/main/org/apache/tools/ant/Target.java | 24 +++++++-- .../tools/ant/helper/ProjectHelper2.java | 49 ++++++++++--------- .../apache/tools/ant/taskdefs/ImportTest.java | 5 ++ 7 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 src/etc/testcases/taskdefs/import/a.xml create mode 100644 src/etc/testcases/taskdefs/import/b.xml create mode 100644 src/etc/testcases/taskdefs/import/c.xml diff --git a/WHATSNEW b/WHATSNEW index 071b914c4..fcd3c4a26 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -200,6 +200,9 @@ Other changes: can be caused by the test JVM exiting during a test, either via a System.exit() call or a JVM crash. +* project name is now used for *all* targets so one can write consistent import + build file. bugzilla report 28444. + Changes from Ant 1.6.3 to current Ant 1.6 CVS version ===================================================== diff --git a/src/etc/testcases/taskdefs/import/a.xml b/src/etc/testcases/taskdefs/import/a.xml new file mode 100644 index 000000000..2d00379c3 --- /dev/null +++ b/src/etc/testcases/taskdefs/import/a.xml @@ -0,0 +1,3 @@ + + + diff --git a/src/etc/testcases/taskdefs/import/b.xml b/src/etc/testcases/taskdefs/import/b.xml new file mode 100644 index 000000000..be747fa82 --- /dev/null +++ b/src/etc/testcases/taskdefs/import/b.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/etc/testcases/taskdefs/import/c.xml b/src/etc/testcases/taskdefs/import/c.xml new file mode 100644 index 000000000..1163144bd --- /dev/null +++ b/src/etc/testcases/taskdefs/import/c.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/main/org/apache/tools/ant/Target.java b/src/main/org/apache/tools/ant/Target.java index 2f78860aa..81f6d301b 100644 --- a/src/main/org/apache/tools/ant/Target.java +++ b/src/main/org/apache/tools/ant/Target.java @@ -52,11 +52,27 @@ public class Target implements TaskContainer { /** Description of this target, if any. */ private String description = null; - /** Sole constructor. */ + /** Default constructor. */ public Target() { //empty } + /** + * Cloning constructor. + * @param other the Target to clone. + */ + public Target(Target other) { + this.name = other.name; + this.ifCondition = other.ifCondition; + this.unlessCondition = other.unlessCondition; + this.dependencies = other.dependencies; + this.location = other.location; + this.project = other.project; + this.description = other.description; + // The children are added to after this cloning + this.children = other.children; + } + /** * Sets the project this target belongs to. * @@ -209,7 +225,7 @@ public class Target implements TaskContainer { * @return an enumeration of the dependencies of this target */ public Enumeration getDependencies() { - return (dependencies != null ? Collections.enumeration(dependencies) + return (dependencies != null ? Collections.enumeration(dependencies) : new CollectionUtils.EmptyEnumeration()); } @@ -222,7 +238,7 @@ public class Target implements TaskContainer { public boolean dependsOn(String other) { Project p = getProject(); Hashtable t = (p == null) ? null : p.getTargets(); - return (p != null + return (p != null && p.topoSort(getName(), t, false).contains(t.get(other))); } @@ -438,4 +454,4 @@ public class Target implements TaskContainer { String test = project.replaceProperties(unlessCondition); return project.getProperty(test) == null; } -} \ No newline at end of file +} diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java index 98f3e0afb..fa9762b19 100644 --- a/src/main/org/apache/tools/ant/helper/ProjectHelper2.java +++ b/src/main/org/apache/tools/ant/helper/ProjectHelper2.java @@ -815,38 +815,39 @@ public class ProjectHelper2 extends ProjectHelper { + "a name attribute", context.getLocator()); } - Hashtable currentTargets = project.getTargets(); + // Check if this target is in the current build file + if (context.getCurrentTargets().get(name) != null) { + throw new BuildException( + "Duplicate target '" + name + "'", target.getLocation()); + } - // If the name has already been defined ( import for example ) - if (currentTargets.containsKey(name)) { - if (context.getCurrentTargets().get(name) != null) { - throw new BuildException( - "Duplicate target '" + name + "'", target.getLocation()); - } - // Alter the name. - if (context.getCurrentProjectName() != null) { - String newName = context.getCurrentProjectName() - + "." + name; - project.log("Already defined in main or a previous import, " - + "define " + name + " as " + newName, - Project.MSG_VERBOSE); - name = newName; - } else { - project.log("Already defined in main or a previous import, " - + "ignore " + name, Project.MSG_VERBOSE); - name = null; - } + if (depends.length() > 0) { + target.setDepends(depends); } - if (name != null) { + Hashtable projectTargets = project.getTargets(); + boolean usedTarget = false; + // If the name has not already been defined define it + if (projectTargets.containsKey(name)) { + project.log("Already defined in main or a previous import, " + + "ignore " + name, Project.MSG_VERBOSE); + } else { target.setName(name); context.getCurrentTargets().put(name, target); project.addOrReplaceTarget(name, target); + usedTarget = true; } - // take care of dependencies - if (depends.length() > 0) { - target.setDepends(depends); + if (context.isIgnoringProjectTag() && context.getCurrentProjectName() != null + && context.getCurrentProjectName().length() != 0) { + // In an impored file (and not completely + // ignoring the project tag) + String newName = context.getCurrentProjectName() + + "." + name; + Target newTarget = usedTarget ? new Target(target) : target; + newTarget.setName(newName); + context.getCurrentTargets().put(newName, newTarget); + project.addOrReplaceTarget(newName, newTarget); } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java b/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java index c9ae9e281..1fa184a04 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/ImportTest.java @@ -163,4 +163,9 @@ public class ImportTest extends BuildFileTest { configureProject("src/etc/testcases/taskdefs/import/importtargetfirst.xml"); assertLogContaining("Importing targetfirstAfter target firstAfter importing"); } + + public void testTargetName() { + configureProject("src/etc/testcases/taskdefs/import/c.xml"); + } + }