是否有用于监视目录更改的 ANT 任务?

发布于 2024-09-16 04:28:10 字数 63 浏览 5 评论 0原文

这对我来说听起来有点牵强,但是是否有一个 ANT 任务来监视目录的更改,然后在目录更改时运行特定的 ANT 类?

It sounds a little far fetched to me, but is there an ANT task for watching a directory for changes and then running a particular ANT class when the directory changes?

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

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

发布评论

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

评论(4

找个人就嫁了吧 2024-09-23 04:28:10

如果文件只能在监视目录中添加或更改,那么您可以使用这个简单的 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>

如果文件可以从监视目录中消失,那么您会遇到更复杂的问题,您可以通过创建监视目录的影子目录结构并检查其是否与监视目录一致来解决。此任务比较复杂,但我将为您提供一个创建影子目录的脚本,因为它并不简单:

<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>

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

If files can only be added to or changed in the watched directory, then you can use this simple OutOfDate task from antcontrib.

<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>

If files can disappear from the watched-dir, then you have more complicated problem, that you can solve by creating shadow directory structure of the watched dir and checking if its consistent with the watched-dir. This task is more complex, but I'll give you a script to create a shadow directory, as it is not straight forward:

<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>

With shadow directory created, I'll leave the task of checking directory consistency as an exercise for the reader.

生生漫 2024-09-23 04:28:10

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

https://github.com/chubbard/WatchTask

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

Yes there is an Ant Task that will do this:

https://github.com/chubbard/WatchTask

It requires 1.7+. It can watch any number of filesets, and invoke any target depending on which fileset it came from.

我最亲爱的 2024-09-23 04:28:10

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

You might be able to use the Waitfor task to achieve what you want. It blocks until one or more conditions (such as the presence of a particular file) become true.

纸伞微斜 2024-09-23 04:28:10

您可以将 apply 任务与 文件集选择器

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

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

while true
> do
> ant
> sleep 300
> done

You can combine the apply task with a fileset selector

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

The fileset will check the files against a stored MD5 checksum for changes. You'll need to put ANT into a loop in order to repeatedly run this check. this is easy to do in Unix:

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