2

I have two directories, let's call them src and build. My build system works so that for all files with mtime more recent in src than in build it copies the file from src to buid and does some transformations (minification, versioning, etc.). Otherwise skips as the file is considered up to date.

This however poses a problem when source file is deleted, as its built version is still present in build and gets into map file generated afterwards.

$ ls src
example1.js
example2.js

$ ant do-the-stuff
...

$ ls build
example1.js
example1-12345.min.js
example2.js
example2-23456.min.js
.map

$ cat .map
example1.js=example1-12345.min.js
example2.js=example2-23456.min.js

$ rm src/example2.js
$ ant do-the-stuff
...

$ cat .map
example1.js=example1-12345.min.js
example2.js=example2-23456.min.js

Is there a way to delete files not present in another directory with Ant? From set theory point of view it's a simple A\B operation.

This is what I have tried already but didn't work:

<delete dir="build">
    <exclude name="src/*" />
</delete>

<delete dir="build">
    <exclude>
        <fileset name="src" />
    </exclude>
</delete>

<delete dir="build">
    <fileset dir="build/*">
        <not>
            <present targetdir="src" />
        </not>
    </fileset>
</delete>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michał Niedźwiedzki
  • 12,859
  • 7
  • 45
  • 47

2 Answers2

3

"Is there a way to delete files not present in another directory with Ant"
yes, use delete task with a fileset using a presentselector, f.e.

<fileset dir="/home/rosebud/temp/dir1" includes="*.jar" id="srcfileset">
 <present present="srconly" targetdir="/home/rosebud/temp/dir2"/>
</fileset>
<echo>Files only in /home/rosebud/temp/dir1 => ${toString:srcfileset}</echo>
<delete>
 <fileset refid="srcfileset"/>
</delete>

would delete all files only present in /home/rosebud/temp/dir1
for the other way around use :

...
 <not>
  <present present="srconly" targetdir="/home/rosebud/temp/dir2"/>
 </not>
...

see also https://stackoverflow.com/a/12847012/130683 for another example using the present selector

Community
  • 1
  • 1
Rebse
  • 10,307
  • 2
  • 38
  • 66
0

In Ant there are tasks depend and dependset for this.

These delete targets for which sources are newer, but I guess this might be ok for you.

In this very concrete case, depend seems to be the right one.

Example:

<depend srcdir="src" destdir="build"/>
Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
  • I think it does the opposite of what I want. I have no problems with maintaining EXISTING files in sync. I need however to get rid of those DELETED from src/, so that they don't stay in build/, and not get into map file. – Michał Niedźwiedzki Nov 08 '12 at 15:57