Friday, May 28, 2004

Custom Ant Task to Throw BuildException if Specified Ant Property is Not Defined

This Ant task throws a BuildException if a specified Ant property is not defined. I use it like this:

    <target name="generateXlnServerPropertyFile">
        <checkDefinedAntProperty propertyName="classpath.delimiter"/>
        <checkDefinedAntProperty propertyName="velocity.template.directory"/>
        <checkDefinedAntProperty propertyName="xlnserver.property.file"/>
        <generateXlnServerPropertyFile .../>
    </target>
package org.wwre.ant.tasks;

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

/**
 * This class configures the XIS server for WIM use.
 *  
 */
public class CheckDefinedAntPropertyTask extends Task{

    private String propertyName = null;

    /**
     * The method executing the task.
     * 
     * @throws BuildException If srcdir or classname are null. This exception is also thrown 
     * if the variable cannot be found, The java file does not exist,
     */
    public void execute() throws BuildException {

        if (getPropertyName() == null) {
            throw new BuildException("Please set the propertyName attribute.");
        }

        String propertyValue = getProject().getProperty(getPropertyName());

        if (propertyValue == null) {
            throw new BuildException("Please define property[" + getPropertyName() + "].");
        }

    }

    /**
     * @return Returns the propertyName.
     */
    public String getPropertyName() {
        return this.propertyName;
    }

    /**
     * @param _propertyName The propertyName to set.
     */
    public void setPropertyName(String _propertyName) {
        this.propertyName = _propertyName;
    }
}
Post a Comment