Browse Source

Allow target-less build files by

(1) making project's default attribute optional.
(2) don't run executeTarget(s) if not asked for one

Adapt documentation and <antstructure>.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273111 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
1c960e7c14
9 changed files with 117 additions and 24 deletions
  1. +6
    -6
      docs/manual/using.html
  2. +3
    -0
      src/etc/testcases/core/topleveltasks/notarget.xml
  3. +5
    -0
      src/etc/testcases/core/topleveltasks/targetlevelant.xml
  4. +3
    -0
      src/etc/testcases/core/topleveltasks/toplevelant.xml
  5. +1
    -1
      src/main/org/apache/tools/ant/Main.java
  6. +1
    -5
      src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java
  7. +11
    -9
      src/main/org/apache/tools/ant/taskdefs/Ant.java
  8. +4
    -3
      src/main/org/apache/tools/ant/taskdefs/AntStructure.java
  9. +83
    -0
      src/testcases/org/apache/tools/ant/TopLevelTaskTest.java

+ 6
- 6
docs/manual/using.html View File

@@ -30,7 +30,7 @@ to be unique. (For additional information, see the
<tr>
<td valign="top">default</td>
<td valign="top">the default target to use when no target is supplied.</td>
<td align="center" valign="top">Yes.</td>
<td align="center" valign="top">No. No target will be run if omitted.</td>
</tr>
<tr>
<td valign="top">basedir</td>
@@ -284,11 +284,11 @@ ant.java.version the JVM version Ant detected; currently it can hold
&lt;/project&gt;
</pre>

Notice that we are declaring properties outside any target. The
<tt>&lt;property&gt;</tt>,<tt>&lt;typedef&gt;</tt> and <tt>&lt;taskdef&gt;</tt>
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.
<p>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 <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>

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


+ 3
- 0
src/etc/testcases/core/topleveltasks/notarget.xml View File

@@ -0,0 +1,3 @@
<project>
<echo message="Called" />
</project>

+ 5
- 0
src/etc/testcases/core/topleveltasks/targetlevelant.xml View File

@@ -0,0 +1,5 @@
<project basedir="." default="foo">
<target name="foo">
<ant antfile="notarget.xml" />
</target>
</project>

+ 3
- 0
src/etc/testcases/core/topleveltasks/toplevelant.xml View File

@@ -0,0 +1,3 @@
<project basedir=".">
<ant antfile="notarget.xml" />
</project>

+ 1
- 1
src/main/org/apache/tools/ant/Main.java View File

@@ -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());
}



+ 1
- 5
src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java View File

@@ -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);
}



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

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


+ 4
- 3
src/main/org/apache/tools/ant/taskdefs/AntStructure.java View File

@@ -196,13 +196,14 @@ public class AntStructure extends Task {

out.println("");

out.print("<!ELEMENT project (target | property | taskdef");
out.print(" | typedef | ");
out.print("<!ELEMENT project (target | ");
out.print(TASKS);
out.print(" | ");
out.print(TYPES);
out.println(")*>");
out.println("<!ATTLIST project");
out.println(" name CDATA #IMPLIED");
out.println(" default CDATA #REQUIRED");
out.println(" default CDATA #IMPLIED");
out.println(" basedir CDATA #IMPLIED>");
out.println("");
}


+ 83
- 0
src/testcases/org/apache/tools/ant/TopLevelTaskTest.java View File

@@ -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
* <http://www.apache.org/>.
*/

package org.apache.tools.ant;

/**
* Tests for builds with tasks at the top level
*
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @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");
}
}

Loading…
Cancel
Save