From ac4a6335c45fb97acbc13dee2af925acb7f51b8b Mon Sep 17 00:00:00 2001 From: James Duncan Davidson Date: Mon, 11 Dec 2000 11:10:16 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268340 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/buildtarget/BuildTargetTask.java | 67 +++++++++++++++++++ .../coretasks/buildtarget/taskdef.properties | 4 ++ proposal/anteater/source/main.ant | 3 +- .../source/main/org/apache/ant/Project.java | 1 + .../main/org/apache/ant/TaskManager.java | 15 +++-- .../main/org/apache/ant/cli/CLIFrontEnd.java | 9 ++- 6 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java create mode 100644 proposal/anteater/source/coretasks/buildtarget/taskdef.properties diff --git a/proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java b/proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java new file mode 100644 index 000000000..cead59f17 --- /dev/null +++ b/proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java @@ -0,0 +1,67 @@ +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 (duncan@apache.org) + */ +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; + } +} \ No newline at end of file diff --git a/proposal/anteater/source/coretasks/buildtarget/taskdef.properties b/proposal/anteater/source/coretasks/buildtarget/taskdef.properties new file mode 100644 index 000000000..ecc450666 --- /dev/null +++ b/proposal/anteater/source/coretasks/buildtarget/taskdef.properties @@ -0,0 +1,4 @@ +# taskdef.properties for Echo task + +tasks=buildtarget +task.buildtarget.class=org.apache.ant.buildtarget.BuildTargetTask \ No newline at end of file diff --git a/proposal/anteater/source/main.ant b/proposal/anteater/source/main.ant index cb0b93c44..d56ee636d 100644 --- a/proposal/anteater/source/main.ant +++ b/proposal/anteater/source/main.ant @@ -8,10 +8,11 @@ Primary buildfile for building Ant itself - + + diff --git a/proposal/anteater/source/main/org/apache/ant/Project.java b/proposal/anteater/source/main/org/apache/ant/Project.java index 0a522b8e9..f2160a535 100644 --- a/proposal/anteater/source/main/org/apache/ant/Project.java +++ b/proposal/anteater/source/main/org/apache/ant/Project.java @@ -234,6 +234,7 @@ public class Project { 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); diff --git a/proposal/anteater/source/main/org/apache/ant/TaskManager.java b/proposal/anteater/source/main/org/apache/ant/TaskManager.java index b9552a9e0..5d79c4910 100644 --- a/proposal/anteater/source/main/org/apache/ant/TaskManager.java +++ b/proposal/anteater/source/main/org/apache/ant/TaskManager.java @@ -47,7 +47,6 @@ public class TaskManager { * Creates a new TaskManager. */ TaskManager(FrontEnd frontEnd) { - System.out.println("CREATING TM"); this.frontEnd = frontEnd; } @@ -58,7 +57,7 @@ public class TaskManager { /** * 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 @@ public class TaskManager { /** * 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 @@ public class TaskManager { 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 @@ public class TaskManager { * 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 @@ public class TaskManager { * 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. diff --git a/proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java b/proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java index 53655cfa8..3c6151080 100644 --- a/proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java +++ b/proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java @@ -141,7 +141,14 @@ public class CLIFrontEnd extends FrontEnd { 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; + } } }