我在Ant属性中有一个文件路径,例如/repo/java/projects/myproj
。如何删除/repo/java
并将结果projects/myproj
存储在另一个属性中?
答案 0 :(得分:4)
pathconvert
Ant任务可以与嵌套mapper
一起使用,以从路径中删除前两个目录(或获取前两个目录)。
/repo/java/
<property name="path" value="/repo/java/projects/myproj"/>
<target name="test1">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^/[^/]+/[^/]+/(.*)"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>
<强>输出强>
test1:
[echo] projects/myproj
BUILD SUCCESSFUL
Total time: 0 seconds
<property name="path" value="/repo/java/projects/myproj"/>
<target name="test2">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^${file.separator}[^${file.separator}]+${file.separator}[^${file.separator}]+${file.separator}(.*)"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>
<强>输出强>
test2:
[echo] projects/myproj
BUILD SUCCESSFUL
Total time: 0 seconds
/repo/java/
<property name="path" value="/repo/java/projects/myproj"/>
<target name="test3">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^(/[^/]+/[^/]+/).*"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>
<强>输出强>
test3:
[echo] /repo/java/
BUILD SUCCESSFUL
Total time: 0 seconds
<property name="path" value="/repo/java/projects/myproj"/>
<target name="test4">
<pathconvert property="path.fragment" pathsep="${line.separator}">
<propertyresource name="path" />
<mapper type="regexp"
from="^(${file.separator}[^${file.separator}]+${file.separator}[^${file.separator}]+${file.separator}).*"
to="\1"/>
</pathconvert>
<echo message="${path.fragment}" />
</target>
<强>输出强>
test4:
[echo] /repo/java/
BUILD SUCCESSFUL
Total time: 0 seconds