Set Ant property based on regular expression in file

I have the following in the file

version: [0,1,0] 

and I would like to set the Ant property to a string value of 0.1.0 .

Regular expression

 version:[[:space:]]\[([[:digit:]]),([[:digit:]]),([[:digit:]])\] 

and I need to set the property

 \1.\2.\3 

To obtain

 0.1.0 

I cannot train how to use Ant tasks together for this.

I have Ant -contrib, so you can use these tasks.

+4
source share
3 answers

Solved this using this:

 <loadfile property="burning-boots-js-lib-build.lib-version" srcfile="burning-boots.js"/> <propertyregex property="burning-boots-js-lib-build.lib-version" override="true" input="${burning-boots-js-lib-build.lib-version}" regexp="version:[ \t]\[([0-9]),([0-9]),([0-9])\]" select="\1.\2.\3" /> 

But that seems a little wasteful - it loads the whole file into a property!

If anyone has the best deals, write :)

+4
source

Based on a matte second solution, this worked for me for any (text) file, single line or not. It has no dependencies apache-contrib.

 <loadfile property="version" srcfile="version.txt"> <filterchain> <linecontainsregexp> <regexp pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]"/> </linecontainsregexp> <replaceregex pattern="version:[ \t]\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" /> </filterchain> </loadfile> 
+5
source

Here is a method that ant -contrib does not use using loadproperties and filterchain (note that replaceregex is a "string filter" - see the tokenfilter docs - and not the replaceregexp task ):

 <loadproperties srcFile="version.txt"> <filterchain> <replaceregex pattern="\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" /> </filterchain> </loadproperties> 

Note that the regex is a little different, we treat the file as a properties file.

Alternatively, you can use loadfile with a filterchain , for example, if the file you want to load was not in the format properties.

For example, if the contents of the file were just [0,1,0] , and you wanted to set the version property to 0.1.0 , you could do something like:

 <loadfile srcFile="version.txt" property="version"> <filterchain> <replaceregex pattern="\s+\[([0-9]),([0-9]),([0-9])\]" replace="\1.\2.\3" /> </filterchain> </loadfile> 
+4
source

All Articles