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

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

Related

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?

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.

Eclipse: Ant script to export User Library/Libraries for project

I am new to Java programming. I initially started with NetBeans but have moved to Eclipse given the advice from a friend.
In NetBeans, a pre-written ant build script for the project would generate a Project.jar file and place all required libraries/jars in a lib/ folder.
However, in Eclipse it appears that I need to write my own ant script. I have written a few lines to generate the jar file:
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="jars" debug="on"/>
</target>
How do I write a command to copy all of the jars in my User Library to a ${build.dir}/lib/ folder?
Thanks.
Use the copy task
like so, with the appropriate include or exclude pattern
<copy todir="${build.dir}/lib/">
<fileset dir="src_dir">
<include name="**/*.jar"/>
</fileset>
</copy>
<copy todir="${build.dir}/lib/">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
If you are new to Java take the chance to have a look at maven. It is a build tool like ant with a lot of predefined 'goals' and a fully developed dependency (to other libraries) handling. You will find a eclipse plugin which will be very useful.
Maven projects have a special directory layout which is kind of best practise and helpful for beginners. If you work on a maven project you can just use the command
mvn dependency:copy-dependencies
as a console command (or eclipse run configuration) to copy your project dependencies (libraries) to the <project>\target\dependency directory.
I recommend to use ant4eclipse library for ant based eclipse projects. When you use it, you can access eclipse workspace/project settings, and can iterate tought eclipse project class path in ant.
See the example code bellow:
<path id="ant.classpath">
<fileset dir="${lib.dir}/ant4eclipse">
<include name="*.jar" />
</fileset>
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<taskdef resource="net/sf/ant4eclipse/antlib.xml" />
<targetPlatform
<target name="copy_jars">
<getEclipseClasspath workspace="${basedir}/.."
projectname="TestProject"
targetPlatformLocation="c:/eclipse"
property="classpath"
relative="false"
runtime="true"
pathseparator="#" />
<!-- iterate over all classpath entries -->
<foreach list="${classpath}" delimiter="#"
target="copy_jar_file" param="classpath.entry" />
</target>
<target name="copy_jar_file">
<!-- check if current is a .jar-file ... -->
<if>
<isfileselected file="${classpath.entry}">
<filename name="**/*.jar" />
</isfileselected>
<then>
<!-- copy the jar file to a destination directory -->
<copy file="${classpath.entry}" tofile="${dest.dir}"/>
</then>
</if>
</target>
If you would like to use user libraries, you can define it by userlibraries command.

Linked folder not resolved by Ant in eclipse

In my build.xml, below works fine :-
<path id="build.classpath">
<fileset dir="lib [myUtils]" includes="*.jar" />
</path>
if lib [myUtils] is of folder type, but don't works, if it's of Linked Folder type.
Also, I found this when googled :-
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
https://bugs.eclipse.org/bugs/show_bug.cgi?id=43081
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
Is there any trick to achieve this, without copying the dependencies in work folder??
Note that ant should work outside of eclipse as well. So you can't reply in IDE abstractions. You can use symbolic links (if your OS supports them).
If not, you can use the FileSync plugin to synchronize eclipse project folders with external folders. Or you can simply use the <copy> ant task.
I resolved this using copy task:
<copy todir="target/web/linked1">
<fileset dir="../linkdProject/source1" />
</copy>
<copy todir="target/web/linked2">
<fileset dir="../linkedProject/source2" />
</copy>
....
<war destfile="target/webApp.war">
<fileset dir="WebContent" />
<fileset dir="target/web" /> <!-- copy linked resources -->
...
</war>
<delete dir="target"/>

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