how to escape backslash in ant

2019-09-17 03:23发布

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

标签: ant
1条回答
Explosion°爆炸
2楼-- · 2019-09-17 04:17

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.

查看更多
登录 后发表回答