Copy Multiple Files and Directories with ANT - java

I have an Ant Script, using the ANT LIBRARY how can i copy multiple files/folders to multiple directories. I use a properties file which contains
FileToCopy = DestinationFolder
FolderToCopy = FolderDestination

Copy a single file
<copy file="myfile.txt" tofile="mycopy.txt"/>
Copy a single file to a directory
<copy file="myfile.txt" todir="../some/other/dir"/>
Copy a directory to another directory
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Copy a set of files to a directory
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
examples from copy ant task

Some additional information to sergiofbsilvas answer, because I was searching for such an example.
One can also specify multiple filesets in a single copy task.
Example:
<copy todir="${temp.dir}">
<fileset dir="${classes.dir}"/>
<fileset dir="${basedir}">
<include name="log4j.xml"/>
<include name="config.properties"/>
<include name="kfatransfer.bat"/>
</fileset>
</copy>
Tested with ant 1.10.6 on Windows.

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

Ant build creates incorrect relative paths to jars when generating manifest

I'm using Ant to build my Java application and to generate the MANIFEST.MF file automatically so it includes all the jars in my lib directory.
This seems to work but the problem is that instead of writing them as lib/some.jar, it includes my Eclipse project's name: MyProject/lib/some.jar.
This is ofcourse incorrect and causes none of the jars to be found when run as a standalone app.
Build.xml (important part is at the end):
<?xml version="1.0"?>
<project name="fidea_migration" default="dist">
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" description="cleaning the old deliverables">
<delete includeemptydirs="true">
<fileset dir="bin" includes="**/*"/>
</delete>
<delete includeemptydirs="true">
<fileset dir="_deliverables" includes="**/*"/>
</delete>
</target>
<target name="prepare" description="preparing the deliverables folders">
<mkdir dir="_deliverables/lib"/>
</target>
<path id="jarlib">
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
</path>
<manifestclasspath property="lib.list" jarfile=".">
<classpath refid="jarlib" />
</manifestclasspath>
<target name="compile" depends="clean, prepare" description="compiling java sources">
<mkdir dir="bin"/>
<javac srcdir="src/main/java" destdir="bin">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="dist" depends="compile" description="creating binary distribution">
<copy todir="_deliverables/lib">
<fileset dir="lib"/>
</copy>
<copy todir="_deliverables">
<fileset dir="src/main/resources">
</fileset>
</copy>
<jar jarfile="_deliverables/lib/app.jar" basedir="bin">
<manifest>
<attribute name="Class-Path" value="${lib.list}"/>
</manifest>
</jar>
</target>
</project>
Example of how my Manifest looks:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Class-Path: MyProject/lib/All-MB.jar MyProject/lib/activation.jar MyProject/lib/aspectjrt.jar
Any idea on how to fix this so it just shows xxx.jar instead of MyProject/lib/xxx.jar (so without "MyProject/lib/")?
Cheers,
Bart
manifestclasspath expects the jarfile attribute to point to the location of the JAR file (which probably doesn't exist yet, but that's fine). Since you're creating the JAR file at _deliverables/lib/app.jar and you're also copying all the lib JARs from lib to _deliverables/lib then
<manifestclasspath property="lib.list" jarfile="lib/app.jar">
<classpath refid="jarlib" />
</manifestclasspath>
should do the trick, and will create an eventual Class-Path with the right relative paths, i.e. All-MB.jar activation.jar aspectjrt.jar etc. etc.
You must set your project Dir inside your build.xml as,
<property name="projectDir" value=".." />
After this, you must try to work on everything with relative path. Currently, the reason behind problem is that the absolute path is being used.

how to copy a directory in ant

Say I have directory structure like this:
base_dir1/src_dir
base_dir2/dest_dir
How do I copy src_dir(folder + contents) into dest_dir.
If I use copy task like this:
<copy todir="base_dir2/dest_dir">
<fileset dir="base_dir1/src_dir"/>
</copy>
It will copy all the contents of src_dir into dest_dir, but will not create a src_dir folder inside dest_dir.
I can make it work by using the copy task like this:
<copy todir="base_dir2/dest_dir">
<fileset dir="base_dir1">
<include name="src_dir/"/>
</fileset>
</copy>
Is this the correct way or is there a better way to do it?
Alternately, you could do this.
<copy todir="base_dir2/dest_dir/src_dir">
<fileset dir="base_dir1/src_dir"/>
</copy>
The folder (or part of the folder) specified by todir need not exist.
<fileset> refers to contents of the specified folder (excluding it).

copy subfolders to a directory using ant

I want to copy a lot of subfolders and its content from various other folders to one specific folder.
I also want to use a patternset to choose the subfolders to copy.
For example there may be a folder wich contains a lots of subfolders which then could contain again subfolders. Now I want a patternset to get all folders named org or com or whatever and copy them where I like.
I tried it this way:
<target name="copysrc">
<patternset id="set">
<include name="**/org/**"/>
<include name="**/com/**"/>
<include name="**/de/**"/>
<include name="**/net/**"/>
</patternset>
<copy todir="${tmp.dir2}">
<fileset dir="${tmp.dir}" casesensitive="no">
<patternset refid="set" />
</fileset>
</copy>
</target>
Almost did the trick but I still have all the folders above the org, com,... copied with them and not just org, com,.. and the contend below these folders.
Thanks in advance, mojoo.de
Okay got it working now .. had to use the ant contrib package code looks like
<target name="copysrc">
<patternset id="zuKopieren">
<include name="org/**"/>
<include name="com/**"/>
<include name="de/**"/>
<include name="net/**"/>
</patternset>
<for param="verzeichnisName">
<dirset dir="${tmp.dir}"></dirset>
<sequential>
<copy todir="${tmp.dir2}/src">
<fileset dir="#{verzeichnisName}" casesensitive="no"><patternset refid="zuKopieren" />
</fileset>
</copy>
</sequential>
</for>
</target>

Categories

Resources