代码之家  ›  专栏  ›  技术社区  ›  Shaggy Frog

使用Ant迭代目录

  •  6
  • Shaggy Frog  · 技术社区  · 15 年前

    假设我有一个包含以下路径的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> 任务, <fileset> S和 <mapper> S,但我很难把它们组合起来。

    我试过这样的方法:

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

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

    如果我只能使用自定义任务来完成这项工作,我想我可以写一个,但我希望我可以把现有的组件插在一起。

    2 回复  |  直到 15 年前
        1
  •  6
  •   martin clayton egrunin    15 年前

    在顺序任务中,您可以使用 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 .

        2
  •  2
  •   Shaggy Frog    15 年前

    为了完整起见,下面是我根据马丁·克莱顿的回答为一个目标想出的办法。它目前只在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>
    
    推荐文章