Hi

Attached you can find an ANT task that allows to iterate over a property
file and, for each key/value pair, assigns the values to ANT properties and
then calls any ANT target using the existing "ant" target functionality.

Perhaps this generic task would also be helpful to other people by being an
optional task?

Best regards, Etienne

P.S. I'm new to this list, so please be kind if I broke any ant mailinglist
rules...


package com.canoo;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Iterator;
import java.util.Properties;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Ant;

/**
 * This class reads a property file and calls an ANT target for each
property found. For each call, the property name and
 * the property value are provided as ANT properties.
 *
 * @author Etienne Studer
 */
public class PropertyFileIterator extends Ant {
    private static final String ANT_FILE_PROP = "ant.file";

    private String fAntfile;
    private String fPropertyFile;
    private String fPropertyName;
    private String fPropertyValue;

    public void setAntfile(String antfile) {
        fAntfile = antfile;
    }

    public void setPropertyFile(String propertyFile) {
        fPropertyFile = propertyFile;
    }

    public void setPropertyName(String propertyName) {
        fPropertyName = propertyName;
    }

    public void setPropertyValue(String propertyValue) {
        fPropertyValue = propertyValue;
    }

    /**
     * Run the task that executes an ANT target for each property read from
the property file.
     *
     * @throws org.apache.tools.ant.BuildException
     *
     */
    public void execute() throws BuildException {
        validate();

        Properties props = new Properties();

        InputStream is = null;
        BufferedInputStream bis = null;

        try {
            log(this, "Reading property file: " + fPropertyFile,
Project.MSG_INFO);

            is = new FileInputStream(fPropertyFile);
            bis = new BufferedInputStream(is);

            props.load(bis);
        } catch (Exception e) {
            throw new BuildException("Could not read property file.", e);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }

        for (Iterator iterator = props.keySet().iterator();
iterator.hasNext();) {
            String name = (String) iterator.next();
            String value = props.getProperty(name);

            getProject().setProperty(fPropertyName, name.trim());
            getProject().setProperty(fPropertyValue, value.trim());

            super.setAntfile(fAntfile != null ? fAntfile :
getProject().getProperty(ANT_FILE_PROP));
            super.execute();
        }
    }

    private void validate() {
        if (fPropertyFile == null) {
            throw new BuildException("Property file attribute is
mandatory.");
        }

        if (fPropertyName == null) {
            throw new BuildException("Property name attribute is
mandatory.");
        }

        if (fPropertyValue == null) {
            throw new BuildException("Property value attribute is
mandatory.");
        }
    }

    private void log(Task task, String message, int logLevel) {
        getProject().log(task, message, logLevel);
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to