FileDocCategorySizeDatePackage
Target.javaAPI DocApache Ant 1.7014919Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant

Target

public class Target extends Object implements TaskContainer
Class to implement a target object with required parameters.

Fields Summary
private String
name
Name of this target.
private String
ifCondition
The "if" condition to test on execution.
private String
unlessCondition
The "unless" condition to test on execution.
private List
dependencies
List of targets this target is dependent on.
private List
children
Children of this target (tasks and data types).
private Location
location
Since Ant 1.6.2
private Project
project
Project this target belongs to.
private String
description
Description of this target, if any.
Constructors Summary
public Target()
Default constructor.


       
      
        //empty
    
public Target(Target other)
Cloning constructor.

param
other the Target to clone.

        this.name = other.name;
        this.ifCondition = other.ifCondition;
        this.unlessCondition = other.unlessCondition;
        this.dependencies = other.dependencies;
        this.location = other.location;
        this.project = other.project;
        this.description = other.description;
        // The children are added to after this cloning
        this.children = other.children;
    
Methods Summary
public voidaddDataType(RuntimeConfigurable r)
Adds the wrapper for a data type element to this target.

param
r The wrapper for the data type element to be added. Must not be null.

        children.add(r);
    
public voidaddDependency(java.lang.String dependency)
Adds a dependency to this target.

param
dependency The name of a target this target is dependent on. Must not be null.

        if (dependencies == null) {
            dependencies = new ArrayList(2);
        }
        dependencies.add(dependency);
    
public voidaddTask(Task task)
Adds a task to this target.

param
task The task to be added. Must not be null.

        children.add(task);
    
public booleandependsOn(java.lang.String other)
Does this target depend on the named target?

param
other the other named target.
return
true if the target does depend on the named target
since
Ant 1.6

        Project p = getProject();
        Hashtable t = (p == null) ? null : p.getTargets();
        return (p != null
                && p.topoSort(getName(), t, false).contains(t.get(other)));
    
public voidexecute()
Executes the target if the "if" and "unless" conditions are satisfied. Dependency checking should be done before calling this method, as it does no checking of its own. If either the "if" or "unless" test prevents this target from being executed, a verbose message is logged giving the reason. It is recommended that clients of this class call performTasks rather than this method so that appropriate build events are fired.

exception
BuildException if any of the tasks fail or if a data type configuration fails.
see
#performTasks()
see
#setIf(String)
see
#setUnless(String)

        if (testIfCondition() && testUnlessCondition()) {
            for (int taskPosition = 0;
                 taskPosition < children.size();
                 ++taskPosition) {
                Object o = children.get(taskPosition);
                if (o instanceof Task) {
                    Task task = (Task) o;
                    task.perform();
                } else {
                    RuntimeConfigurable r = (RuntimeConfigurable) o;
                    r.maybeConfigure(project);
                }
            }
        } else if (!testIfCondition()) {
            project.log(this, "Skipped because property '"
                        + project.replaceProperties(ifCondition)
                        + "' not set.", Project.MSG_VERBOSE);
        } else {
            project.log(this, "Skipped because property '"
                        + project.replaceProperties(unlessCondition)
                        + "' set.", Project.MSG_VERBOSE);
        }
    
public java.util.EnumerationgetDependencies()
Returns an enumeration of the dependencies of this target.

return
an enumeration of the dependencies of this target

        return (dependencies != null ? Collections.enumeration(dependencies)
                                     : new CollectionUtils.EmptyEnumeration());
    
public java.lang.StringgetDescription()
Returns the description of this target.

return
the description of this target, or null if no description is available.

        return description;
    
public java.lang.StringgetIf()
Returns the "if" property condition of this target.

return
the "if" property condition or null if no "if" condition had been defined.
since
1.6.2

        return ("".equals(ifCondition) ? null : ifCondition);
    
public LocationgetLocation()
Get the location of this target's definition.

return
Location
since
1.6.2

        return location;
    
public java.lang.StringgetName()
Returns the name of this target.

return
the name of this target, or null if the name has not been set yet.

        return name;
    
public ProjectgetProject()
Returns the project this target belongs to.

return
The project this target belongs to, or null if the project has not been set yet.

        return project;
    
public Task[]getTasks()
Returns the current set of tasks to be executed by this target.

return
an array of the tasks currently within this target

        List tasks = new ArrayList(children.size());
        Iterator it = children.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            if (o instanceof Task) {
                tasks.add(o);
            }
        }

        return (Task[]) tasks.toArray(new Task[tasks.size()]);
    
public java.lang.StringgetUnless()
Returns the "unless" property condition of this target.

return
the "unless" property condition or null if no "unless" condition had been defined.
since
1.6.2

        return ("".equals(unlessCondition) ? null : unlessCondition);
    
public final voidperformTasks()
Performs the tasks within this target (if the conditions are met), firing target started/target finished messages around a call to execute.

see
#execute()

        RuntimeException thrown = null;
        project.fireTargetStarted(this);
        try {
            execute();
        } catch (RuntimeException exc) {
            thrown = exc;
            throw exc;
        } finally {
            project.fireTargetFinished(this, thrown);
        }
    
voidreplaceChild(Task el, RuntimeConfigurable o)
Replaces all occurrences of the given task in the list of children with the replacement data type wrapper.

param
el The task to replace. Must not be null.
param
o The data type wrapper to replace el with.

        int index;
        while ((index = children.indexOf(el)) >= 0) {
            children.set(index, o);
        }
    
voidreplaceChild(Task el, Task o)
Replaces all occurrences of the given task in the list of children with the replacement task.

param
el The task to replace. Must not be null.
param
o The task to replace el with.

        int index;
        while ((index = children.indexOf(el)) >= 0) {
            children.set(index, o);
        }
    
public voidsetDepends(java.lang.String depS)
Sets the list of targets this target is dependent on. The targets themselves are not resolved at this time.

param
depS A comma-separated list of targets this target depends on. Must not be null.

        if (depS.length() > 0) {
            StringTokenizer tok =
                new StringTokenizer(depS, ",", true);
            while (tok.hasMoreTokens()) {
                String token = tok.nextToken().trim();

                // Make sure the dependency is not empty string
                if ("".equals(token) || ",".equals(token)) {
                    throw new BuildException("Syntax Error: depends "
                        + "attribute of target \"" + getName()
                        + "\" has an empty string as dependency.");
                }

                addDependency(token);

                // Make sure that depends attribute does not
                // end in a ,
                if (tok.hasMoreTokens()) {
                    token = tok.nextToken();
                    if (!tok.hasMoreTokens() || !",".equals(token)) {
                        throw new BuildException("Syntax Error: Depend "
                            + "attribute for target \"" + getName()
                            + "\" ends with a , character");
                    }
                }
            }
        }
    
public voidsetDescription(java.lang.String description)
Sets the description of this target.

param
description The description for this target. May be null, indicating that no description is available.

        this.description = description;
    
public voidsetIf(java.lang.String property)
Sets the "if" condition to test on execution. This is the name of a property to test for existence - if the property is not set, the task will not execute. The property goes through property substitution once before testing, so if property foo has value bar, setting the "if" condition to ${foo}_x will mean that the task will only execute if property bar_x is set.

param
property The property condition to test on execution. May be null, in which case no "if" test is performed.

        ifCondition = (property == null) ? "" : property;
    
public voidsetLocation(Location location)
Sets the location of this target's definition.

param
location Location
since
1.6.2

        this.location = location;
    
public voidsetName(java.lang.String name)
Sets the name of this target.

param
name The name of this target. Should not be null.

        this.name = name;
    
public voidsetProject(Project project)
Sets the project this target belongs to.

param
project The project this target belongs to. Must not be null.

        this.project = project;
    
public voidsetUnless(java.lang.String property)
Sets the "unless" condition to test on execution. This is the name of a property to test for existence - if the property is set, the task will not execute. The property goes through property substitution once before testing, so if property foo has value bar, setting the "unless" condition to ${foo}_x will mean that the task will only execute if property bar_x isn't set.

param
property The property condition to test on execution. May be null, in which case no "unless" test is performed.

        unlessCondition = (property == null) ? "" : property;
    
private booleantestIfCondition()
Tests whether or not the "if" condition is satisfied.

return
whether or not the "if" condition is satisfied. If no condition (or an empty condition) has been set, true is returned.
see
#setIf(String)

        if ("".equals(ifCondition)) {
            return true;
        }

        String test = project.replaceProperties(ifCondition);
        return project.getProperty(test) != null;
    
private booleantestUnlessCondition()
Tests whether or not the "unless" condition is satisfied.

return
whether or not the "unless" condition is satisfied. If no condition (or an empty condition) has been set, true is returned.
see
#setUnless(String)

        if ("".equals(unlessCondition)) {
            return true;
        }
        String test = project.replaceProperties(unlessCondition);
        return project.getProperty(test) == null;
    
public java.lang.StringtoString()
Returns the name of this target.

return
the name of this target, or null if the name has not been set yet.

        return name;