Browse Source

add 'as' attribute to <import> 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
master
Stefan Bodewig 16 years ago
parent
commit
dcf31da592
8 changed files with 159 additions and 12 deletions
  1. +3
    -0
      WHATSNEW
  2. +15
    -0
      docs/manual/CoreTasks/import.html
  3. +28
    -0
      src/main/org/apache/tools/ant/ProjectHelper.java
  4. +20
    -9
      src/main/org/apache/tools/ant/helper/ProjectHelper2.java
  5. +16
    -3
      src/main/org/apache/tools/ant/taskdefs/ImportTask.java
  6. +31
    -0
      src/tests/antunit/taskdefs/import-test.xml
  7. +23
    -0
      src/tests/antunit/taskdefs/importtests/a.xml
  8. +23
    -0
      src/tests/antunit/taskdefs/importtests/b.xml

+ 3
- 0
WHATSNEW View File

@@ -516,6 +516,9 @@ Other changes:
STARTTLS.
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
=============================================



+ 15
- 0
docs/manual/CoreTasks/import.html View File

@@ -62,6 +62,10 @@ made available by the name &quot;<b>builddocs</b><b>.docs</b>&quot;.
This enables the new implementation to call the old target, thus
<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>

<p>Imported files are treated as they are present in the main
@@ -152,6 +156,17 @@ project).</p>
</td>
<td valign="top" align="center">No</td>
</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>
</table>



+ 28
- 0
src/main/org/apache/tools/ant/ProjectHelper.java View File

@@ -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.
*
* <p>May be set by &lt;import&gt;'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 --------------------
/**
* Parses the project file, configuring the project as it goes.


+ 20
- 9
src/main/org/apache/tools/ant/helper/ProjectHelper2.java View File

@@ -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 <target>.
// 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.
*


+ 16
- 3
src/main/org/apache/tools/ant/taskdefs/ImportTask.java View File

@@ -34,9 +34,9 @@ import java.util.Vector;
* into the same Project.
* </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.
* </p>
* <p>Examples:</p>
@@ -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);
}
}



+ 31
- 0
src/tests/antunit/taskdefs/import-test.xml View File

@@ -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>

+ 23
- 0
src/tests/antunit/taskdefs/importtests/a.xml View File

@@ -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>


+ 23
- 0
src/tests/antunit/taskdefs/importtests/b.xml View File

@@ -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>


Loading…
Cancel
Save