Diane,

I have added this task with a few mods/fixes. My only concern when this
task was originally submitted was that it may create a mini-language. I
think it is small enough at the moment to be manageable but I wouldn't
want to see the set of operations grow.

Who wants to write the documentation? :-)

Conor


> -----Original Message-----
> From: Diane Holt [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, 25 October 2000 21:20
> To: [EMAIL PROTECTED]
> Subject: Re: increment task
>
>
> There's almost already a task for this (<propertyfile>) -- it does
> increments, decrements, gen's a date, puts a comment-block at
> the top --
> but it isn't actually submitted yet. I think Stefan was going
> to take a
> look at it again, but he's off at ApacheCon, so...
>
> Diane
> --- Stuart Barlow <[EMAIL PROTECTED]> wrote:
> >
> > 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;
> >    }
> > }
> >
>
>
> =====
> ([EMAIL PROTECTED])
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Messenger - Talk while you surf!  It's FREE.
> http://im.yahoo.com/
>

Reply via email to