duncan 00/12/11 03:10:17
Modified: proposal/anteater/source main.ant
proposal/anteater/source/main/org/apache/ant Project.java
TaskManager.java
proposal/anteater/source/main/org/apache/ant/cli
CLIFrontEnd.java
Added: proposal/anteater/source/coretasks/buildtarget
taskdef.properties
proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget
BuildTargetTask.java
Log:
A small task example that illustrates something... A long time ago I
argued against "if" logic as part of the defined part of Ant stating
that all logic really should go into tasks. Now that the object model
is clean enough, it becomes silly obvious how to do this without requiring
the addition of any if/unless attributes in the target definitions themselves.
The build target task takes a target name and, optionally a if property. If
the property is used and is "true", then the target is executed by calling
the appropriate functionality on the Project object.
Revision Changes Path
1.3 +2 -1 jakarta-ant/proposal/anteater/source/main.ant
Index: main.ant
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/anteater/source/main.ant,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- main.ant 2000/12/11 10:31:59 1.2
+++ main.ant 2000/12/11 11:10:05 1.3
@@ -8,10 +8,11 @@
<description>Primary buildfile for building Ant itself</description>
- <property name="foo" value="bar"/>
+ <property name="foo" value="true"/>
<target name="default" depends="main">
<echo text="Default Target is Executing"/>
+ <buildtarget target="main" if="foo"/>
</target>
<target name="main">
1.1
jakarta-ant/proposal/anteater/source/coretasks/buildtarget/taskdef.properties
Index: taskdef.properties
===================================================================
# taskdef.properties for Echo task
tasks=buildtarget
task.buildtarget.class=org.apache.ant.buildtarget.BuildTargetTask
1.1
jakarta-ant/proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java
Index: BuildTargetTask.java
===================================================================
package org.apache.ant.buildtarget;
import org.apache.ant.*;
/**
* A simple task that builds a target if a property is set to true
*
* @author James Duncan Davidson ([EMAIL PROTECTED])
*/
public class BuildTargetTask extends AbstractTask {
// -----------------------------------------------------------------
// PRIVATE DATA MEMBERS
// -----------------------------------------------------------------
/**
* Data to echo
*/
private String ifProperty;
/**
* Target to execute
*/
private String targetName;
// -----------------------------------------------------------------
// PUBLIC METHODS
// -----------------------------------------------------------------
/**
* Executes this task.
*/
public boolean execute() throws AntException {
// XXX should really check internal state before proceeding! Target
// has to be set...
// XXX oh, and we should really check to see if the target exists
// and fail out if it doesn't. :)
if (ifProperty != null) {
String ifPropertyValue = project.getProperty(ifProperty);
if (ifPropertyValue.equals("true")) {
project.startBuild(targetName);
return true;
} else {
return true;
}
} else {
project.startBuild(targetName);
return true;
}
}
/**
* Sets the property that will be examined
*/
public void setIf(String ifProperty) {
this.ifProperty = ifProperty;
}
/**
* Sets the target to be executed
*/
public void setTarget(String targetName) {
this.targetName = targetName;
}
}
1.4 +1 -0
jakarta-ant/proposal/anteater/source/main/org/apache/ant/Project.java
Index: Project.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/proposal/anteater/source/main/org/apache/ant/Project.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Project.java 2000/12/11 10:32:02 1.3
+++ Project.java 2000/12/11 11:10:12 1.4
@@ -234,6 +234,7 @@
public void startBuild(String targetName) throws AntException {
// notify FrontEnd that we are starting a build on a project
+
frontEnd.notifyProjectStart(this);
Target target = getTarget(targetName);
1.4 +10 -5
jakarta-ant/proposal/anteater/source/main/org/apache/ant/TaskManager.java
Index: TaskManager.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/proposal/anteater/source/main/org/apache/ant/TaskManager.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TaskManager.java 2000/12/11 10:32:04 1.3
+++ TaskManager.java 2000/12/11 11:10:12 1.4
@@ -47,7 +47,6 @@
* Creates a new TaskManager.
*/
TaskManager(FrontEnd frontEnd) {
- System.out.println("CREATING TM");
this.frontEnd = frontEnd;
}
@@ -58,7 +57,7 @@
/**
* Adds a node to the task path
*/
- public void addTaskPathNode(File file) {
+ public void addTaskPathNode(File file) throws AntException {
taskPathNodes.addElement(file);
processTaskPathNode(file);
}
@@ -141,7 +140,7 @@
/**
* Processes a jar file to get class definitions from it
*/
- private void processJar(File file) {
+ private void processJar(File file) throws AntException {
frontEnd.writeMessage("Scanning " + file + " for tasks",
FrontEnd.MSG_LEVEL_LOW);
try {
@@ -155,8 +154,14 @@
Enumeration enum = getTaskNames(props);
while (enum.hasMoreElements()) {
+
String taskName = (String)enum.nextElement();
String taskClass = props.getProperty("task." + taskName
+ ".class");
+ if (taskClass == null) {
+ String msg = "No class definition for task " +
taskName +
+ "in jar file " + file;
+ throw new AntException(msg);
+ }
URLClassLoader loader = new URLClassLoader(new URL[]
{file.toURL()});
try {
Class clazz = loader.loadClass(taskClass);
@@ -183,7 +188,7 @@
* Processes a node of the task path searching for task definitions there
* and adding them to the list of known tasks
*/
- private void processTaskPathNode(File file) {
+ private void processTaskPathNode(File file) throws AntException {
// task path nodes can be any of the following:
// * jar file
@@ -214,7 +219,7 @@
* system directory, and then installation. This allows users or
* system admins to override or add tasks.
*/
- private void setUpTaskPath() {
+ private void setUpTaskPath() throws AntException {
// 1st, add user's home dir.
1.4 +8 -1
jakarta-ant/proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java
Index: CLIFrontEnd.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CLIFrontEnd.java 2000/12/11 10:32:08 1.3
+++ CLIFrontEnd.java 2000/12/11 11:10:16 1.4
@@ -141,7 +141,14 @@
return;
} else {
// XXX need to separate on path seps so that real paths can
be taken
- taskManager.addTaskPathNode(new File(argTaskpath));
+ try {
+ taskManager.addTaskPathNode(new File(argTaskpath));
+ } catch (AntException ae) {
+ System.out.println(ae);
+ System.out.println(ae.getMessage());
+ ae.printStackTrace(System.out);
+ return;
+ }
}
}