how to escape backslash in ant

醉酒当歌 提交于 2019-12-11 17:16:49

问题


I am writing Ant scripts.

I have a property which has the value: "C\:Program Files\\test1\\test2"

Is there a method in Ant to convert it to: C:Program Files\test1\test2


回答1:


You could use : http://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html

although I am not sure if this will do what you are asking for. Are the backslashes visible when you echo your property?

In any case to use the above task you will have to have ant-contrib installed and simply write a task like this :

<project name="test" default="build">
<!--Needed for antcontrib-->
<taskdef resource="net/sf/antcontrib/antlib.xml"/>

<target name="build">

    <property name="path" value="C\:Program Files\\test1\\test2"/>
    <echo message="Path with slashes : ${path}"/>
    <propertyregex  property="removed.backslash.property"
                input="${path}"
                global="true"
                regexp="\\(\\|:)"
                replace="\1"
    />
    <echo message="Path with single slashes : ${removed.backslash.property}"/>
</target>

</project>

Output :

build:
 [echo] Path with slashes : C\:Program Files\\test1\\test2
 [echo] Path with single slashes : C:Program Files\test1\test2

In addition you could use any of the BSF languages :

http://ant.apache.org/manual/Tasks/script.html

provided you are using jre 1.6 and above.



来源:https://stackoverflow.com/questions/7655609/how-to-escape-backslash-in-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!