Ant do not delete file with no name like .gitignore .git - ant

I want to use git to delete a folder,the folder contains some file with no name and only have extension,such as .gitignore,.log
I did it as below,and I also tried many other ways,but still can not delete them,can anyone help me?Thanks in advance!
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="clean">
<target name="clean">
<delete>
<fileset dir="E:\test\tmt"/>
</delete>
<echo message="Finished delete file"/>
</target>
</project>

In the following snippet...
<delete>
<fileset dir="E:\test\tmt"/>
</delete>
....gitignore won't be deleted because .gitignore is in Ant's Default Exclude Set.
You can configure Ant to ignore the Default Exclude Set with the defaultexcludes attribute...
<delete>
<fileset dir="E:\test\tmt" defaultexcludes="false"/>
</delete>
...or even better, use the following instead...
<delete dir="E:\test\tmt"/>
defaultexcludes isn't needed in this case because the Default Exclude Set only applies to pattern sets such as <fileset>.

It is deprecated so I would go with Chad's answer, but ant delete also has an excludes property which takes in a comma-separated list of patterns (or direct file names). Just FYI.

Related

Delete a folder using starts with in ANT

I am comparing 2 folders A and B, and wanted to delete folders and jars present in B which are not present in folder A.
I have written the logic to get needed files to delete, but i do not wanted to delete the directory and jars starting with "com.ibm".
For that I have written delete task as below:
<delete>
<dirset dir="D://mypath/plugins<Filename to delete> excludes="**/com.ibm.*/**" />
</delete>
I have tried the excludes with the scenarios like:
excludes="**/com.ibm.*/**"
excludes="**/com.ibm.*"
excludes="com.ibm.*"
excludes="com.ibm.*/**"
But nothing works for me (It is not deleting any folders/files). Any help would be highly appreciated. Thanks !
you should use fileset instead and specify includeemptydirs="true" of delete.
<project default="init" name="My Project">
<target name="init">
<delete verbose="true" includeemptydirs="true">
<fileset dir="/home/guest/Desktop/plugins" defaultexcludes="no">
<exclude name="com.ibm.*"/>
<exclude name="com.ibm.*/**"/>
</fileset>
</delete>
</target>
</project>

ivy cleancache but specific folders

We have some internal jars in a remote repository and do not build them very often.The resolver looks like this: view-snapshots is a local repo and hub-releases is a shared repo.
<chain name="hub-internal-library-chain" returnFirst="true">
<resolver ref="view-snapshots"/>
<resolver ref="hub-releases"/>
</chain>
By default,if a developer publishes anything it goes to view-snapshots cache.We need to keep the flag
return first-"true"
for performance issues.So the issue I want to delete some specific files in cache(local) and repository(local).I am a newbie to ivy,any help will be appreciated.
Thanks
PS: I have already had a look at this question which is similar to mine.But its not solved yet.
http://grokbase.com/t/ant/ivy-user/105d2bxpyy/refreshing-ivy-cache-after-changing-a-published-version
There is no dedicated code in Ivy which delete the entries of only one module in a cache.
But it is quite easy to do with regular Ant tasks. Assuming you have the default cache with the default pattern, here a piece of build.xml which will delete the cache of the module foo of the company com.acme:
<delete dir="${user.home}/.ivy2/cache/com.acme/foo" />
This the solution that works for me :
<target name="clear-entries" if="clean-selected-cache" depends="clear-cache,clear- repository">
<echo>Clearing cache and repository entries for internal libraries</echo>
</target>
<target name="clear-cache">
<delete verbose="true">
<fileset dir="${ivy.view-local.cache.root}">
<includesfile name="path.to.file/clear-cache.includes.txt"/>
</fileset>
</delete>
</target>
<target name="clear-repository">
<delete verbose="true">
<fileset dir="${ivy.view-local.repository.root}">
<includesfile name="path.to.file/clear-cache.includes.txt"/>
</fileset>
</delete>
</target>
You can easily add the specific folder you want to delete from cache and from repository in clear-cache.includes.txt.

Using Ant to delete a file based on existence of another file

I need to write an ant task to selectively delete files.
In a directory, I have the following jars:
acme.jar
acme-201105251330.jar
I want to delete acme.jar because acme-*.jar exists.
Here's what I've tried:
<target name="-check-use-file">
<available property="file.exists">
<filepath> <fileset dir=".">
<include name="./test-*.jar"/> </fileset>
</filepath>
</available>
</target>
<target name="use-file" depends="-check-use-file" if="file.exists">
<!-- do something requiring that file... -->
</target>
Thanks
Have a look at If/Unless Attributes, examples given there seem to be exactly what you are looking for.

Ant Copying Empty Directories

I am still very new to ant and, although I know coldfusion, I don't know very much about java conventions, but I know that ant is built using java conventions. That being said I am working on an ant process to copy a project to a temp folder, change some code in the project, and then push the temp directory up to an FTP. I am trying to exclude all of my git, eclipse, and ant files from the copy so that my testing platform doesn't get cluttered. I setup a target to do the copy, but it seems that Ant not only is ignoring my excludes (which I am sure I wrote wrong), but it is only copying top level directories and files. No recursive copy. My current target is:
<target name="moveToTemp" depends="init">
<delete dir="./.ant/temp" />
<mkdir dir="./.ant/temp" />
<copy todir="./.ant/temp">
<fileset dir=".">
<include name="*" />
<exclude name=".*/**" />
<exclude name=".*" />
<exclude name="build.xml" />
<exclude name="settings.xml" />
<exclude name="WEB-INF/**" />
</fileset>
<filterset>
<filter token="set(environment='design')" value="set(environment='testing')" />
</filterset>
</copy>
</target>
I know that I am not doing my excludes right, but I don't know what I am doing wrong with them. I see double asterisks (**) used all the time in Ant but I can't figure out
By default an Ant fileset will (recursively) include all files under the specified directory, equivalent to:
<include name="**/*" />
That's the implicit include. If you supply an include, it overrides the implicit one.
Your include
<include name="*" />
Says 'match any file in the fileset directory', but that excludes traversal of subdirectories, hence your issue. Only files and the top-level directories are being copied.
See Patterns in the Ant docs for directory-based tasks: ** matches any directory tree (zero or more directories).
For your case you should be able to simply remove the 'include', so that the implicit 'include all' applies.
Suggest you also investigate the defaultexcludes task, which lets you set up this sort of thing once for the whole project.
Responding to the title of the question. You can include copy of empty directories as follows. (includeemptydirs attribute)
Example:
<copy includeemptydirs="true" todir="${directory}${file.separator}sentinel_files">
<fileset dir="${basedir}${file.separator}sentinel_files"/>
</copy>
Use the documentation provided in:
https://ant.apache.org/manual/Tasks/copy.html

ANT How to delete ONLY empty directories recursively

Does anyone know how to recursively delete "empty" directories with ANT (empty includes directories that only contain ".svn" etc).
I know ant allows you to "includeEmptyDirs=true" but I want it to ONLY delete a directory if it is empty (and actually I'd probably need to walk up the recursive chain and delete the directory it was contained in if it is now empty).
Basically as part of our build process we copy over a set of directories that contain a bunch of other nested directories containing various XML and data, and when we move the location for that data our "copy" and checkin build process doesn't really work, and because we are checking into another source control (SVN), wiping out the directories and copying over isn't really an option either (we'd be blowing away the ".svn" folders).
Before we copy over the new builds I can "clear" out the directories by doing the following:
<delete>
<fileset dir="${webplatformBin}" includes="**/*"/>
</delete>
This leaves every directory (with the ".svn") as an empty directory and then I copy over the new files. After they're copied I'm not sure how I can clear out the empty directories that are left (if we've completely moved where the top-level data directory is etc.).
For example if I had a /projectA/data/localization/text.xml file and I moved it to /projectB/data/localization/text.xml, I would end up with an empty folder /projectA/data/localization/ (that would only contain a .svn folder).
Here's the best answer I've been able to come up with:
<delete includeemptydirs="true">
<fileset dir="${dirToStartFrom}" >
<and>
<size value="0"/>
<type type="dir"/>
</and>
</fileset>
</delete>
I then wrapped it in a macro so I can pass the dir name in from any target:
<!-- Find and delete empty folders under dir -->
<macrodef name="deleteEmptyFolders">
<attribute name="dir"/>
<sequential>
<delete includeemptydirs="true">
<fileset dir="#{dir}" >
<and>
<size value="0"/>
<type type="dir"/>
</and>
</fileset>
</delete>
</sequential>
</macrodef>
Like so:
<target name="clean">
<deleteEmptyFolders dir="build"/>
<deleteEmptyFolders dir="common"/>
<deleteEmptyFolders dir="lib"/>
</target>
Here's what I cooked up:
<!-- next three targets are connected
To remove empty folders from XXX folder. Process is recursed 3 times. This
is necessary because parent directories are not removed until all their children
are (if they are empty), and parents are processed before children
My example will process structures 3 deep, if you need to go deeper
then add members to the list like list="1,2,3,x,x,x,x,x,x" -->
<target name="rmmtdirs">
<foreach list="1,2,3" target="rmmtdirs_recurse" param="num"/>
</target>
<target name="rmmtdirs_recurse">
<foreach target="rmmtdir" param="rmdir">
<path>
<dirset dir="${XXX}"/>
</path>
</foreach>
</target>
<target name="rmmtdir">
<echo message=" Removing: ${rmdir} "/>
<delete includeemptydirs="true">
<fileset dir="${rmdir}" excludes="**/*"/>
</delete>
</target>
I was able to delete all the empty directories starting with the current working directory with the following:
<delete includeemptydirs="true">
<fileset dir="." includes="**" excludes="**/*.*" />
</delete>
If it is not sufficient to completely clear the target location (use defaultExcludes="false" to ensure the .svn folders are deleted), you could try writing a custom ant task to traverse the file system below the target, deleting empty directories as you move back up from each leaf.
This is probably easier to do with a batch file that gets called from ant.
You can use Raymond Chen's script, but it doesn't work if there are spaces in the names.

Resources