copying folder from source to destination in build.xml using copydir command - ant

I am facing issue while copying directories from source to destination in ant build file.
code:-
<target name="getResources" depends="init">
<delete dir="${resources.dir}"/>
<copy file="${svn.resources.url}" tofile="${resources.dir} " />
</target>
Error:-
build.xml:150: Use a resource collection to copy directories.
Please suggest me how to copy directory from source to destination. I used several ways...like
<export srcUrl="" destPath=""> this is also saying deprecated and used and also. but none of the properties are working. Please suggest me how to do.

<!-- copy one file -->
<copy file="/home/guest/workspace/so/src/build.xml" tofile="/home/guest/workspace/so/src/build2.xml" />
<!-- copy a folder recuresively -->
<copy todir="/home/guest/workspace/so/src2">
<fileset dir="/home/guest/workspace/so/src" includes="**" />
</copy>
you also may want to look at copy task documentation and examples here

Related

Copying only files which have another file as a sibling [based on extension]?

I am trying to use ANT to copy some files of a C++ build into another directory.
The output of the build looks like this:
/Build
/LibA
LibA.lib
LibA.pdb
/LibB
LibB.lib
LibB.pdb
/ProjA
myexe.exe
myexe.pdb
/Foo
foo.exe
foo.pdb
/...
Now, I would like to copy all *.exe files and their *.pdb files to another directory (but not the *.pdb files of the libs).
I tried with:
<copy todir="outdir">
<fileset dir="Build">
<include name="**/*.exe" />
<include name="**/*.pdb" />
</fileset>
</copy>
However, then I will also get the *.pdb files inside the LibA, LibB, ... folders.
Is there any way I can only copy the pdb-files which have an *.exe-file in the same directory as their sibling?
Unfortunately, the folders are not named in any way that would allow using wildcards based on the folder name.
Of course, I could list each file individually, such as:
<include name="ProjA/*.pdb" />
<include name="Foo/*.pdb" />
<!-- ... -->
However, I am thinking that maybe there is an elegant way where I can specify "copy all *.exe files and all *.pdb files which have an *.exe file next to them".
You could use the <present> selector to find the "sibling" files, something like this:
<copy todir="outdir">
<fileset dir="Build" includes="**/*.pdb">
<present targetdir="Build">
<mapper type="glob" from="*.pdb" to="*.exe" />
</present>
</fileset>
<fileset dir="Build" includes="**/*.exe" />
<flattenmapper />
</copy>
This will only copy the .pdb files with matching .exe files.
To keep it simple, use a separate fileset for the exe files.
The flatten mapper is only needed if you want to copy just the files ignoring the directory structure in the source.

How to echo the file names that only copied or removed by sync task in ant?

I'm trying to copy the schema files from my workspace to a config folder as part of build.
I've achieved that by using the sync task.
These are my requirements:
1.Need to replace only the latest not every file each time.
2.Need to display in console, what are the files being changed (copied /removed)
<target name="copy-schema">
<sync todir="C:/config/schema">
<fileset dir="${schema.dir}" id="schema_dir"/>
<preserveintarget preserveemptydirs="true">
<include name="**/**" />
</preserveintarget>
</sync>
</target>
This copies the schema Files, but I'm not able to see what are the files copied.
I've tried the below,
<target name="copy-schema">
<sync todir="C:/config/schema">
<fileset dir="${schema.dir}" id="schema_dir"/>
<preserveintarget preserveemptydirs="true">
<include name="**/**" />
</preserveintarget>
</sync>
<property name="filesCopied" refid="schema_dir"/>
<echo>${fileCopied}</echo>
</target>
But it prints all the files in the directory.
Any help is appreciated.
Thanks in advance
The sync task supports a verbose attribute to log the files that are being copied.
Try adding the verbose attribute in your sync:
<sync todir="C:/config/schema" verbose="true">

How To copy a file in ant irrespective of its location

I want to copy a jar file from ../source,but I am not sure where it exactly is,it can be anywhere in any subdirectory but it is there. I am writing this code
<target name="CopyingFromSource" depends="clean">
<copy file="../source/org.springframework-spring-support-2.0.8.jar" todir="../result"/>
</target>
but is showing me can't find org.springframework-spring-support-2.0.8.jar Can you please tell me how to copy it?
It is present anywhere either immediately in /source or in any subdirectory.
Use copy task with nested fileset like that :
<project>
<copy todir="../result">
<fileset dir="/home/gilreb/temp" includes="**/org.springframework-spring-support-2.0.8.jar" id="foobar"/>
</copy>
</project>
see Ant manual copy task and Ant manual fileset for turther details.. i.e. if you need to copy the flat file only and not it's whole path you need to use :
<copy todir="../result" flatten="true">
...
</copy>

Relocate contents of dynamic folder

I have a zip file which has one base folder inside it with other content inside that. I don't always know what that base folder is going to be called until I unzip it.
I'd like to move that base folder, and rename it at the same time, in ant - but can't seem to find out how. I've written code to extract the contents of the zip file to ${local.sdk.dir}/temp/ but from here i can't work out how to rename/move the extracted folder
<move todir="${local.sdk.dir}/${remote.sdk.file.name}">
<fileset dir="${local.sdk.dir}/temp/<WHAT_DO_I_PUT_HERE?>"></fileset>
</move>
also tried
<move todir="${local.sdk.dir}/${remote.sdk.file.name}" includeEmptyDirs="yes" verbose="true">
<fileset dir="${local.sdk.dir}/temp/" >
<include name="**/*" />
</fileset>
</move>
and played about with this, but closest I can get without ant throwing an error is to copy the contents of the temp dir, not the base folder within temp.
You can do all this in one step - copy from the zip file and rename the files changing the dir name as you copy. The copy task accepts a nested resource collection, so you can use a zipfileset to specify the files to copy directly from the zip file.
In order to rename the files as they are copied, you can use a mapper, which the copy task also takes as a nested element. In this case, a cutsdirmapper looks like the tool for the job.
So, if I've understood what you want to do correctly, something like this should work:
<copy todir="${local.sdk.dir}/${remote.sdk.file.name}">
<zipfileset src="${your.zip.file}" />
<cutdirsmapper dirs="1" />
</copy>
cutdirsmapper is only available in Ant 1.8.2 onward, so if you're using an earlier version, you could try a regexpmapper:
<regexpmapper from="[^/]*(.*)" to="\1" />
Similar to this question
<target name="relocate_sdk_folder">
<path id="sdk_folder_name">
<dirset dir="${local.sdk.dir}/temp/">
<include name="*"/>
</dirset>
</path>
<property name="sdk_folder_name" refid="sdk_folder_name" />
<echo message="renaming ${sdk_folder_name} to ${remote.sdk.file.name}" />
<move file="${sdk_folder_name}" tofile="${local.sdk.dir}/${remote.sdk.file.name}" />
</target>

ANT war task fails with strange error message

I am trying to create a war file out of an eclipse project using ant
The responsible ant target looks like this
<target name="jar" depends="build" description="Erzeugt das WAR File">
<war destfile="${project.dir.dist}/xyz.jar" webxml="${basedir}/WebRoot/WEB-INF/web.xml" duplicate="fail" basedir="${basedir}">
<lib dir="${project.dir.dist}" excludesfile="${project.dir.dist}/xyz.jar" />
<classes dir="${project.dir.bin}" />
<webinf dir="${basedir}/WebRoot/WEB-INF" excludes="*.class" />
<metainf dir="${basedir}/WebRoot/META-INF" />
</war>
</target>
And it fails with the following error:
F:\eclipse_workspaces\skyeye\railWeb\build.xml:35: Syntax error in property: ??? ???i8?
Google search turned up only this: http://209.85.135.132/search?q=cache:OrmNOY9EJd0J:teamcity.jetbrains.com/viewLog.html%3Bjsessionid%3D114D52086BAE423B2F69A99B4CFACACD%3FbuildId%3D29573%26tab%3DbuildChangesDiv%26buildTypeId%3Dbt134+ant+war+task+%22Syntax+error+in+property%22&cd=1&hl=en&ct=clnk&client=firefox-a
Can anybody explain, what the heck is going on?
The propblem was that I used 'excludesFile' assuming it would exclude a single file. Instead ANT tried to parse it as an property file which gets difficult since it actually was a jar file.
The correct way to exclude a jar file is given in the documentation. If anyone face same issue, they can refer to this link.
This example is taken from the documentation, here we are removing jdbc1.jar from lib
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif

Resources