Great!!! Ive been thinking about doing this for a couple of weeks...I'll look into it...
Mikkel -----Oprindelig meddelelse----- Fra: Stuart Barlow [mailto:[EMAIL PROTECTED] Sendt: 25. oktober 2000 11:49 Til: [EMAIL PROTECTED] Emne: increment task When undertaking nightly builds we have found the need for a build number. And we also wanted this to live in a java propery file so that ANT could make use of it during the build. Below is a simple task that increments a numeric property in a java property file. Thanks, Stuart. /** * Allows a particular property in a Java property file to be incremented by * one. Useful for build numbers that increase by one with each build. * * @author Stuart Barlow <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @author <a href="mailto:[EMAIL PROTECTED]">Stuart Barlow</a> */ public class IncrementProperty extends Task { private File src = null; private String property = null; /** * Do the execution. */ public void execute() throws BuildException { if (property == null) { throw new BuildException("replace token must not be null", location); } log("Incrementing " + property + " in file: " + src); if (src != null) { processFile(src); } } /** * Perform the increment of a property value on the given file. * * The replacement is performed on a temporary file which then replaces the original file. * * @param src the source file */ private void processFile(File src) throws BuildException { if (!src.exists()) { throw new BuildException("Increment: source file " + src.getPath() + " doesn't exist", location); } File temp = new File(src.getPath() + ".temp"); if (temp.exists()) { throw new BuildException("Replace: temporary file " + temp.getPath() + " already exists", location); } Properties propFile = new Properties(); try { // load in the property file FileInputStream in = new FileInputStream(src); propFile.load(in); in.close(); } catch (IOException ioe) { ioe.printStackTrace(); throw new BuildException(ioe, location); } try { // get the old value and set the new one. String strValue = propFile.getProperty(property); long longValue = Long.parseLong(strValue); longValue++; Long newIncrementedValue = new Long(longValue); propFile.setProperty(property, newIncrementedValue.toString()); // write out the property file FileOutputStream out = new FileOutputStream(src); propFile.store(out, "Ant - written out by the task: IncrementProperty"); out.close(); } catch (Exception ex) { ex.printStackTrace(); throw new BuildException(ex, location); } } /** * Set the source file. */ public void setFile(File file) { this.src = file; } /** * Set the string token to replace. */ public void setProperty(String property) { this.property = property; } }
