如何在ant脚本中迭代文件?

时间:2013-12-06 21:36:07

标签: bash ant scripting

如何将此bash脚本重写为ant脚本?

#!/bin/bash

# # # First remove files in /removals dir
cd "/removals"
for file in `ls *.jar`
do
  rm -f "/targetdir/${file}"
done

# # # Next add new files from /additions
cd "/additions"
for file in `ls *.jar`
do
  cp -f ${file} /targetdir/${file}
done

我无法在bash中概念化如何在ant中迭代文件。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

Ant使用一个名为filesets的概念来处理文件集合。

以下示例演示了传递给删除和复制任务的文件集。条件删除逻辑由selector处理。

实施例

├── build.xml
└── src
    ├── additions
    │   ├── file1.txt
    │   ├── file2.txt
    │   └── file3.txt
    └── removals
        ├── file1.txt
        └── file3.txt

运行示例

run:
   [delete] Deleting /home/mark/tmp/build/file1.txt
   [delete] Deleting /home/mark/tmp/build/file3.txt
     [copy] Copying 3 files to /home/mark/tmp/build
     [copy] Copying /home/mark/tmp/src/additions/file1.txt to /home/mark/tmp/build/file1.txt
     [copy] Copying /home/mark/tmp/src/additions/file2.txt to /home/mark/tmp/build/file2.txt
     [copy] Copying /home/mark/tmp/src/additions/file3.txt to /home/mark/tmp/build/file3.txt

的build.xml

<project name="demo" default="run">

  <property name="removalsdir"  location="src/removals"/>
  <property name="additionsdir" location="src/additions"/>
  <property name="targetdir"    location="build"/>

  <target name="run">
    <mkdir dir="build"/>

    <delete verbose="true">
      <fileset dir="${targetdir}">
        <present present="both" targetdir="${removalsdir}"/>
      </fileset>
    </delete>

    <copy todir="${targetdir}" verbose="true" overwrite="true">
      <fileset dir="${additionsdir}"/>
    </copy>
  </target>

</project>