diff --git a/docs/manual/using.html b/docs/manual/using.html
index a4cf01699..462d5b09c 100644
--- a/docs/manual/using.html
+++ b/docs/manual/using.html
@@ -30,7 +30,7 @@ to be unique. (For additional information, see the
basedir |
@@ -284,11 +284,11 @@ ant.java.version the JVM version Ant detected; currently it can hold
</project>
-Notice that we are declaring properties outside any target. The
-<property>,<typedef> and <taskdef>
-tasks are special in that they can be declared outside any target. When you
-do this they are evaluated before any targets are executed. No other tasks
-can be declared outside targets.
+Notice that we are declaring properties outside any target. As of
+Ant 1.6 all tasks can be declared outside targets (earlier version
+only allowed <property>,<typedef> and
+<taskdef>). When you do this they are evaluated before
+any targets are executed.
We have given some targets descriptions; this causes the projecthelp
diff --git a/src/etc/testcases/core/topleveltasks/notarget.xml b/src/etc/testcases/core/topleveltasks/notarget.xml
new file mode 100644
index 000000000..35ef87355
--- /dev/null
+++ b/src/etc/testcases/core/topleveltasks/notarget.xml
@@ -0,0 +1,3 @@
+
+
+
diff --git a/src/etc/testcases/core/topleveltasks/targetlevelant.xml b/src/etc/testcases/core/topleveltasks/targetlevelant.xml
new file mode 100644
index 000000000..adf7b3a74
--- /dev/null
+++ b/src/etc/testcases/core/topleveltasks/targetlevelant.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
diff --git a/src/etc/testcases/core/topleveltasks/toplevelant.xml b/src/etc/testcases/core/topleveltasks/toplevelant.xml
new file mode 100644
index 000000000..427eddbc0
--- /dev/null
+++ b/src/etc/testcases/core/topleveltasks/toplevelant.xml
@@ -0,0 +1,3 @@
+
+
+
diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java
index 1b8d3f850..768d27802 100644
--- a/src/main/org/apache/tools/ant/Main.java
+++ b/src/main/org/apache/tools/ant/Main.java
@@ -604,7 +604,7 @@ public class Main {
}
// make sure that we have a target to execute
- if (targets.size() == 0) {
+ if (targets.size() == 0 && project.getDefaultTarget() != null) {
targets.addElement(project.getDefaultTarget());
}
diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java b/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java
index 81fea650d..7fbc1a0e5 100644
--- a/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java
+++ b/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java
@@ -451,11 +451,7 @@ public class ProjectHelperImpl extends ProjectHelper {
}
}
- if (def == null) {
- throw new SAXParseException("The default attribute of project "
- + "is required",
- helperImpl.locator);
- } else {
+ if (def != null) {
helperImpl.project.setDefaultTarget(def);
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java
index f2f0a2e11..82406594f 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Ant.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java
@@ -356,19 +356,21 @@ public class Ant extends Task {
target = newProject.getDefaultTarget();
}
- addReferences();
-
- // Are we trying to call the target in which we are defined?
+ // 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(project.getBaseDir()) &&
- newProject.getProperty("ant.file").equals(project.getProperty("ant.file")) &&
- getOwningTarget() != null &&
- target.equals(this.getOwningTarget().getName())) {
-
+ newProject.getProperty("ant.file").equals(project.getProperty("ant.file"))
+ && (getOwningTarget() == null ||
+ getOwningTarget().getName().equals(target))) {
throw new BuildException("ant task calling its own parent "
- + "target");
+ + "target");
}
- newProject.executeTarget(target);
+ addReferences();
+
+ if (target != null) {
+ newProject.executeTarget(target);
+ }
} finally {
// help the gc
newProject = null;
diff --git a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
index 589876b88..ea068569f 100644
--- a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
+++ b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
@@ -196,13 +196,14 @@ public class AntStructure extends Task {
out.println("");
- out.print("");
out.println("");
out.println("");
}
diff --git a/src/testcases/org/apache/tools/ant/TopLevelTaskTest.java b/src/testcases/org/apache/tools/ant/TopLevelTaskTest.java
new file mode 100644
index 000000000..f9cf39b34
--- /dev/null
+++ b/src/testcases/org/apache/tools/ant/TopLevelTaskTest.java
@@ -0,0 +1,83 @@
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2002 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 "The Jakarta Project", "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
+ * .
+ */
+
+package org.apache.tools.ant;
+
+/**
+ * Tests for builds with tasks at the top level
+ *
+ * @author Stefan Bodewig
+ * @since Ant 1.6
+ */
+public class TopLevelTaskTest extends BuildFileTest {
+
+ public TopLevelTaskTest(String name) {
+ super(name);
+ }
+
+ public void testNoTarget() {
+ configureProject("src/etc/testcases/core/topleveltasks/notarget.xml");
+ assertEquals("Called", getLog());
+ }
+
+ public void testCalledFromTopLevelAnt() {
+ configureProject("src/etc/testcases/core/topleveltasks/toplevelant.xml");
+ assertEquals("Called", getLog());
+ }
+
+ public void testCalledFromTargetLevelAnt() {
+ configureProject("src/etc/testcases/core/topleveltasks/targetlevelant.xml");
+ expectLog("foo", "Called");
+ }
+}