Avoiding the dollar in the middle of the ant property

I have a property whose value contains $. I would like to use this property as a regular expression in propertyregexp. Ant seems to resolve the property as a parameter to propertyregexp, but then the dollar is interpreted as a regular expression character.

Example:

<property name="a" value="abc$" />
<property name="b" value="xyz" />
<path id="paths">
  <pathelement location="abc$/def" />
  <pathelement location="abc$/ghi" />
</path>
<pathconvert property="list" refid="paths" pathsep="${line.separator}" dirsep="/" />
<propertyregex property="list" input="${list}" override="true" regexp="${a}(.*)" replace="${b}\1" />
<echo message="${list}" />

I would like to get a couple xyz/defand xyz/ghi. Is it possible? I am using Ant 1.8.

+3
source share
2 answers

oops somehow I did not read your comment in detail, but, nevertheless, a toy solution works here; -)

<project name="project" default="main">

    <taskdef resource="net/sf/antcontrib/antlib.xml"/>

 <property name="a" value="abc$" />
 <property name="b" value="xyz" />
 <path id="paths">
  <pathelement location="abc$/def" />
  <pathelement location="abc$/ghi" />
 </path>

 <target name="main">

  <pathconvert property="list" refid="paths" pathsep="${line.separator}" dirsep="/" />
  <propertyregex property="a" input="${a}" override="true" regexp="\$" replace="" />
  <propertyregex property="list" input="${list}" override="true" regexp="\$" replace="" />
  <propertyregex property="list" input="${list}" override="true" regexp="${a}" replace="${b}" />
  <echo>${list}</echo>
 </target>

</project>

result:

main:
     [echo] /foobar/AntScripts/xyz/def
     [echo] /foobar/AntScripts/xyz/ghi
BUILD SUCCESSFUL

IMO, using properties with '$' in it, is causing trouble, is there no other way ?!

+1

:

<property name="a" value="abc$" /> 
<property name="b" value="xyz" /> 
<path id="paths"> 
  <pathelement location="abc$/def" /> 
  <pathelement location="abc$/ghi" /> 
</path> 
<pathconvert property="list" refid="paths" pathsep="${line.separator}" dirsep="/" /> 
<propertyregex property="a.escaped" input="${a}" regexp="\$$" replace="\\\\$$" global="true" />
<propertyregex property="list" input="${list}" override="true" regexp="${a.escaped}(.*)" replace="${b}\1" /> 
<echo message="${list}" /> 
0

Source: https://habr.com/ru/post/1740953/


All Articles