Browse Source

add an <import> variant named <include> that matches EasyAnt's <use>

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@713016 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
32c6ef39e4
13 changed files with 571 additions and 16 deletions
  1. +3
    -0
      WHATSNEW
  2. +92
    -1
      docs/manual/CoreTasks/import.html
  3. +274
    -0
      docs/manual/CoreTasks/include.html
  4. +1
    -0
      docs/manual/coretasklist.html
  5. +5
    -0
      docs/manual/tasksoverview.html
  6. +37
    -2
      src/main/org/apache/tools/ant/ProjectHelper.java
  7. +22
    -7
      src/main/org/apache/tools/ant/Target.java
  8. +29
    -5
      src/main/org/apache/tools/ant/helper/ProjectHelper2.java
  9. +23
    -1
      src/main/org/apache/tools/ant/taskdefs/ImportTask.java
  10. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  11. +18
    -0
      src/tests/antunit/taskdefs/import-test.xml
  12. +25
    -0
      src/tests/antunit/taskdefs/importtests/override.xml
  13. +41
    -0
      src/tests/antunit/taskdefs/include-test.xml

+ 3
- 0
WHATSNEW View File

@@ -519,6 +519,9 @@ Other changes:
* <import> has a new attribute "as" that can be used to control the
prefix prepended to the imported target's names.

* a new task <include> provides an alternative to <import> that
should be preferred when you don't want to override any targets.

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 92
- 1
docs/manual/CoreTasks/import.html View File

@@ -27,6 +27,14 @@
<p>
Imports another build file into the current project.
</p>

<p>
<b>Note</b> this task heavily relies on the ProjectHelper
implementation and doesn't really perform any work of its own. If
you have configured Ant to use a ProjectHelper other than Ant's
default, this task may or may not work.
</p>

<p>
On execution it will read another Ant file into
the same Project. This means that it basically works like the
@@ -83,7 +91,7 @@ property of the main buildfile.</p>
<p>Note that &quot;builddocs&quot; is not the filename, but the name attribute
present in the imported project tag.</p>
<p>
If import file does not have a name attribute, the ant.file.projectname
If the imported file does not have a name attribute, the ant.file.projectname
property will not be set.
</p>

@@ -182,6 +190,89 @@ directory.</p>

<p>Imports the project defined by the property deploy-platform</p>

<h3>How is &lt;import&gt; different
from <a href="include.html">&lt;include&gt;</a>?</h3>

<p>When using import the imported targets are available by up to two
names. Their "normal" name without any prefix and potentially with
a prefixed name (the value of the as attribute or the imported
project's name attribute, if any).</p>

<p>When using include the included targets are only available in the
prefixed form.</p>

<p>When using import, the imported target's depends attribute
remains unchanged, i.e. it uses "normal" names and allows you to
override targets in the dependency list.</p>

<p>When using include, the included target's depends attribute is
rewritten so that prefixed names are used. This allows writers of
the included file to control which target is invoked as part of the
dependencies.</p>

<p>It is possible to include the same file more than once by using
different prefixes, it is not possible to import the same file more
than once.</p>

<p>Use import if you intend to override a target, otherwise use include.</p>

<p><i>nested.xml</i> shall be:</p>

<pre>
&lt;project&gt;
&lt;target name="setUp"&gt;
&lt;property name="prop" value="in nested"/&gt;
&lt;/target&gt;

&lt;target name="echo" depends="setUp"&gt;
&lt;echo&gt;prop has the value ${prop}&lt;/echo&gt;
&lt;/target&gt;
&lt;/project&gt;
</pre>

<p>When using import like in</p>

<pre>
&lt;project&gt;
&lt;target name="setUp"&gt;
&lt;property name="prop" value="in importing"/&gt;
&lt;/target&gt;

&lt;import file="nested.xml" as="nested"/&gt;
&lt;/project&gt;
</pre>

<p>Running the target <i>nested.echo</i> will emit:

<pre>
setUp:

nested.echo:
[echo] prop has the value in importing
</pre>

<p>When using include like in</p>

<pre>
&lt;project&gt;
&lt;target name="setUp"&gt;
&lt;property name="prop" value="in importing"/&gt;
&lt;/target&gt;

&lt;include file="nested.xml" as="nested"/&gt;
&lt;/project&gt;
</pre>

<p>Running the target <i>nested.echo</i> will emit:

<pre>
nested.setUp:

nested.echo:
[echo] prop has the value in nested
</pre>

<p>and there won't be any target named "echo" on the including build file.</p>

</body>
</html>

+ 274
- 0
docs/manual/CoreTasks/include.html View File

@@ -0,0 +1,274 @@
<!--
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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
<title>Include Task</title>
</head>
<body>
<h2><a name="include">Include</a></h2>
<h3>Description</h3>
<p>
Include another build file into the current project.
</p>

<p>
<b>Note</b> this task heavily relies on the ProjectHelper
implementation and doesn't really perform any work of its own. If
you have configured Ant to use a ProjectHelper other than Ant's
default, this task may or may not work.
</p>

<p>
On execution it will read another Ant file into the same Project
rewriting the included target names and depends lists. This is
different
from <a href="http://ant.apache.org/faq.html#xml-entity-include">Entity
Includes as explained in the Ant FAQ</a> in so far as the target
names get prefixed by the included project's name or the as
attribute and do not appear as if the file was contained in the
including file.
</p>
<p>
The include task may only be used as a top-level task. This means that
it may not be used in a target.
</p>
<p>
There are two further functional aspects that pertain to this task and
that are not possible with entity includes:
<ul>
<li>target rewriting</li>
<li>special properties</li>
</ul>
</p>
<h4>Target rewriting</h4>

<p>Any target in the included file will be renamed
to <i>prefix.name</i> where <i>name</i> is the original target's
name and <i>prefix</i> is either the value of the <i>as</i>
attribute or the <i>name</i> attribute of the <i>project</i> tag of
the included file.</p>

<p>The depends attribute of all included targets is rewritten so that
all target names are prefixed as well. This makes the included file
self-contained.</p>

<h4>Special Properties</h4>

<p>Included files are treated as they are present in the main
buildfile. This makes it easy to understand, but it makes it impossible
for them to reference files and resources relative to their path.
Because of this, for every included file, Ant adds a property that
contains the path to the included buildfile. With this path, the
included buildfile can keep resources and be able to reference them
relative to its position.</p>

<p>So if I include for example a <i>docsbuild.xml</i> file named <b>builddocs</b>,
I can get its path as <b>ant.file.builddocs</b>, similarly to the <b>ant.file</b>
property of the main buildfile.</p>

<p>Note that &quot;builddocs&quot; is not the filename, but the name attribute
present in the included project tag.</p>
<p>
If the included file does not have a name attribute, the ant.file.projectname
property will not be set.
</p>

<h4>Resolving files against the included file</h4>

<p>Suppose your main build file called <code>including.xml</code>
includes a build file <code>included.xml</code>, located anywhere on
the file system, and <code>included.xml</code> reads a set of
properties from <code>included.properties</code>:</p>

<pre>&lt;!-- including.xml --&gt;
&lt;project name="including" basedir="." default="..."&gt;
&nbsp; &lt;include file="${path_to_included}/included.xml"/&gt;
&lt;/project&gt;

&lt;!-- included.xml --&gt;
&lt;project name="included" basedir="." default="..."&gt;
&nbsp; &lt;property file="included.properties"/&gt;
&lt;/project&gt;
</pre>

<p>This snippet however will resolve <code>included.properties</code>
against the basedir of <code>including.xml</code>, because the basedir
of <code>included.xml</code> is ignored by Ant. The right way to use
<code>included.properties</code> is:</p>

<pre>
&lt;!-- included.xml --&gt;
&lt;project name="included" basedir="." default="..."&gt;
&nbsp; &lt;dirname property="included.basedir" file="${ant.file.included}"/&gt;
&nbsp; &lt;property file="${included.basedir}/included.properties"/&gt;
&lt;/project&gt;
</pre>

<p>As explained above <code>${ant.file.included}</code> stores the
path of the build script, that defines the project called
<code>included</code>, (in short it stores the path to
<code>included.xml</code>) and <a
href="dirname.html"><code>&lt;dirname&gt;</code></a> takes its
directory. This technique also allows <code>included.xml</code> to be
used as a standalone file (without being included in other
project).</p>

<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tbody>
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">
file
</td>
<td valign="top">
The file to include. If this is a relative file name, the file name will be resolved
relative to the <i>including</i> file. <b>Note</b>, this is unlike most other
ant file attributes, where relative files are resolved relative to ${basedir}.
</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">
optional
</td>
<td valign="top">
If true, do not stop the build if the file does not exist,
default is false.
</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">Yes, if the included file's
project tag doesn't specify a name attribute.</td>
</tr>
</tbody>
</table>

<h3>Examples</h3>
<pre>&nbsp; &lt;include file=&quot;../common-targets.xml&quot;/&gt;
</pre>

<p>Includes targets from the common-targets.xml file that is in a parent
directory.</p>

<pre>&nbsp; &lt;include file=&quot;${deploy-platform}.xml&quot;/&gt;
</pre>

<p>Includes the project defined by the property deploy-platform</p>

<h3>How is <a href="import.html">&lt;import&gt;</a> different
from &lt;include&gt;?</h3>

<p>When using import the imported targets are available by up to two
names. Their "normal" name without any prefix and potentially with
a prefixed name (the value of the as attribute or the imported
project's name attribute, if any).</p>

<p>When using include the included targets are only available in the
prefixed form.</p>

<p>When using import, the imported target's depends attribute
remains unchanged, i.e. it uses "normal" names and allows you to
override targets in the dependency list.</p>

<p>When using include, the included target's depends attribute is
rewritten so that prefixed names are used. This allows writers of
the included file to control which target is invoked as part of the
dependencies.</p>

<p>It is possible to include the same file more than once by using
different prefixes, it is not possible to import the same file more
than once.</p>

<p>Use import if you intend to override a target, otherwise use include.</p>

<p><i>nested.xml</i> shall be:</p>

<pre>
&lt;project&gt;
&lt;target name="setUp"&gt;
&lt;property name="prop" value="in nested"/&gt;
&lt;/target&gt;

&lt;target name="echo" depends="setUp"&gt;
&lt;echo&gt;prop has the value ${prop}&lt;/echo&gt;
&lt;/target&gt;
&lt;/project&gt;
</pre>

<p>When using import like in</p>

<pre>
&lt;project&gt;
&lt;target name="setUp"&gt;
&lt;property name="prop" value="in importing"/&gt;
&lt;/target&gt;

&lt;import file="nested.xml" as="nested"/&gt;
&lt;/project&gt;
</pre>

<p>Running the target <i>nested.echo</i> will emit:

<pre>
setUp:

nested.echo:
[echo] prop has the value in importing
</pre>

<p>When using include like in</p>

<pre>
&lt;project&gt;
&lt;target name="setUp"&gt;
&lt;property name="prop" value="in importing"/&gt;
&lt;/target&gt;

&lt;include file="nested.xml" as="nested"/&gt;
&lt;/project&gt;
</pre>

<p>Running the target <i>nested.echo</i> will emit:

<pre>
nested.setUp:

nested.echo:
[echo] prop has the value in nested
</pre>

<p>and there won't be any target named "echo" on the including build file.</p>

</body>
</html>

+ 1
- 0
docs/manual/coretasklist.html View File

@@ -77,6 +77,7 @@
<a href="CoreTasks/unpack.html">GUnzip</a><br/>
<a href="CoreTasks/pack.html">GZip</a><br/>
<a href="CoreTasks/import.html">Import</a><br/>
<a href="CoreTasks/include.html">Include</a><br/>
<a href="CoreTasks/input.html">Input</a><br/>
<a href="CoreTasks/jar.html">Jar</a><br/>
<a href="CoreTasks/java.html">Java</a><br/>


+ 5
- 0
docs/manual/tasksoverview.html View File

@@ -843,6 +843,11 @@ documentation.</p>
in it with targets of your own.</p></td>
</tr>

<tr valign="top">
<td nowrap><a href="CoreTasks/include.html">Include</a></td>
<td><p>Include another build file.</p></td>
</tr>

<tr valign="top">
<td nowrap><a href="OptionalTasks/javacc.html">JavaCC</a></td>
<td><p>Invokes the


+ 37
- 2
src/main/org/apache/tools/ant/ProjectHelper.java View File

@@ -128,7 +128,7 @@ public class ProjectHelper {
*
* @return the configured prefix or null
*
* @since ant 1.8.0
* @since Ant 1.8.0
*/
public static String getCurrentTargetPrefix() {
return (String) targetPrefix.get();
@@ -137,12 +137,47 @@ public class ProjectHelper {
/**
* Sets the prefix to prepend to imported target names.
*
* @since ant 1.8.0
* @since Ant 1.8.0
*/
public static void setCurrentTargetPrefix(String prefix) {
targetPrefix.set(prefix);
}

private final static ThreadLocal inIncludeMode = new ThreadLocal() {
protected Object initialValue() {
return Boolean.FALSE;
}
};

/**
* Whether the current file should be read in include as opposed
* to import mode.
*
* <p>In include mode included targets are only known by their
* prefixed names and their depends lists get rewritten so that
* all dependencies get the prefix as well.</p>
*
* <p>In import mode imported targets are known by an adorned as
* well as a prefixed name and the unadorned target may be
* overwritten in the importing build file. The depends list of
* the imported targets is not modified at all.</p>
*
* @since Ant 1.8.0
*/
public static boolean isInIncludeMode() {
return inIncludeMode.get() == Boolean.TRUE;
}

/**
* Sets whether the current file should be read in include as
* opposed to import mode.
*
* @since Ant 1.8.0
*/
public static void setInIncludeMode(boolean includeMode) {
inIncludeMode.set(includeMode ? Boolean.TRUE : Boolean.FALSE);
}

// -------------------- Parse method --------------------
/**
* Parses the project file, configuring the project as it goes.


+ 22
- 7
src/main/org/apache/tools/ant/Target.java View File

@@ -125,19 +125,31 @@ public class Target implements TaskContainer {
* depends on. Must not be <code>null</code>.
*/
public void setDepends(String depS) {
if (depS.length() > 0) {
for (Iterator iter = parseDepends(depS, getName()).iterator();
iter.hasNext(); ) {
addDependency((String) iter.next());
}
}

public static List/*<String>*/ parseDepends(String depends,
String targetName) {
ArrayList list = new ArrayList();
if (depends.length() > 0) {
StringTokenizer tok =
new StringTokenizer(depS, ",", true);
new StringTokenizer(depends, ",", true);
while (tok.hasMoreTokens()) {
String token = tok.nextToken().trim();

// Make sure the dependency is not empty string
if ("".equals(token) || ",".equals(token)) {
throw new BuildException("Syntax Error: depends " + "attribute of target \""
+ getName() + "\" has an empty string as dependency.");
throw new BuildException("Syntax Error: depends "
+ "attribute of target \""
+ targetName
+ "\" has an empty string as "
+ "dependency.");
}

addDependency(token);
list.add(token);

// Make sure that depends attribute does not
// end in a ,
@@ -145,12 +157,15 @@ public class Target implements TaskContainer {
token = tok.nextToken();
if (!tok.hasMoreTokens() || !",".equals(token)) {
throw new BuildException("Syntax Error: Depend "
+ "attribute for target \"" + getName()
+ "\" ends with a , character");
+ "attribute for target \""
+ targetName
+ "\" ends with a \",\" "
+ "character");
}
}
}
}
return list;
}

/**


+ 29
- 5
src/main/org/apache/tools/ant/helper/ProjectHelper2.java View File

@@ -45,6 +45,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;

@@ -845,9 +846,25 @@ public class ProjectHelper2 extends ProjectHelper {
context.getLocator());
}

String prefix = null;
boolean isInIncludeMode =
context.isIgnoringProjectTag() && isInIncludeMode();
if (isInIncludeMode) {
prefix = getTargetPrefix(context);
if (prefix == null) {
throw new BuildException("can't include build file "
+ context.getBuildFile()
+ ", no as attribute has been given"
+ " and the project tag doesn't"
+ " specify a name attribute");
}
name = prefix + "." + name;
}

// Check if this target is in the current build file
if (context.getCurrentTargets().get(name) != null) {
throw new BuildException("Duplicate target '" + name + "'", target.getLocation());
throw new BuildException("Duplicate target '" + name + "'",
target.getLocation());
}
Hashtable projectTargets = project.getTargets();
boolean usedTarget = false;
@@ -862,12 +879,19 @@ public class ProjectHelper2 extends ProjectHelper {
usedTarget = true;
}
if (depends.length() > 0) {
target.setDepends(depends);
if (!isInIncludeMode) {
target.setDepends(depends);
} else {
for (Iterator iter =
Target.parseDepends(depends, name).iterator();
iter.hasNext(); ) {
target.addDependency(prefix + "." + iter.next());
}
}
}
String prefix = null;
if (context.isIgnoringProjectTag()
if (!isInIncludeMode && context.isIgnoringProjectTag()
&& (prefix = getTargetPrefix(context)) != null) {
// In an impored file (and not completely
// In an imported file (and not completely
// ignoring the project tag or having a preconfigured prefix)
String newName = prefix + "." + name;
Target newTarget = usedTarget ? new Target(target) : target;


+ 23
- 1
src/main/org/apache/tools/ant/taskdefs/ImportTask.java View File

@@ -145,7 +145,7 @@ public class ImportTask extends Task {
}
}

if (importStack.contains(importedFile)) {
if (!isInIncludeMode() && importStack.contains(importedFile)) {
getProject().log(
"Skipped already imported file:\n "
+ importedFile + "\n", Project.MSG_VERBOSE);
@@ -155,14 +155,36 @@ public class ImportTask extends Task {
// nested invokations are possible like an imported file
// importing another one
String oldPrefix = ProjectHelper.getCurrentTargetPrefix();
boolean oldIncludeMode = ProjectHelper.isInIncludeMode();
try {
ProjectHelper.setCurrentTargetPrefix(targetPrefix);
ProjectHelper.setInIncludeMode(isInIncludeMode());
helper.parse(getProject(), importedFile);
} catch (BuildException ex) {
throw ProjectHelper.addLocationToBuildException(
ex, getLocation());
} finally {
ProjectHelper.setCurrentTargetPrefix(oldPrefix);
ProjectHelper.setInIncludeMode(oldIncludeMode);
}
}

/**
* Whether the task is in include (as opposed to import) mode.
*
* <p>In include mode included targets are only known by their
* prefixed names and their depends lists get rewritten so that
* all dependencies get the prefix as well.</p>
*
* <p>In import mode imported targets are known by an adorned as
* well as a prefixed name and the unadorned target may be
* overwritten in the importing build file. The depends list of
* the imported targets is not modified at all.</p>
*
* @since Ant 1.8.0
*/
protected final boolean isInIncludeMode() {
return "include".equals(getTaskType());
}

}

+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

@@ -56,6 +56,7 @@ get=org.apache.tools.ant.taskdefs.Get
gunzip=org.apache.tools.ant.taskdefs.GUnzip
gzip=org.apache.tools.ant.taskdefs.GZip
import=org.apache.tools.ant.taskdefs.ImportTask
include=org.apache.tools.ant.taskdefs.ImportTask
input=org.apache.tools.ant.taskdefs.Input
jar=org.apache.tools.ant.taskdefs.Jar
java=org.apache.tools.ant.taskdefs.Java


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

@@ -28,4 +28,22 @@
<target name="testExplicitPrefix" depends="c.b">
<au:assertEquals expected="baz" actual="${foo}"/>
</target>

<target name="testNoExplicitPrefixUsedWithoutPrefix" depends="a">
<au:assertEquals expected="bar" actual="${foo}"/>
</target>

<target name="testExplicitPrefixUsedWithoutPrefix" depends="b">
<au:assertEquals expected="baz" actual="${foo}"/>
</target>

<import file="importtests/override.xml"/>

<target name="setProperty">
<property name="prop" value="in including/importing"/>
</target>

<target name="testOverride" depends="override.dummy">
<au:assertEquals expected="in including/importing" actual="${prop}"/>
</target>
</project>

+ 25
- 0
src/tests/antunit/taskdefs/importtests/override.xml View File

@@ -0,0 +1,25 @@
<?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="override">
<target name="setProperty">
<property name="prop" value="in included/imported"/>
</target>

<target name="dummy" depends="setProperty"/>
</project>


+ 41
- 0
src/tests/antunit/taskdefs/include-test.xml View File

@@ -0,0 +1,41 @@
<?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" />

<include file="importtests/a.xml"/>
<include 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>

<include file="importtests/override.xml"/>

<target name="setProperty">
<property name="prop" value="in including/importing"/>
</target>

<target name="testNoOverride" depends="override.dummy">
<au:assertEquals expected="in included/imported" actual="${prop}"/>
</target>
</project>

Loading…
Cancel
Save