I am new to ant and i want to know how the ant copy decides the copying order. Problem is, I have a folder structure like this releases/v1,v2,v3
. folders will be created for each new release. and names of the files will be similar in these folders. and it is required to copy the latest release in to a folder called 'latest' in this scenario it will be required to copy v3 into 'latest'. so my logic is to copy one by one into 'latest' folder and override the files and the latest file will be remained at the end. my code is like this.
<copy todir="${srcdist.layout.dir}/etc/wsdl/latest" flatten="true" overwrite="true">
<fileset dir="../../releases">
<include name="**/*.wsdl"/>
</fileset>
</copy>
This code works fine for me (in centOS). it starts copying from v1 and ends in v3. but i feel doubt on this copying order whether this will work every time or not. is copy todir sorting the folder names before copying ? please help me.
I think it's not good to rely on how <fileset>
orders the files when copying. So maybe it's better if you can try to change this logic to use a TimestampSelector from the Ant-Contrib project to select the newest wsdl file and copy only this file, not all the files of all versions. It could be done like:
<timestampselector property="latest.modified">
<path>
<fileset dir="../../releases">
<include name="**/*.wsdl" />
</fileset>
</path>
</timestampselector>
<copy todir="${srcdist.layout.dir}/etc/wsdl/latest" file="${latest.modified}"/>
Or since Ant 1.7.1 you can use a Last to get the latest file. Here you can find an example.
From Path-like Structures in the Ant documentation:
...certain resource collection types such as fileset, dirset and files
are undefined in terms of order.
So, you cannot depend on a fileset maintaining any order.
Instead, you can resort to using JavaScript to find the "latest" release directory:
<!-- match directories starting with "v" under the "releases" directory -->
<dirset id="release.dirs" dir="releases" includes="v*"/>
<script language="javascript">
<![CDATA[
var dirSet = project.getReference( "release.dirs" );
var ds = dirSet.getDirectoryScanner( project );
var includes = ds.getIncludedDirectories();
var greatestVersion = 0;
for ( var i = 0; i < includes.length; i++ )
{
var dirname = includes[i];
// chop off the "v" from the front
var dirVersion = dirname.substr(1);
greatestVersion = Math.max( greatestVersion, dirVersion );
}
project.setProperty( "greatestVersion", greatestVersion );
]]>
</script>
<copy todir="my-out-dir" verbose="true">
<fileset dir="releases/v${greatestVersion}" includes="**/*.wsdl"/>
</copy>
To test the script, I created several v#
directories under a releases
directory. Each of those directories has a *.wsdl
file.
$ ls -R releases/
releases/:
v1 v10 v2 v3
releases/v1:
v1.wsdl
releases/v10:
v10.wsdl
releases/v2:
v2.wsdl
releases/v3:
v3.wsdl
The output of running the script:
run:
...
[copy] Copying releases\v10\v10.wsdl to my-out-dir\v10.wsdl
using sort
<copy todir="${srcdist.layout.dir}/etc/wsdl/latest" flatten="true" overwrite="true">
<sort>
<fileset dir="../../releases">
<include name="**/*.wsdl"/>
</fileset>
</sort>
</copy>