2010-08-25 47 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

相關問題