I’m quite a newbie in Ant, but I thought it would be straightforward to do a regular expression replacement in a string contained in a property; in my case, I had to replace Windows backslashes with Unix slashes… apparently, unless you use thepropertyregextask fromAnt Contrib,that is not supported out-of-the-box: you have to pass through a file!
Thisstackoverflow postshows a possible solution, and starting from that, I created a macrodef to make it easier
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!-- = = = = = = = = = = = = = = = = = macrodef: replace_win_slashes = = = = = = = = = = = = = = = = = --> <macrodefname="replace_win_slashes"> <attributename="property.to.process"default="default"/> <attributename="output.property"default="default"/> <sequential> <echomessage="@{property.to.process}"file="some.tmp.file"/> <loadfileproperty="@{output.property}"srcFile="some.tmp.file"> <filterchain> <tokenfilter> <replaceregexpattern="\\" replace="/" flags="g" /> </tokenfilter> </filterchain> </loadfile> <delete file="some.tmp.file"/> </sequential> </macrodef> |
This macro takes the property value to process (property.to.process) and the property name where to store the result; it outputs the input value to a temporary file, reads it back with a regular expression replacement (which is supported for files) and store it in the specified property (the temporary file is then deleted).
Here’s an example of use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <projectname="replace win slashes"> <propertyname="my.prop1"value="file:C:\Users\bettini/my/path1"/> <!-- = = = = = = = = = = = = = = = = = macrodef: replace_win_slashes = = = = = = = = = = = = = = = = = --> <macrodefname="replace_win_slashes"> <attributename="property.to.process"default="default"/> <attributename="output.property"default="default"/> <sequential> <echomessage="@{property.to.process}"file="some.tmp.file"/> <loadfileproperty="@{output.property}"srcFile="some.tmp.file"> <filterchain> <tokenfilter> <replaceregexpattern="\\" replace="/" flags="g" /> </tokenfilter> </filterchain> </loadfile> <delete file="some.tmp.file" /> </sequential> </macrodef> <replace_win_slashes property.to.process="${my.prop1}" output.property="my-prop-1" /> <echo message="my-prop-1=${my-prop-1}"/> </project> |








