Delete duplicate files using ant? - java

Is there a way to delete duplicate files using ant? Specifically, if I have the same file name in two different output directories, I want to delete it from the second directory.

I think I came up with a solution.
<target name="delete-duplicates">
<delete>
<fileset dir="delete-here" includes="**/*">
<present targetdir="if-present-here" />
</fileset>
</delete>
</target>

Related

ANT unzip every jar except one

I have an ANT target that unzips every jar with a "for" task, but I want to exclude a specific jar called Neo.jar. This is what I have so far, but it is unzipping every single jar that is available to it.
<target name="unzipjars">
<for param="jar">
<sequential>
<unzip dest="${expanded.dirs}" src="#{jar}">
<exclude name="Neo.jar/**"/>
</unzip>
</sequential>
</for>
</target>
I was looking at trying to exclude something from "for param="jar"" but I don't think anything like that exists. The "exclude name" for Neo.jar doesn't seem to work because I believe it doesn't think it is a directory because it is a jar
As you can see here, it's quietly easy.
All you need is to add the tag patternset to your configuration (and you don't need to iterate by the way...). Like this:
<target name="unzipjars">
<unzip dest="${expanded.dirs}">
<patternset>
<exclude name="**/Neo.jar"/>
</patternset>
<fileset dir="${jar}">
<include name="**/*.*"/>
</fileset>
</unzip>
</target>
You can use the if task (http://ant-contrib.sourceforge.net/tasks/tasks/if.html) to filter out the Neo.jar file. For example
<for param="jar">
<sequential>
<if>
<not><equals arg1="#{jar}" arg2="Neo.jar" /></not>
<then><!-- unzip the jar --></then>
</if>
</sequential>
</for>

how to copy latest updated single file using ant?

I am using ant to build my application with timestamp. It is running currently. Now i want to get the latest updated folder so that i can copy that to any other folder.
I tried and it is printing me the latest file only but its not copying that file to another folder.
Please help me
<tstamp>
<format property="timestamp" pattern="dd-MMM-yy" locale="en,UK"/>
</tstamp>
<timestampselector property="latest.modified">
<path>
<fileset dir="${Engine.stagging.dir}">
</fileset>
</path>
</timestampselector>
<copy todir="${prjHop.release.dir}" flatten="true" overwrite="true">
<fileset dir="${Engine.stagging.dir}">
<include name="${latest.modified}"/>
</fileset>
</copy>
Also tried this to copy
<copy todir="${prjHop.release.dir}">
<path refid="${latest.modified}"/>
</copy>
<echo message="${latest.modified}" />
this echo is printing current folder and file name. I just want to copy the file inside that folder.
Thanks
The ${latest.modified} contains the absolute path of the file. When you use this path in the include nested element, the copy task will not find the file in the directory specified in ${Engine.stagging.dir} and hence will not copy it.
You can do the copy as follows:
<copy file="${latest.modified}" todir="${prjHop.release.dir}"/>

How to delete all .jar file into a directory using Ant?

I am pretty new to ant and I would delete all the .jar files that are into a directory.
Can I do something like it to do it?
<delete file="../Release/*.jar" />
Tnx
Andrea
According to the documentation
<delete>
<fileset dir=".." includes="Release/*.jar"/>
</delete>
should do the job.
See the examples of the Delete Task documentation for more details.
You can do it like this
<delete>
<fileset dir="../Release/" includes="*.jar"/>
</delete>
ANT Documentation is very easy to follow. Please learn using this link

Why my Ant delete does not work?

Using Ant 1.8.0
<target name="main">
<delete includeEmptyDirs="true">
<fileset dir="target/xxx/WEB-INF/lib" casesensitive="yes">
<filename name="junit-*.jar"/>
<filename name="gin-*.jar"/>
</fileset>
</delete>
</target>
When I run this Ant script, nothing happened, if I leave only one seem it works. I checked out the Ant FileSet Type, http://ant.apache.org/manual/Types/fileset.html, seem two does not matter.
So anybody who can tell me what's the problem here?
The delete isn't succeeding because your files don't match both of the filename selectors you specified. From the docs:
If any of the selectors within the FileSet do not select the file, the
file is not considered part of the FileSet. This makes a FileSet
equivalent to an <and> selector container.

Ant copy classpath jars to a directory

I'm sure this has either been asked before or is pretty straightforward. But for whatever reason, I cannot seem to make it work. I want to use ant to copy the ${build.classpath} (which contains a colon separated list of jars) to the ${output.dir}/myapp/WEB-INF/lib.
I have this right now and it doesn't seem to work:
<copy toDir="${output.dir}/myapp/WEB-INF/lib">
<fileset file="${build.classpath}" />
</copy>
It treats the whole classpath as one file. How do I get this to work?
The Ant Manual on the copy task contains the answer for your problem. One of the example snippets it provides:
Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.
<copy todir="dest" flatten="true">
<path>
<pathelement path="${java.class.path}"/>
</path>
</copy>
I think somethink like this should work:
<copy todir="${output.dir}/myapp/WEB-INF/lib" verbose="yes" flatten="yes" failonerror="no">
<fileset dir="${build.classpath}">
<include name="*.jar" />
</fileset>
</copy>
or with wildcard in include: <include name="**/*.jar" />
I think you should put all your colon separated jar files to one root folder. If it is not possible then create a separate task that put those jar files into one folder(may be temporary). And assign ${build.classpath} to that folder. Use <fileset dir="${build.classpath}"/> in your copy clause.
I hope, it should help.

Categories

Resources