Browse Source

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
master
James Duncan Davidson 24 years ago
parent
commit
ac4a6335c4
6 changed files with 92 additions and 7 deletions
  1. +67
    -0
      proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java
  2. +4
    -0
      proposal/anteater/source/coretasks/buildtarget/taskdef.properties
  3. +2
    -1
      proposal/anteater/source/main.ant
  4. +1
    -0
      proposal/anteater/source/main/org/apache/ant/Project.java
  5. +10
    -5
      proposal/anteater/source/main/org/apache/ant/TaskManager.java
  6. +8
    -1
      proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java

+ 67
- 0
proposal/anteater/source/coretasks/buildtarget/org/apache/ant/buildtarget/BuildTargetTask.java View File

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

+ 4
- 0
proposal/anteater/source/coretasks/buildtarget/taskdef.properties View File

@@ -0,0 +1,4 @@
# taskdef.properties for Echo task

tasks=buildtarget
task.buildtarget.class=org.apache.ant.buildtarget.BuildTargetTask

+ 2
- 1
proposal/anteater/source/main.ant View File

@@ -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
- 0
proposal/anteater/source/main/org/apache/ant/Project.java View File

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


+ 10
- 5
proposal/anteater/source/main/org/apache/ant/TaskManager.java View File

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


+ 8
- 1
proposal/anteater/source/main/org/apache/ant/cli/CLIFrontEnd.java View File

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


Loading…
Cancel
Save