git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@713005 13f79535-47bb-0310-9956-ffa450edef68master
@@ -516,6 +516,9 @@ Other changes: | |||||
STARTTLS. | STARTTLS. | ||||
Bugzilla Report 46063. | Bugzilla Report 46063. | ||||
* <import> 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 | Changes from Ant 1.7.0 TO Ant 1.7.1 | ||||
============================================= | ============================================= | ||||
@@ -62,6 +62,10 @@ made available by the name "<b>builddocs</b><b>.docs</b>". | |||||
This enables the new implementation to call the old target, thus | This enables the new implementation to call the old target, thus | ||||
<i>enhancing</i> it with tasks called before or after it.</p> | <i>enhancing</i> it with tasks called before or after it.</p> | ||||
<p>If you use the <i>as</i> 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.</p> | |||||
<h4>Special Properties</h4> | <h4>Special Properties</h4> | ||||
<p>Imported files are treated as they are present in the main | <p>Imported files are treated as they are present in the main | ||||
@@ -152,6 +156,17 @@ project).</p> | |||||
</td> | </td> | ||||
<td valign="top" align="center">No</td> | <td valign="top" align="center">No</td> | ||||
</tr> | </tr> | ||||
<tr> | |||||
<td valign="top"> | |||||
as | |||||
</td> | |||||
<td valign="top"> | |||||
Specifies the prefix prepended to the target names. If | |||||
ommitted, the name attribute of the project tag of the | |||||
imported file will be used. | |||||
</td> | |||||
<td valign="top" align="center">No</td> | |||||
</tr> | |||||
</tbody> | </tbody> | ||||
</table> | </table> | ||||
@@ -115,6 +115,34 @@ public class ProjectHelper { | |||||
return importStack; | return importStack; | ||||
} | } | ||||
private final static ThreadLocal targetPrefix = new ThreadLocal() { | |||||
protected Object initialValue() { | |||||
return (String) null; | |||||
} | |||||
}; | |||||
/** | |||||
* The prefix to prepend to imported target names. | |||||
* | |||||
* <p>May be set by <import>'s as attribute.</p> | |||||
* | |||||
* @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 -------------------- | // -------------------- Parse method -------------------- | ||||
/** | /** | ||||
* Parses the project file, configuring the project as it goes. | * Parses the project file, configuring the project as it goes. | ||||
@@ -612,11 +612,6 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
&& (uri.equals("") || uri.equals(ANT_CORE_URI))) { | && (uri.equals("") || uri.equals(ANT_CORE_URI))) { | ||||
return ProjectHelper2.projectHandler; | return ProjectHelper2.projectHandler; | ||||
} | } | ||||
// if (context.importlevel > 0) { | |||||
// // we are in an imported file. Allow top-level <target>. | |||||
// if (qname.equals( "target" ) ) | |||||
// return ProjectHelper2.targetHandler; | |||||
// } | |||||
if (name.equals(qname)) { | if (name.equals(qname)) { | ||||
throw new SAXParseException("Unexpected element \"{" + uri | throw new SAXParseException("Unexpected element \"{" + uri | ||||
+ "}" + name + "\" {" + ANT_CORE_URI + "}" + name, context.getLocator()); | + "}" + name + "\" {" + ANT_CORE_URI + "}" + name, context.getLocator()); | ||||
@@ -869,11 +864,12 @@ public class ProjectHelper2 extends ProjectHelper { | |||||
if (depends.length() > 0) { | if (depends.length() > 0) { | ||||
target.setDepends(depends); | 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 | // 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; | Target newTarget = usedTarget ? new Target(target) : target; | ||||
newTarget.setName(newName); | newTarget.setName(newName); | ||||
context.getCurrentTargets().put(newName, newTarget); | 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. | * Handles the start of an element within a target. | ||||
* | * | ||||
@@ -34,9 +34,9 @@ import java.util.Vector; | |||||
* into the same Project. | * into the same Project. | ||||
* </p> | * </p> | ||||
* <p> | * <p> | ||||
* <b>Important</b>: 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 | |||||
* <b>Important</b>: 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. | * enhanced build file stability, especially in the imported files. | ||||
* </p> | * </p> | ||||
* <p>Examples:</p> | * <p>Examples:</p> | ||||
@@ -55,6 +55,7 @@ import java.util.Vector; | |||||
public class ImportTask extends Task { | public class ImportTask extends Task { | ||||
private String file; | private String file; | ||||
private boolean optional; | private boolean optional; | ||||
private String targetPrefix; | |||||
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); | ||||
/** | /** | ||||
@@ -78,6 +79,15 @@ public class ImportTask extends Task { | |||||
this.file = file; | 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. | * This relies on the task order model. | ||||
* | * | ||||
@@ -143,10 +153,13 @@ public class ImportTask extends Task { | |||||
} | } | ||||
try { | try { | ||||
ProjectHelper.setCurrentTargetPrefix(targetPrefix); | |||||
helper.parse(getProject(), importedFile); | helper.parse(getProject(), importedFile); | ||||
} catch (BuildException ex) { | } catch (BuildException ex) { | ||||
throw ProjectHelper.addLocationToBuildException( | throw ProjectHelper.addLocationToBuildException( | ||||
ex, getLocation()); | ex, getLocation()); | ||||
} finally { | |||||
ProjectHelper.setCurrentTargetPrefix(null); | |||||
} | } | ||||
} | } | ||||
@@ -0,0 +1,31 @@ | |||||
<?xml version="1.0"?> | |||||
<!-- | |||||
Licensed to the Apache Software Foundation (ASF) under one or more | |||||
contributor license agreements. See the NOTICE file distributed with | |||||
this work for additional information regarding copyright ownership. | |||||
The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
(the "License"); you may not use this file except in compliance with | |||||
the License. You may obtain a copy of the License at | |||||
http://www.apache.org/licenses/LICENSE-2.0 | |||||
Unless required by applicable law or agreed to in writing, software | |||||
distributed under the License is distributed on an "AS IS" BASIS, | |||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
See the License for the specific language governing permissions and | |||||
limitations under the License. | |||||
--> | |||||
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"> | |||||
<import file="../antunit-base.xml" /> | |||||
<import file="importtests/a.xml"/> | |||||
<import file="importtests/b.xml" as="c"/> | |||||
<target name="testNoExplicitPrefix" depends="a.a"> | |||||
<au:assertEquals expected="bar" actual="${foo}"/> | |||||
</target> | |||||
<target name="testExplicitPrefix" depends="c.b"> | |||||
<au:assertEquals expected="baz" actual="${foo}"/> | |||||
</target> | |||||
</project> |
@@ -0,0 +1,23 @@ | |||||
<?xml version="1.0"?> | |||||
<!-- | |||||
Licensed to the Apache Software Foundation (ASF) under one or more | |||||
contributor license agreements. See the NOTICE file distributed with | |||||
this work for additional information regarding copyright ownership. | |||||
The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
(the "License"); you may not use this file except in compliance with | |||||
the License. You may obtain a copy of the License at | |||||
http://www.apache.org/licenses/LICENSE-2.0 | |||||
Unless required by applicable law or agreed to in writing, software | |||||
distributed under the License is distributed on an "AS IS" BASIS, | |||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
See the License for the specific language governing permissions and | |||||
limitations under the License. | |||||
--> | |||||
<project name="a"> | |||||
<target name="a"> | |||||
<property name="foo" value="bar"/> | |||||
</target> | |||||
</project> | |||||
@@ -0,0 +1,23 @@ | |||||
<?xml version="1.0"?> | |||||
<!-- | |||||
Licensed to the Apache Software Foundation (ASF) under one or more | |||||
contributor license agreements. See the NOTICE file distributed with | |||||
this work for additional information regarding copyright ownership. | |||||
The ASF licenses this file to You under the Apache License, Version 2.0 | |||||
(the "License"); you may not use this file except in compliance with | |||||
the License. You may obtain a copy of the License at | |||||
http://www.apache.org/licenses/LICENSE-2.0 | |||||
Unless required by applicable law or agreed to in writing, software | |||||
distributed under the License is distributed on an "AS IS" BASIS, | |||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
See the License for the specific language governing permissions and | |||||
limitations under the License. | |||||
--> | |||||
<project name="b"> | |||||
<target name="b"> | |||||
<property name="foo" value="baz"/> | |||||
</target> | |||||
</project> | |||||