views:

298

answers:

3

Hi,

I'm working on an ant script. In this particular part, I need to get the current month, as well as the prior month. I was thinking something similar to

<tstamp>
   <format property="thismonth" pattern="MMyy"/> <!-- 0210 by february 2010-->
</tstamp>

<!--I'd like to get 0110 (january 2010) here, but can't imagine how-->
<property name="priormonth" value="?">

I've been reading on property helpers, but I cant get what I need. Any ideas?

Thanks in advance.

+2  A: 

I'm sure some regex can do wonder but I would simply create a custom Task.

Within your task, you can define a new property with the getProjet().setProperty() method.

Something like the following should suffice:

public class PreviousMonthTask extends Task {

    private String currentDate;
    private String propertyName;

    public void setCurrentDate(String currentDate) {
        this.currentDate = currentDate;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    @Override
    public void execute() throws BuildException {
        // calculate the previous month
        String previousMonth = ...;
        getProject().setProperty(this.propertyName, previousMonth);
    }

}

What's left to do is to define a properties file with a link to the class:

previousmonth = org.myproject.PreviousMonthTask

When you load the task (see the Ant documentation), you just have to invoke your task with:

<previousmonth propertyName="previous" currentDate="${current}"/>
Vladimir
Will take a look at both approaches, thanks.
Tom
I went with the scripting solution beacuse it provides an embedded solution, instead of creating a Java Class in a huge project, but will look at custom tasks for future troublesolving.Thanks.
Tom
+2  A: 

You can do it with a custom JavaScript scriptdef:

<project default="build">

    <target name="build">
        <echo message="Hello world"/>
        <setdates/>
        <echo message="thismonth ${thismonth}"/>
        <echo message="priormonth ${priormonth}"/>
    </target>

    <scriptdef name="setdates" language="javascript">
        <![CDATA[

            importClass(java.text.SimpleDateFormat);
            importClass(java.util.Calendar);

            today = new Date();

            cal = Calendar.getInstance();
            cal.setTime(today);
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);

            priormonth = cal.getTime();

            fmt = new SimpleDateFormat("MMyy");

            self.getProject().setProperty('thismonth', fmt.format(today));
            self.getProject().setProperty('priormonth', fmt.format(priormonth));

        ]]>
    </scriptdef>

</project>
Kevin
I really like this.
Tom
Until now it never occurred to me to use the scriptdef and javascript to simplify task definitions. Thanks!
akr
+1  A: 

ANT's tstamp has an offset (http://ant.apache.org/manual/CoreTasks/tstamp.html)

    <tstamp>
        <format property="twoDaysAgo" pattern="yyyy-MM-dd" offset="-2"/>
    </tstamp>

This returns me a timestamp for two days ago. I would expect you'd be able to do the same thing if your pattern is months, then the offset would probably work in months.

Rob Thornton
Will take a look, thanks.
Tom