view src/org/ssdt_ohio/tools/ant/ReplaceTargetTask.java @ 6:c989b9aa8820

USASR-644: Add ant task to replace existing ant targets
author smith@nwoca.org
date Wed, 12 Oct 2011 12:59:32 -0400
parents src/org/ssdt_ohio/tools/ant/AddDependencyTask.java@de1522a9d4bc
children 418ba4cfc553
line wrap: on
line source
package org.ssdt_ohio.tools.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;

/** @author smith */
public class ReplaceTargetTask extends Task {

    private String target;
    private String with;

    @Override
    public void execute() throws BuildException {
        if (target == null) {
            throw new BuildException("'target' attribute is required");
        }
        if (with == null) {
            throw new BuildException("'with' attribute is required");
        }

        Target t = (Target) getProject().getTargets().get(target);
        if (t == null) {
            log(target + " to be replaced does not exist", Project.MSG_VERBOSE);
        } else {
            Target withTarget = (Target) getProject().getTargets().get(with);
            if (withTarget == null) {
                log(with + " replacement target does not exist", Project.MSG_ERR);
            } else {
                Target newTarget = new Target(withTarget);
                newTarget.setName(target);
                getProject().addOrReplaceTarget(newTarget);
                
                log(target + " target replaced by " + with, Project.MSG_INFO);
            }
        }
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public void setWith(String with) {
        this.with = with;
    }
}