是否有ANT任务用于查看目录以进行更改?

时间:2010-08-25 22:00:36

标签: ant directory listener watch

这听起来有点牵强,但有一个ANT任务,用于观察目录的变化,然后在目录更改时运行特定的ANT类?

4 个答案:

答案 0 :(得分:5)

如果只能在监视目录中添加或更改文件,则可以使用antcontrib中的这个简单OutOfDate任务。

<property name="watched-dir.flagfile"
  location="MUST be not in watched dir"/>
<outofdate>
  <sourcefiles>
    <fileset dir="watched-dir"/>
  </sourcefiles>
  <targetfiles>
    <pathelement location="${watched-dir.flagfile}"/>        
  </targetfiles>
  <sequential>

    <!--Tasks when something changes go here-->

    <touch file="${watched-dir.flagfile}"/>
  </sequential>
</outofdate>

如果文件可以从监视目录中消失,那么你有更复杂的问题,你可以通过创建监视目录的影子目录结构并检查它是否与watched-dir一致来解决。这个任务比较复杂,但我会给你一个脚本来创建一个影子目录,因为它不是直截了当的:

<property name="TALK" value="true"/>
<property name="shadow-dir"
  location="MUST be not in watched dir"/>

<touch
  mkdirs="true"
  verbose="${TALK}"
>
  <fileset dir="watched-dir">
    <patterns/>
    <type type="file"/>
  </fileset>

  <!-- That's the tricky globmapper to make touch task work -->
  <globmapper from="*" to="${shadow-dir}/*"/>
</touch>

<!--
  Due to how touch task with mapped fileset is implemented, it 
  truncates file access times down to a milliseconds, so if you 
  would have used outofdate task on shadow dir it would always 
  show that something is out of date.

  Because of that, touching all files in ${shadow-dir} again fixes
  that chicken and egg problem.
-->
<touch verbose="${TALK}">
  <fileset dir="${shadow-dir}"/>
</touch>

创建了影子目录后,我将把检查目录一致性的任务留给读者。

答案 1 :(得分:4)

是的,有一个Ant任务可以执行此操作:

https://github.com/chubbard/WatchTask

需要1.7+。它可以监视任意数量的文件集,并根据它来自哪个文件集来调用任何目标。

答案 2 :(得分:1)

您可以使用Waitfor任务来实现您的目标。它会阻塞,直到一个或多个条件(例如特定文件的存在)变为真。

答案 3 :(得分:0)

您可以将apply任务与fileset selector

结合使用
<apply executable="somecommand" parallel="false">
  <srcfile/>
  <fileset dir="${watch.dir}">
    <modified/>
  </fileset>
</apply>

文件集将根据存储的MD5校验和检查文件是否有变化。您需要将ANT放入循环中以便重复运行此检查。这在Unix中很容易做到:

while true
> do
> ant
> sleep 300
> done