使用 Ant 遍历目录

发布于 2024-09-02 02:14:14 字数 1621 浏览 3 评论 0原文

假设我有一个具有以下路径的 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/thumbnails/birds/goose.png
/another/path/thumbnails/insects/fly.png
/another/path/thumbnails/insects/mosquito.png

我希望完成此操作在蚂蚁.假设我要在命令行上使用 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>

所以我需要做的是计算出 ${thumbnail.image.path} 和 ${input.pdf.path} 在遍历 PDF 输入目录时。

我可以访问 ant-contrib(刚刚安装了“最新”版本,即 1.0b3)并且我正在使用 Ant 1.8.0。我想我可以使用 任务、 来完成一些工作,但是我我很难把它们放在一起。

我尝试了类似的方法:

    <for param="file">
        <path>
            <fileset dir="${some.dir.path}/pdfs">
                <include name="**/*.pdf"/>
            </fileset>
        </path>
        <sequential>
            <echo message="@{file}"/>
        </sequential>
    </for>

但不幸的是 @{file} 属性是绝对路径,我找不到任何简单的方法将其分解为相关组件。

如果我只能使用自定义任务来完成此操作,我想我可以编写一个,但我希望我可以将现有组件插入在一起。

Let's say I have a collection of PDF files with the following paths:

/some/path/pdfs/birds/duck.pdf
/some/path/pdfs/birds/goose.pdf
/some/path/pdfs/insects/fly.pdf
/some/path/pdfs/insects/mosquito.pdf

What I'd like to do is generate thumbnails for each PDF that respect the relative path structure, and output to another location, i.e.:

/another/path/thumbnails/birds/duck.png
/another/path/thumbnails/birds/goose.png
/another/path/thumbnails/insects/fly.png
/another/path/thumbnails/insects/mosquito.png

I'd like this to be done in Ant. Assume I'm going to use Ghostscript on the command line and I've already worked out the call to 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>

So what I need to do is work out the correct values for ${thumbnail.image.path} and ${input.pdf.path} while traversing the PDF input directory.

I have access to ant-contrib (just installed the "latest", which is 1.0b3) and I'm using Ant 1.8.0. I think I can make something work using the <for> task, <fileset>s and <mapper>s, but I am having trouble putting it all together.

I tried something like:

    <for param="file">
        <path>
            <fileset dir="${some.dir.path}/pdfs">
                <include name="**/*.pdf"/>
            </fileset>
        </path>
        <sequential>
            <echo message="@{file}"/>
        </sequential>
    </for>

But unfortunately the @{file} property is an absolute path, and I can't find any simple way of decomposing it into the relative components.

If I can only do this using a custom task, I guess I could write one, but I'm hoping I can just plug together existing components.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-09-09 02:14:14

在连续任务中,您可以使用 ant-contrib propertyregex 将输入路径映射到输出的任务。例如:

<propertyregex override="yes" property="outfile" input="@{file}"
               regexp="/some/path/pdfs/(.*).pdf"
               replace="/another/path/\1.png" />

哪个映射,例如 /some/path/pdfs/birds/duck.pdf/another/path/birds/duck.png

In the sequential task you could may be able to use the ant-contrib propertyregex task to map the input paths to output. For example:

<propertyregex override="yes" property="outfile" input="@{file}"
               regexp="/some/path/pdfs/(.*).pdf"
               replace="/another/path/\1.png" />

Which maps, for example /some/path/pdfs/birds/duck.pdf to /another/path/birds/duck.png.

追我者格杀勿论 2024-09-09 02:14:14

为了完整起见,以下是我根据马丁·克莱顿的答案提出的目标。它目前仅适用于 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>

For the sake of completeness, here's what I came up with for a target based on martin clayton's answer. It only works on Windows for now but that's because I haven't gotten around to installing Ghostscript in a non-proxy way yet on Mac OS X. Note that to be a cross-platform solution I had to "scrub" the file separators to be consistently forward-slash only.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文