Return-Path: Delivered-To: apmail-ant-dev-archive@www.apache.org Received: (qmail 51973 invoked from network); 28 Mar 2004 18:26:59 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 28 Mar 2004 18:26:59 -0000 Received: (qmail 58920 invoked by uid 500); 28 Mar 2004 18:26:51 -0000 Delivered-To: apmail-ant-dev-archive@ant.apache.org Received: (qmail 58578 invoked by uid 500); 28 Mar 2004 18:26:49 -0000 Mailing-List: contact dev-help@ant.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Ant Developers List" Reply-To: "Ant Developers List" Delivered-To: mailing list dev@ant.apache.org Received: (qmail 58563 invoked from network); 28 Mar 2004 18:26:48 -0000 Received: from unknown (HELO mail.canoo.com) (212.232.176.82) by daedalus.apache.org with SMTP; 28 Mar 2004 18:26:48 -0000 Received: from etienne (cable-ggar54-024.intergga.ch [157.161.54.24]) by mail.canoo.com (Postfix) with ESMTP id 5DA0657DBE for ; Sun, 28 Mar 2004 20:15:14 +0200 (CEST) From: "etienne studer" To: Subject: Ant task: PropertyFileIterator Date: Sun, 28 Mar 2004 20:15:10 +0200 Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0000_01C41501.5EE39420" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N ------=_NextPart_000_0000_01C41501.5EE39420 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit 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); } } ------=_NextPart_000_0000_01C41501.5EE39420 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org For additional commands, e-mail: dev-help@ant.apache.org ------=_NextPart_000_0000_01C41501.5EE39420--