2010-08-25 100 views
9

这听起来有点牵强附会,但是有一个ANT任务,用于观察目录中的更改,然后在目录更改时运行特定的ANT类?是否有ANT任务用于查看目录中的更改?

+0

那么我还没有找到简单的解决方案,所以我暂时使用bash特定命令:'inoticoming --foreground ./src/ ant \;' – leeand00 2012-03-07 06:07:28

回答

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> 

如果文件可以从观看-DIR消失,那么你有更复杂的问题,你可以通过创建观看目录的影子目录结构,如果它符合的看着-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> 

随着创建的影子目录,当我离开的检查目录的一致性任务,给读者一个练习。

+3

目标目录是否具有比源目录更旧的文件并不重要。我想要一个可以监视目录的ant任务,当我在编辑器中保存文件中的更改时,它将调用特定的ant任务。 – leeand00 2012-03-07 05:04:49

1

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

+0

该ant任务确实轮询。没有办法让ant任务订阅目录进行更改吗? – leeand00 2010-08-25 22:28:46

+1

“订阅”是什么意思?注意文件系统事件? – akr 2010-08-27 23:24:35

0

您可以用fileset selector

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

文件集的apply任务结合起来将检查与存储的MD5校验文件更改。您需要将ANT放入循环才能重复运行此检查。这是很容易在UNIX环境中执行:

while true 
> do 
> ant 
> sleep 300 
> done 
3

是的,有一个Ant任务,将做到这一点:

https://github.com/chubbard/WatchTask

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

+0

没有许可证...... :( – 2014-04-28 18:33:16

+1

@JethroLarson现在它在当天添加您的评论,似乎:) – kekkis 2014-05-30 08:38:28

+0

还没有想出如何使用它,不知道在哪里找到他们引用的JAR,没有足够的文档。 – Vadorequest 2015-03-26 13:09:56

相关问题