给定包含通配符(如C:\Program Files\VC\Redist\x64\Microsoft.*.CRT\*.dll )的绝对路径,如何将此路径解析为FileSet
假设路径存储在属性myPath中,该属性由用户提供,可能包含空格,并且可能位于文件系统的任何位置。我需要一些类似以下内容的东西:
<fileset>
<include name="${myPath}" />
</fileset>当然,这是不起作用的,因为fileset需要dir参数-但是,我没有可以提供的基本dir。这怎么能解决呢?
可用的ant版本为1.10.5。
发布于 2018-11-28 22:30:03
通过使用正则表达式将绝对路径分解为基本路径部分和包含通配符的部分,我解决了这个问题。可用宏:
<macrodef name="resolveWildcardPath">
<!-- resolves a wildcard path to a fileset -->
<attribute name="path" /> <!-- input path -->
<attribute name="filesetID" /> <!-- output fileset ID -->
<sequential>
<local name="normalizedPath" />
<local name="basePath" />
<local name="wildcardPath" />
<pathconvert property="normalizedPath">
<path location="@{path}" />
</pathconvert>
<regexp id="pathWildcardRegex" pattern="([^\*]+)\${file.separator}([^\${file.separator}]*\*.*)" />
<propertyregex input="${normalizedPath}" select="\1" property="basePath">
<regexp refid="pathWildcardRegex"/>
</propertyregex>
<propertyregex input="${normalizedPath}" select="\2" property="wildcardPath">
<regexp refid="pathWildcardRegex"/>
</propertyregex>
<fileset id="@{filesetID}" dir="${basePath}" if:set="wildcardPath">
<include name="${wildcardPath}" />
</fileset>
<fileset id="@{filesetID}" file="${normalizedPath}" unless:set="wildcardPath" />
</sequential>
</macrodef>请注意,此解决方案还需要antcont肋骨和if/unless (xmlns:if="ant:if" xmlns:unless="ant:unless"作为project参数)。
https://stackoverflow.com/questions/53526663
复制相似问题