how to copy a directory in ant - java

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).

Related

Simultaneously copying and renaming directory in ant

I've looked through ant tasks like copy, but can't really seem to find a way. With this kind of file structure:
/Resources/Docs/SpecificDoc/ScalaDocs
I want to rename "ScalaDocs" to "TargetDocs", having an end structure like so:
/Resources/Docs/TargetDocs
Try this.
<copy todir="/Resources/Docs/TargetDocs">
<fileset dir="/Resources/Docs/SpecificDoc/ScalaDocs">
<include name="*" />
</fileset>
</copy>

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

Copy Multiple Files and Directories with ANT

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.

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.

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