使用Ant遍历目录
假设我有一个PDF文件集合,其路径如下:使用Ant遍历目录,ant,fileset,Ant,Fileset,假设我有一个PDF文件集合,其路径如下: /some/path/pdfs/birds/duck.pdf /some/path/pdfs/birds/goose.pdf /some/path/pdfs/insects/fly.pdf /some/path/pdfs/insects/mosquito.pdf 我想做的是为每个PDF生成符合相对路径结构的缩略图,并输出到另一个位置,即: /another/path/thumbnails/birds/duck.png /another/path/thu
/some/path/pdfs/birds/duck.pdf
/some/path/pdfs/birds/goose.pdf
/some/path/pdfs/insects/fly.pdf
/some/path/pdfs/insects/mosquito.pdf
我想做的是为每个PDF生成符合相对路径结构的缩略图,并输出到另一个位置,即:
/another/path/thumbnails/birds/duck.png
/another/path/thumbnails/birds/goose.png
/another/path/thumbnails/insects/fly.png
/another/path/thumbnails/insects/mosquito.png
我想在Ant中完成这项工作。假设我将在命令行上使用Ghostscript,并且我已经完成了对GS的调用:
<exec executable="${ghostscript.executable.name}">
<arg value="-q"/>
<arg value="-r72"/>
<arg value="-sDEVICE=png16m"/>
<arg value="-sOutputFile=${thumbnail.image.path}"/>
<arg value="${input.pdf.path}"/>
</exec>
但不幸的是,@{file}
属性是一个绝对路径,我找不到任何简单的方法将其分解为相关组件
如果我只能使用自定义任务来实现这一点,我想我可以编写一个,但我希望我可以将现有组件插入到一起。在顺序任务中,您可以使用该任务将输入路径映射到输出。例如:
<propertyregex override="yes" property="outfile" input="@{file}"
regexp="/some/path/pdfs/(.*).pdf"
replace="/another/path/\1.png" />
例如,它将
/some/path/pdfs/birds/duck.pdf
映射到/other/path/birds/duck.png
为了完整起见,以下是我根据martin clayton的答案提出的目标。它目前只能在Windows上运行,但这是因为我还没有在Mac OS X上以非代理方式安装Ghostscript。请注意,要成为一个跨平台的解决方案,我必须“擦洗”文件分隔符,使其始终只使用正斜杠
<target name="make-thumbnails" depends="">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="/path/to/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<condition property="ghostscript.executable.name" value="/path/to/gswin32c.exe">
<os family="windows"/>
</condition>
<condition property="ghostscript.executable.name" value="">
<os family="mac"/>
</condition>
<for param="file">
<path>
<fileset dir="/path/to/pdfs">
<include name="**/*.pdf"/>
</fileset>
</path>
<sequential>
<propertyregex override="yes" property="file-scrubbed" input="@{file}"
regexp="\\"
replace="/" />
<propertyregex override="yes" property="output-path-directory-fragment" input="${file-scrubbed}"
regexp=".*/pdfs/(.*)/.+\.pdf"
replace="\1" />
<propertyregex override="yes" property="output-path-file-fragment" input="${file-scrubbed}"
regexp=".*/pdfs.*/(.+)\.pdf"
replace="\1.png" />
<mkdir dir="${thumbnails.base.dir}/${output-path-directory-fragment}"/>
<exec executable="${ghostscript.executable.name}">
<arg value="-q"/>
<arg value="-dLastPage=1"/>
<arg value="-dNOPAUSE"/>
<arg value="-dBATCH"/>
<arg value="-dSAFER"/>
<arg value="-r72"/>
<arg value="-sDEVICE=png16m"/>
<arg value="-sOutputFile=${thumbnails.base.dir}/${output-path-directory-fragment}/${output-path-file-fragment}"/>
<arg value="${file-scrubbed}"/>
</exec>
</sequential>
</for>
</target>
这看起来很有趣。你能稍微扩展一下你的代码来展示我是如何使用它的吗?i、 e.我是否将其与任务一起使用,如果是,它是否会进入?您能否解释一下最后2个propertyregex
做了什么,并没有真正得到替换?也许一些输入和预期输出示例将帮助您的属性regex在这里救了我的命……尽管我想指出,“override”属性接受“true”或“false”。干杯
<target name="make-thumbnails" depends="">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="/path/to/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<condition property="ghostscript.executable.name" value="/path/to/gswin32c.exe">
<os family="windows"/>
</condition>
<condition property="ghostscript.executable.name" value="">
<os family="mac"/>
</condition>
<for param="file">
<path>
<fileset dir="/path/to/pdfs">
<include name="**/*.pdf"/>
</fileset>
</path>
<sequential>
<propertyregex override="yes" property="file-scrubbed" input="@{file}"
regexp="\\"
replace="/" />
<propertyregex override="yes" property="output-path-directory-fragment" input="${file-scrubbed}"
regexp=".*/pdfs/(.*)/.+\.pdf"
replace="\1" />
<propertyregex override="yes" property="output-path-file-fragment" input="${file-scrubbed}"
regexp=".*/pdfs.*/(.+)\.pdf"
replace="\1.png" />
<mkdir dir="${thumbnails.base.dir}/${output-path-directory-fragment}"/>
<exec executable="${ghostscript.executable.name}">
<arg value="-q"/>
<arg value="-dLastPage=1"/>
<arg value="-dNOPAUSE"/>
<arg value="-dBATCH"/>
<arg value="-dSAFER"/>
<arg value="-r72"/>
<arg value="-sDEVICE=png16m"/>
<arg value="-sOutputFile=${thumbnails.base.dir}/${output-path-directory-fragment}/${output-path-file-fragment}"/>
<arg value="${file-scrubbed}"/>
</exec>
</sequential>
</for>
</target>