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

lee*_*d00 9 ant directory listener watch

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

Ale*_*yak 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>
Run Code Online (Sandbox Code Playgroud)

如果文件可以从watch-dir中消失,那么你有更复杂的问题,你可以通过创建监视目录的影子目录结构并检查它是否与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>
Run Code Online (Sandbox Code Playgroud)

在创建了影子目录的情况下,我将把检查目录一致性的任务留给读者练习.

  • 目标目录是否包含比源目录更旧的文件并不重要.我想要一个将查看目录的ant任务,当我在编辑器中保存文件中的更改时,它将调用特定的ant任务. (3认同)