Get files from FTP in ant - ant

I have a FTP folder structure as,
-- Folder 1
-- Folder 2
-- external
-- Code
-- MY Dir
Now, I want to get the folder "Code" from here. I am using the code,
<ftp action="get"
server="server"
userid="administrator"
password="pass" >
<fileset dir="Code">
</fileset>
</ftp>
But this gets my whole contents present in the FTP rather than getting only the folder "Code".
Please let me know.

Hi I'm using this and i t works for me http://www.avajava.com/tutorials/lessons/how-do-i-ftp-a-file-with-ant.html
edited: retrive files
<ftp action="get"
server="server"
userid="administrator"
password="pass">
<fileset dir="/path/to/Code">
<include name="*"/>
</fileset>
</ftp>

remote dir has to be used within the Ftp. The example is given below.
<ftp action="get"
server="sever"
userid="user"
password="pass"
separator="/"
remotedir="/remotedir">
<fileset dir="e:/temp/">
<include name="**/*" />
</fileset>
</ftp>

Related

I wan to unzip multiple files in a folder with the same name as the file name using Ant?

I have a folder with multiple .zip files. I want to unzip them in the same folder, with the name same as that of the file.
eg.
temp/product_file1.zip
temp/product_file2.zip
temp/product_file3.zip
Output expected:
temp/product_file1
temp/product_file2
temp/product_file2
How can I make a generic code using Ant?
The code which I am using now is able to only unzip the last file.
<path id="warFilePath">
<fileset dir="/temp">
<include name="product_*.zip"/>
</fileset>
</path>
<property name="warFile" refid="warFilePath" />
<basename property="warFilename" file="${warFile}" suffix=".zip" />
<unzip dest="temp/${warFilename}">
<fileset dir="/temp">
<include name="${warFilename}.zip"/>
</fileset>
</unzip>

Running ant from jenkins

I am trying to deploy my zip file from local machine to remote machine, i am using jenkins with ant. the problem is build gets success but 0 files has been sent to remote machine using ftp.
my build.xml file as below
<project name="test" default="test">
<target name="test">
<zip destfile="htmlfiles.zip">
<fileset dir=".">
<include name="**/*.php"/>
</fileset>
</zip>
<ftp server="192.168.0.66"
userid="admin"
password="admin">
<fileset dir="C:\Users\ADMIN\Desktop\ftp">
<include name="*"/>
</fileset>
</ftp>
</target>
</project>
and following my result of jenkins
test:
[zip] Building zip: C:\Users\ADMIN\.jenkins\workspace\test\htmlfiles.zip
[ftp] sending files
[ftp] 0 files sent
BUILD SUCCESSFUL
Total time: 0 seconds
Finished: SUCCESS
anyone help me out that why it is happening?
Try this and let me know if its works for you.
<project name="test" default="test">
<target name="test">
<property name="distdir" value="C:\Users\ADMIN\Desktop\ftp"/>
<zip destfile="${distdir}/htmlfiles.zip">
<fileset dir=".">
<include name="**/*.php"/>
</fileset>
</zip>
<ftp server="192.168.0.66"
userid="admin"
password="admin">
<fileset dir="C:\Users\ADMIN\Desktop\ftp">
<include name="*"/>
</fileset>
</ftp>
</target>
</project>
As i can see from logs that your .zip file is created at C:\Users\ADMIN\.jenkins\workspace\test\htmlfiles.zip but in ftp tag you have specified the location "C:\Users\ADMIN\Desktop\ftp". fileset in ftp folder doesn't specify the destination directory. The file will be put in default folder for ftp user(here which is admin)

Copy files and folder to WEB-INF using Ant

I want to copy a .properties file from a certain location to my WEB-INF/classes/com/infiniti folder(in a WAR file).
I have gone through this link How to get Ant to copy properties file to classes directory
using which I can copy the .properties file to WEB-INF/classes/ but not to WEB-INF/classes/com/infiniti
Code I am using is:
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
<lib dir="${lib}">
.......
.......
.......
<classes dir="${configHome}/config/com/infiniti">
<include name="accredit.properties" />
</classes>
...
....
.......
</war>
Also I need to copy ${configHome}/resources/com/infiniti/errorcode folder to
WEB-INF/classes/com/infiniti.
Is this possible using Ant?
yes, you can use for instance ZipFileSet like this
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
...
<zipfileset dir="${configHome}/config/com/infiniti" includes="**/*.properties" prefix="WEB-INF/classes/com/infiniti"/>
Yes, it's possible using ant. Just use the copy or sync commands to move your file:
<copy todir="${distribution}/location" file="${local.path}/data/file.txt">
</copy>
You can also copy with rules:
<copy includeemptydirs="false" todir="${combined.bin}">
<fileset dir="${buildbin}"/>
<fileset dir="${output2buildbin}"/>
<fileset dir="${output3buildbin}"/>
</copy>
Using sync:
<sync includeemptydirs="false" todir="${distres}">
<fileset dir="${buildres}">
<include name="logging/**" />
</fileset>
</sync>
Tasks are described on their doc site:
http://ant.apache.org/manual/Tasks/copy.html
The same 'fileset' declarative applies to the war task:
Examples
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
http://ant.apache.org/manual/Tasks/war.html

Ant Replace Task in FTP Files

Is there a way to call the Ant replace task on files in an FTP? I have a project with some static IDs in the code that change depending on whether I am on development platform or production. I setup my and build.xml file to copy all my files to FTP, but I need to change those static IDs either on the way to the FTP or when they hit it.
Ant doesnot have inbulit replace task.
I achieved it by calling "del" first and than used "put", as below.
<ftp action="del" server="%ftpservername%" userid="%ftpserver_user%" password="%ftpserver_pwd%">
<fileset>
<include name="%file1%.db" />
<include name="%file2%.db" />
</fileset>
</ftp>
<ftp action="put" retriesAllowed="-1" remotedir="%dest_dir%" server="%ftpservername%" port="%ftpport%" userid="%ftpserver_user%" password="%ftpserver_pwd%" binary="yes">
<fileset dir="%src_dir%">
<include name="%file1%.db" />
<include name="%file2%.db" />
</fileset>
</ftp>
we can also do for directory which is in ant.apache.org
If you are looking to do replace within the ant ftp task, it looks like this is not supported. But you could do a replace followed by ftp.
<replace dir="${ftp.src} token="###" value="replaced"/>
<ftp server="server" userid="user" password="password">
<fileset dir="${ftp.src}"/>
</ftp>
Or perhaps you are looking for something different.

Ant - copy only file not directory

I need to copy all files in a folder except directory in that folder using Ant script.
Im using below script to do that.
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="**/*.*"/>
</fileset>
</copy>
But it copies all files and directory in that folder.
how to restrict/filter directory in that folder?
thanks,
I think there is an easier way.
flatten="true" - Ignore directory structure of source directory, copy all files into a single directory, specified by the todir attribute. The default is false.
Do you mean that srcdir conatins sub-directories, and you you don't want to copy them, you just want to copy the files one level beneath srcdir?
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="*"/>
<type type="file"/>
</fileset>
</copy>
That should work. The "**/*.*" in your question means "every file under every sub directory". Just using "*" will just match the files under srcdir, not subdirectories.
Edited to exclude creation of empty subdirectories.
I do not have enough reputation to comment, so I'm writing new post here. Both solutions to include name="*" or name="*.*" work fine in general, but none of them is exactly what you might expect.
The first creates empty directories that are present in the source directory, since * matches the directory name as well. *.* works mostly because a convention that files have extension and directories not, but if you name your directory my.dir, this wildcard will create an empty directory with this name as well.
To do it properly, you can leverage the <type /> selector that <fileset /> accepts:
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="*"/>
<type type="file"/>
</fileset>
</copy>
Can you try
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="*.*"/>
</fileset>
</copy>
** is used to match a directory structure.
<copy todir="targetsir" includeEmptyDirs="false">
<fileset dir="srcdir">
<include name="*"/>
</fileset>
</copy>
If your folder has many subdirectories and you don't want them to be copied (if you want only files) try this..
<target name="copy">
<copy todir="out" flatten="true">
<fileset dir="tn">
<filename name="**/cit.txt" />
</fileset>
</copy>
</target>
The secret is to use not fileset but dirset instead.

Resources