Search code examples
ant

How can I extract version from a file name in Ant without using ant contrib?


I am trying to scan a folder in ant script and extract the version out of it, suppose file name is abc-1.0.0.exe/dmg. I want to get the 1.0.0 in Ant. Is there any way I can do without using ant contrib? I only found help with ant contrib which I don't want to use.


Solution

  • You might be able to use something like this, based on the <pathconvert> task with a regexmapper.

    Here's the directory structure in this example:

    $ find folder
    folder
    folder/abc-1.0.0.exe
    folder/abc-1.0.0.exe/dmg
    

    Here's the Ant buildfile:

    <fileset dir="folder" id="folder"/>
    
    <echo message="file is: ${toString:folder}" />
    
    <pathconvert property="version">
        <path path="${toString:folder}" />
        <regexpmapper from=".*-(.*).exe.*" to="\1" />
    </pathconvert>
    
    <echo message="version is: ${version}" />
    

    Output:

     [echo] file is: abc-1.0.0.exe/dmg
     [echo] version is: 1.0.0