Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an ANT task for watching a directory for changes?

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?

like image 432
leeand00 Avatar asked Aug 25 '10 22:08

leeand00


People also ask

What is true about Ant task?

Ant tasks are the units of your Ant build script that actually execute the build operations for your project. Ant tasks are usually embedded inside Ant targets. Thus, when you tell Ant to run a specific target it runs all Ant tasks nested inside that target.

What does the Replace Task do?

Replace is a directory based task for replacing the occurrence of a given string with another string in selected file. If you want to replace a text that crosses line boundaries, you must use a nested <replacetoken> element. The output file is only written if it differs from the existing file.

What is Basedir in Ant?

The 'basedir' is the base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.

What is target Ant?

A target is a container of tasks and datatypes that cooperate to reach a desired state during the build process. Targets can depend on other targets and Apache Ant ensures that these other targets have been executed before the current target.


3 Answers

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.

like image 137
Alexander Pogrebnyak Avatar answered Oct 24 '22 00:10

Alexander Pogrebnyak


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.

like image 4
chubbsondubs Avatar answered Oct 24 '22 01:10

chubbsondubs


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.

like image 1
Dan Dyer Avatar answered Oct 24 '22 00:10

Dan Dyer