我正在为此使用连续轮询目录更改解决方案。(通用代码:http ://www.qualityontime.eu/articles/directory-watcher/groovy-poll-watcher/ (匈牙利语,但源代码是英文))
用于编译基于 nanoc 的站点的定制解决方案。查看并根据您的需要进行定制。(时髦)
def job = {
String command = /java -jar jruby-nanoc2.jar -S nanoc compile/
println "Executing "+command
def proc = command.execute()
proc.waitForProcessOutput(System.out, System.err)
}
params = [
closure: job,
sleepInterval: 1000,
dirPath: /R:\java\dev\eclipse_workspaces\project\help\content/
]
import groovy.transform.Canonical;
@Canonical
class AutoRunner{
def closure
def sleepInterval = 3000
// running for 8 hours then stop automatically if checking every 3 seconds
def nrOfRepeat = 9600
def dirPath = "."
long lastModified = 0
def autorun(){
println "Press CTRL+C to stop..."
println this
def to_run = {
while(nrOfRepeat--){
sleep(sleepInterval)
if(anyChange()){
closure()
}
}
} as Runnable
Thread runner = new Thread(to_run)
runner.start()
}
def boolean anyChange(){
def max = lastModified
new File(dirPath).eachFileRecurse {
if(it.name.endsWith('txt') && it.lastModified() > max){
max = it.lastModified()
}
}
if(max > lastModified){
lastModified = max
return true
}
return false;
}
}
new AutoRunner(params).autorun()