ANT: load file names and extract data from file names

Hello, I was not sure what to call this question, but I will explain why I am trying to do this.

First of all, I have a folder containing SQL scripts in a specific format, which is updateXtoY.sql, where X and Y are integers. I need to know which Y is the largest number. (mainly to find out what is the last script)

So, if I have 3 files in the "scripts /" folder:

update3to5.sql update2to5.sql update1to6.sql 

the result I need is to set the property "last.version" to 6.

From this point, I can easily run the script. So, the problem I have is 3 times:

1- How to load file names into a data structure.

2- How to iterate over a data structure.

3 How to evaluate the name of each file so that I can extract the "Y" part of the file and get the maximum value. (Now I read the regex)

I am new to ANT and I'm not sure if this is possible and / or possible.

Thanks for any suggestions.

+4
source share
1 answer

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. 
+5
source

All Articles