Browse Source

Throw an exception instead of looping forever in top-level <*ant*> task.

PR: 22759


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275176 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
451172e226
8 changed files with 144 additions and 17 deletions
  1. +5
    -2
      WHATSNEW
  2. +6
    -1
      docs/manual/CoreTasks/ant.html
  3. +5
    -2
      docs/manual/CoreTasks/antcall.html
  4. +5
    -0
      docs/manual/CoreTasks/subant.html
  5. +3
    -1
      docs/manual/using.html
  6. +21
    -11
      src/main/org/apache/tools/ant/taskdefs/Ant.java
  7. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/SubAnt.java
  8. +98
    -0
      src/testcases/org/apache/tools/ant/taskdefs/AntLikeTasksAtTopLevelTest.java

+ 5
- 2
WHATSNEW View File

@@ -231,6 +231,11 @@ Fixed bugs:

Other changes:
--------------
* All tasks can be used outside of <target>s. Note that some tasks
will not work at all outside of targets as they would cause infinite
loops (<antcall> as well as <ant> and <subant> if they invoke the
current build file).

* Six new Clearcase tasks added.

* A new filter reader namely tokenfilter has been added. Bugzilla
@@ -258,8 +263,6 @@ Other changes:

* <input> has a new attribute that allows you to specify a default value.

* All tasks can be used outside of <target>s

* Added <image> task (requires JAI).

* <image> task has now proportions attribute in the <scale/> nested element


+ 6
- 1
docs/manual/CoreTasks/ant.html View File

@@ -9,7 +9,12 @@

<h2><a name="ant">Ant</a></h2>
<h3>Description</h3>
<p>Runs Ant on a supplied buildfile. This can be used to build subprojects.</p>

<p>Runs Ant on a supplied buildfile. This can be used to build
subprojects. <strong>This task must no be used outside of a
<code>target</code> if it invoces the same build file it is part
of.</strong></p>

<p>When the <i>antfile</i> attribute is omitted, the file &quot;build.xml&quot;
in the supplied directory (<i>dir</i> attribute) is used.</p>
<p>If no target attribute is supplied, the default target of the new project is


+ 5
- 2
docs/manual/CoreTasks/antcall.html View File

@@ -9,8 +9,11 @@

<h2><a name="antcall">AntCall</a></h2>
<h3>Description</h3>
<p>Call another target within the same build-file optionally specifying some
properties (param's in this context)</p>

<p>Call another target within the same build-file optionally
specifying some properties (param's in this context). <strong>This
task must no be used outside of a <code>target</code>.</strong></p>

<p>By default, all of the properties of the current project will be
available in the new project. Alternatively, you can
set the <i>inheritAll</i> attribute to <code>false</code> and only


+ 5
- 0
docs/manual/CoreTasks/subant.html View File

@@ -53,6 +53,11 @@
<p>
Calls a given target for all defined sub-builds. This is an extension
of ant for bulk project execution.

<strong>This task must no be used outside of a
<code>target</code> if it invoces the same build file it is
part of.</strong>

</p>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<!-- Subsection heading -->


+ 3
- 1
docs/manual/using.html View File

@@ -288,7 +288,9 @@ ant.java.version the JVM version Ant detected; currently it can hold
Ant 1.6 all tasks can be declared outside targets (earlier version
only allowed <tt>&lt;property&gt;</tt>,<tt>&lt;typedef&gt;</tt> and
<tt>&lt;taskdef&gt;</tt>). When you do this they are evaluated before
any targets are executed.</p>
any targets are executed. Some tasks will generate build failures if
they are used outside of targets as they may cause infinite loops
otherwise (<code>&lt;antcall&gt;</code> for example).</p>

<p>
We have given some targets descriptions; this causes the <tt>projecthelp</tt>


+ 21
- 11
src/main/org/apache/tools/ant/taskdefs/Ant.java View File

@@ -357,23 +357,33 @@ public class Ant extends Task {
+ " in build file " + antFile.toString(),
Project.MSG_VERBOSE);
newProject.setUserProperty("ant.file" , antFile);

// Are we trying to call the target in which we are defined (or
// the build file if this is a top level task)?
if (newProject.getProperty("ant.file")
.equals(getProject().getProperty("ant.file"))
&& getOwningTarget() != null) {
if (getOwningTarget().getName().equals("")) {
if (getTaskName().equals("antcall")) {
throw new BuildException("antcall must not be used at"
+ " the top level.");
} else {
throw new BuildException(getTaskName() + " task at the"
+ " top level must not invoke"
+ " its own build file.");
}
} else if (getOwningTarget().getName().equals(target)) {
throw new BuildException(getTaskName() + " task calling "
+ "its own parent target.");
}
}

ProjectHelper.configureProject(newProject, new File(antFile));

if (target == null) {
target = newProject.getDefaultTarget();
}

// Are we trying to call the target in which we are defined (or
// the build file if this is a top level task)?
if (newProject.getBaseDir().equals(getProject().getBaseDir())
&& newProject.getProperty("ant.file").equals(getProject().getProperty("ant.file"))
&& getOwningTarget() != null
&& (getOwningTarget().getName().equals("")
|| getOwningTarget().getName().equals(target))) {
throw new BuildException("ant task calling its own parent "
+ "target");
}

addReferences();

if (target != null) {


+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/SubAnt.java View File

@@ -431,6 +431,7 @@ public class SubAnt
private Ant createAntTask(File directory) {
Ant ant = (Ant) getProject().createTask("ant");
ant.setOwningTarget(getOwningTarget());
ant.setTaskName(getTaskName());
ant.init();
if (target != null && target.length() > 0) {
ant.setTarget(target);


+ 98
- 0
src/testcases/org/apache/tools/ant/taskdefs/AntLikeTasksAtTopLevelTest.java View File

@@ -0,0 +1,98 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Ant" and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileTest;

/**
* @since Ant 1.6
*/
public class AntLikeTasksAtTopLevelTest extends BuildFileTest {
public AntLikeTasksAtTopLevelTest(String name) {
super(name);
}
public void testAnt() {
try {
configureProject("src/etc/testcases/taskdefs/toplevelant.xml");
fail("no exception thrown");
} catch (BuildException e) {
assertEquals("ant task at the top level must not invoke its own"
+ " build file.", e.getMessage());
}
}

public void testSubant() {
try {
configureProject("src/etc/testcases/taskdefs/toplevelsubant.xml");
fail("no exception thrown");
} catch (BuildException e) {
assertEquals("subant task at the top level must not invoke its own"
+ " build file.", e.getMessage());
}
}

public void testAntcall() {
try {
configureProject("src/etc/testcases/taskdefs/toplevelantcall.xml");
fail("no exception thrown");
} catch (BuildException e) {
assertEquals("antcall must not be used at the top level.",
e.getMessage());
}
}

}// AntLikeTasksAtTopLevelTest

Loading…
Cancel
Save