Why my Ant delete does not work? - java

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.

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>

Ant: Unzip patternset not excluding META-INF and jUnit

Because some guys forgot to set the junit scope to "test", it has been packaged along with our libraries. Since we don't need junit in our final product, I wanted to exclude the class files in an unzip task.
Note: This task is necessary in the further build process, so leaving this out is not an option.
So far my unzip statement looks like this:
<unzip dest="${classes.dir}">
<fileset refid="dependency.fileset"/>
<patternset>
<exclude name="META-INF/*"/>
<exclude name="org/junit/**/*.class"/>
<exclude name="junit/**/*.class"/>
</patternset>
</unzip>
I tried various combinations, but the junit and META-INF files magically reappear each time.
As a work-around I added a delete statement with a fileset. It works but is completely unnecessary if I have the option to add a patternset to the unzip statement - in my opinion:
<delete includeemptydirs="true">
<fileset dir="${classes.dir}" casesensitive="false" includes="META-INF/*,junit/**/*,org/junit/**/*" />
</delete>
I already read through the manual, but found no clue on how to solve this problem.
Did I miss something, are the patterns incorrect or is it something else?

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

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.

Delete duplicate files using ant?

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>

Categories

Resources