I needed something similar. Both of our requirements requires the age of a
file/directory to be determined. Therefore, the easiest approach I found was
to write my own ANT condition. By writing a condition, you can use it inside
an if statement or whatever else in ANT that supports conditions. Here is
source code. I am sure it can be improved.
import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.condition.Condition;
public class FileAgeCondition implements Condition {
protected String _file = null;
protected float _age = -999.25F; //In seconds
protected boolean _olderThan = true;
public boolean eval() throws BuildException
{
if((_file == null) || (_file.length() == 0))
throw new BuildException("file must be set");
if(_age == -999.25F)
throw new BuildException("age must be set");
File file = new File(_file);
if(!file.exists())
return(true);
long now = System.currentTimeMillis();
long fileAge = file.lastModified();
float testAge = _age * 1000.0F;
boolean retVal;
if(_olderThan)
retVal = (now - fileAge) > testAge;
else
retVal = (now - fileAge) < testAge;
return(retVal);
}
public void setFile(String file)
{
_file = file;
}
public void setAge(float age)
{
_age = age;
}
}
---
Shawn Castrianni
-----Original Message-----
From: Sascha Ernst [mailto:[email protected]]
Sent: Tuesday, June 16, 2009 10:58 AM
To: [email protected]
Subject: delete directory structures older than X days
Hello,
I googled the mailing list archive and found a matching solution based on
javascript for ant 1.5.
I'm using ant 1.7.1 and tried this to delete NBS-folders (NightlyBuildSystem)
older than a specific amount of time.
<target name="delete.old.stuff"
depends="init"
description="delete all NBS stuff which is older than a specific
amount of time">
<!-- start the timer -->
<stopwatch name="timer.delete.old.stuff"/>
<!-- first calculate the borderline timestamp -->
<tstamp>
<format property="borderline.timestamp" pattern="MM/dd/yyyy HH:mm aa"
offset="${deletion.threadshold}" unit="day"/>
</tstamp>
<echo message="borderline.timestamp=${borderline.timestamp}" />
<!-- now delete all NBS checkouts and stuff older than that boderline
timestamp -->
<echo message="delete ${NBS.dir.prefix}** in ${nbsdir} before
${borderline.timestamp}" />
<delete includeemptydirs="true" verbose="true">
<dirset dir="${nbsdir}" includes="${NBS.dir.prefix}**">
<date datetime="${borderline.timestamp}" when="before"
checkdirs="true"/>
</dirset>
</delete>
<!-- now determine the artifact folder -->
<property name="artifact.dir"
value="${builddir}/CruiseControl/artifacts/${projectname}" />
<echo message="artifact.dir=${artifact.dir}" />
<!-- now delete all artifacts and stuff older than that boderline
timestamp -->
<echo message="delete **/**/* in ${artifact.dir} before
${borderline.timestamp}" />
<delete includeemptydirs="true" verbose="true">
<dirset dir="${artifact.dir}" includes="**/**/*">
<date datetime="${borderline.timestamp}" when="before"
checkdirs="true"/>
</dirset>
</delete>
<!-- print out timer and reset it -->
<stopwatch name="timer.delete.old.stuff" action="total"/>
<echo message="DONE : delete.old.stuff" />
</target>
Unfortunately this construction does not delete anything :-(
The console says this:
delete.old.stuff:
[echo] borderline.timestamp=06/06/2009 17:32 PM
[echo] delete NBS-run** in F:/NightlyBuildSystem before 06/06/2009 17:32 PM
[echo]
artifact.dir=F:\NightlyBuildSystem/CruiseControl/artifacts/Mailminder NBS
[echo] delete **/**/* in
F:\NightlyBuildSystem/CruiseControl/artifacts/Mailminder NBS before 06/06/2009
17:32 PM
[stopwatch] [timer.delete.old.stuff: 0.844 sec]
[echo] DONE : delete.old.stuff
Is there something I'm doing completely wrong?
Is there any possibility to delete such old stuff without using javascript
anyway?
Thanks for your time and your help
Regards
///Sascha Ernst///
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
----------------------------------------------------------------------
This e-mail, including any attached files, may contain confidential and
privileged information for the sole use of the intended recipient. Any review,
use, distribution, or disclosure by others is strictly prohibited. If you are
not the intended recipient (or authorized to receive information for the
intended recipient), please contact the sender by reply e-mail and delete all
copies of this message.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]