2

我有一个文件路径,例如/repo/java/projects/myproj在 Ant 属性中。如何删除/repo/java结果并将其存储projects/myproj在另一个属性中?

4

1 回答 1

4

pathconvertAnt 任务可以与嵌套一起使用以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 回答