PR: 34566 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278179 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -90,6 +90,8 @@ Fixed bugs: | |||||
| * JavaTest testcases sometimes fail on windows. Bugzilla Report 34502. | * JavaTest testcases sometimes fail on windows. Bugzilla Report 34502. | ||||
| * Targets with identical name work in imported project. Bugzilla Report 34566. | |||||
| Other changes: | Other changes: | ||||
| -------------- | -------------- | ||||
| @@ -0,0 +1,4 @@ | |||||
| <project> | |||||
| <target name="t"/> | |||||
| <target name="t"/> | |||||
| </project> | |||||
| @@ -0,0 +1,3 @@ | |||||
| <project> | |||||
| <import file="import_same_target.xml"/> | |||||
| </project> | |||||
| @@ -1,5 +1,5 @@ | |||||
| /* | /* | ||||
| * Copyright 2003-2004 The Apache Software Foundation | |||||
| * Copyright 2003-2005 The Apache Software Foundation | |||||
| * | * | ||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
| @@ -92,6 +92,9 @@ public class AntXMLContext { | |||||
| private Map prefixMapping = new HashMap(); | private Map prefixMapping = new HashMap(); | ||||
| /** Keeps track of targets in files */ | |||||
| private Map currentTargets = null; | |||||
| /** | /** | ||||
| * constructor | * constructor | ||||
| * @param project the project to which this antxml context belongs to | * @param project the project to which this antxml context belongs to | ||||
| @@ -341,6 +344,23 @@ public class AntXMLContext { | |||||
| } | } | ||||
| return (String) list.get(list.size() - 1); | return (String) list.get(list.size() - 1); | ||||
| } | } | ||||
| /** | |||||
| * Get the targets in the current source file. | |||||
| * @return the current targets. | |||||
| */ | |||||
| public Map getCurrentTargets() { | |||||
| return currentTargets; | |||||
| } | |||||
| /** | |||||
| * Set the map of the targets in the current source file. | |||||
| * @param currentTargets a map of targets. | |||||
| */ | |||||
| void setCurrentTargets(Map currentTargets) { | |||||
| this.currentTargets = currentTargets; | |||||
| } | |||||
| } | } | ||||
| @@ -24,7 +24,9 @@ import java.io.InputStream; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.UnsupportedEncodingException; | import java.io.UnsupportedEncodingException; | ||||
| import java.net.URL; | import java.net.URL; | ||||
| import java.util.HashMap; | |||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||
| import java.util.Map; | |||||
| import java.util.Stack; | import java.util.Stack; | ||||
| import org.xml.sax.Locator; | import org.xml.sax.Locator; | ||||
| @@ -117,20 +119,24 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
| context.setIgnoreProjectTag(true); | context.setIgnoreProjectTag(true); | ||||
| Target currentTarget = context.getCurrentTarget(); | Target currentTarget = context.getCurrentTarget(); | ||||
| Target currentImplicit = context.getImplicitTarget(); | Target currentImplicit = context.getImplicitTarget(); | ||||
| Map currentTargets = context.getCurrentTargets(); | |||||
| try { | try { | ||||
| Target newCurrent = new Target(); | Target newCurrent = new Target(); | ||||
| newCurrent.setProject(project); | newCurrent.setProject(project); | ||||
| newCurrent.setName(""); | newCurrent.setName(""); | ||||
| context.setCurrentTarget(newCurrent); | context.setCurrentTarget(newCurrent); | ||||
| context.setCurrentTargets(new HashMap()); | |||||
| context.setImplicitTarget(newCurrent); | context.setImplicitTarget(newCurrent); | ||||
| parse(project, source, new RootHandler(context, mainHandler)); | parse(project, source, new RootHandler(context, mainHandler)); | ||||
| newCurrent.execute(); | newCurrent.execute(); | ||||
| } finally { | } finally { | ||||
| context.setCurrentTarget(currentTarget); | context.setCurrentTarget(currentTarget); | ||||
| context.setImplicitTarget(currentImplicit); | context.setImplicitTarget(currentImplicit); | ||||
| context.setCurrentTargets(currentTargets); | |||||
| } | } | ||||
| } else { | } else { | ||||
| // top level file | // top level file | ||||
| context.setCurrentTargets(new HashMap()); | |||||
| parse(project, source, new RootHandler(context, mainHandler)); | parse(project, source, new RootHandler(context, mainHandler)); | ||||
| // Execute the top-level target | // Execute the top-level target | ||||
| context.getImplicitTarget().execute(); | context.getImplicitTarget().execute(); | ||||
| @@ -819,8 +825,7 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
| // If the name has already been defined ( import for example ) | // If the name has already been defined ( import for example ) | ||||
| if (currentTargets.containsKey(name)) { | if (currentTargets.containsKey(name)) { | ||||
| if (!context.isIgnoringProjectTag()) { | |||||
| // not in an import'ed file | |||||
| if (context.getCurrentTargets().get(name) != null) { | |||||
| throw new BuildException( | throw new BuildException( | ||||
| "Duplicate target '" + name + "'", target.getLocation()); | "Duplicate target '" + name + "'", target.getLocation()); | ||||
| } | } | ||||
| @@ -841,6 +846,7 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
| if (name != null) { | if (name != null) { | ||||
| target.setName(name); | target.setName(name); | ||||
| context.getCurrentTargets().put(name, target); | |||||
| project.addOrReplaceTarget(name, target); | project.addOrReplaceTarget(name, target); | ||||
| } | } | ||||
| @@ -1,5 +1,5 @@ | |||||
| /* | /* | ||||
| * Copyright 2003-2004 The Apache Software Foundation | |||||
| * Copyright 2003-2005 The Apache Software Foundation | |||||
| * | * | ||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with the License. | * you may not use this file except in compliance with the License. | ||||
| @@ -91,6 +91,22 @@ public class ImportTest extends BuildFileTest { | |||||
| assertNotNull(getProject().getReference("baz")); | assertNotNull(getProject().getReference("baz")); | ||||
| } | } | ||||
| public void testImportSameTargets() { | |||||
| try { | |||||
| configureProject( | |||||
| "src/etc/testcases/taskdefs/import/same_target.xml"); | |||||
| } catch (BuildException ex) { | |||||
| String message = ex.getMessage(); | |||||
| if (message.indexOf("Duplicate target") == -1) { | |||||
| assertTrue("Did not see 'Duplicate target' in '" + message +"'", false); | |||||
| } | |||||
| return; | |||||
| } | |||||
| assertTrue( | |||||
| "Did not see build exception", | |||||
| false); | |||||
| } | |||||
| public void testImportError() { | public void testImportError() { | ||||
| try { | try { | ||||
| configureProject( | configureProject( | ||||