The first part of the task - getting the file names into a "structure" is best done using FileSet - say for SQL scripts in a directory called scripts:
<fileset dir="scripts" includes="*.sql" id="versions" />
This creates an Ant resource collection that can be referenced using id versions . The collection knows about your SQL script files.
Using (as you are told) the regexp display tool, we can convert a set of files into a collection of strings, keeping only parts of the version from file names:
<mappedresources id="versions"> <fileset dir="scripts" includes="*.sql" /> <regexpmapper from="update.*to(.*).sql" to="\1" /> </mappedresources>
In this example, versions now contains a "list" that will be "5,5,6" for your sample files.
Now itβs getting harder because you probably need to do a numerical sorting by what is a list of strings β to avoid sorting 10 as βlessβ than 9. Ant comes with a wired Javascript interpreter, so you can use this to find the maximum. Another option would be to use a numerical sort function that ant-contrib can do .
Here's the "maximum search" in Javascript:
<scriptdef name="numeric_max" language="javascript"> <attribute name="property" /> <attribute name="resources_id" /> <![CDATA[ var iter = project.getReference( attributes.get( "resources_id" ) ).iterator( ); var max_n = 0.0; while ( iter.hasNext() ) { var n = parseFloat( iter.next() ); if ( n > max_n ) max_n = n; } project.setProperty( attributes.get( "property" ), max_n ); ]]> </scriptdef>
This defines a new Ant XML object β numeric_max β that looks like a task and can be used to find the numerical maximum for a set of strings. This is not ideal - there is no string check, and I used float, not ints.
Combining this with the mappedresources above:
<mappedresources id="versions"> <fileset dir="scripts" includes="*.sql" /> <regexpmapper from="update.*to(.*).sql" to="\1" /> </mappedresources> <numeric_max property="latest.version" resources_id="versions" /> <echo message="Latest SQL script version: ${latest.version}." />
When I run this with three files, I get:
[echo] Latest SQL script version: 6.