Welcome !

Read, comment and forward on your own risk :-)

Thursday, December 4, 2008

Archive old files with ANT

The question was "How to move all but latest five files to an archive folder using ANT?".
Simple, is it not? But the answer was not so easy to get. I was trying avoiding codding this in java or anything else to keep the configuration simple and portable. Reading the ANT manuals was helpful but sometimes disappointing (thanks for the <reverse> example, but what is happened with documentation of <difference>?). It took a while but eventually the solution was found! No contribution java code, no scripts, 'simple' ANT:
<
target name="_archive_old_data" description="Archive all files except the X newest">
<!--
The next part takes a difference (exists in A but don't exists in B)
between two sets and moves it to the destination directory.-->
<
move todir="${arch.dst_dir}" failonerror="yes">
<difference>
<!-- Set with the all files in dir -->
<fileset dir="${arch.org_dir}" />
<!-- Set with the X newest files in dir -->
<first count="${arch.num_keep}">
<sort>
<fileset dir="${arch.org_dir}" />
<reverse xmlns="antlib:org.apache.tools.ant.types.resources.comparators">
<date />
</reverse>
</sort>
</first>
</difference>
<!-- <mapper type="glob" from="*" to="*.bak" /> -->
</move>
</target>

No comments: