How to delete files that are not present in another directory?

好久不见. 提交于 2019-12-11 10:23:46

问题


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>

回答1:


"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




回答2:


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"/>


来源:https://stackoverflow.com/questions/13290555/how-to-delete-files-that-are-not-present-in-another-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!