我有一个文件路径,例如/repo/java/projects/myproj
在 Ant 属性中。如何删除/repo/java
结果并将其存储projects/myproj
在另一个属性中?
问问题
2662 次
1 回答
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
于 2012-08-23T23:49:49.580 回答