How to delete files that are not present in anothe

2019-08-08 19:08发布

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>

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-08 19:35

"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

查看更多
Explosion°爆炸
3楼-- · 2019-08-08 19:41

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"/>
查看更多
登录 后发表回答