I will explain, how you can send files using ANT scipt over SFTP protocol.
Until i find how to use sftp protocol, I googled also ftp protocol. I will explaint it firstly,
If you want to use FTP protocol, you may use <ftp> tag of commons-net-1.4.1.jar. To do this :
- Download commons-net-1.4.1.zip jar from here.
- Add this jar to your ant lib folder using explorer or Define it from eclipse, “Window > Preferences > Ant > Runtime > Global Entries > Add Jar “
- If you do not add this jar, you will get “Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig” error.
- Then write your ftp command to ant script
<target name="deploy" description="War is deploying">
<ftp server="${ftp.host}" port="${ftp.port}" remotedir="${ftp.remotedir}" userid="${ftp.userid}" password="${ftp.password}" depends="yes">
<fileset dir="${maven.build.dir}/war/" />
</ftp>
</target>
- If you use this method for SFTP (Port:22), You will get “error during FTP transfer: org.apache.commons.net.MalformedServerReplyException:
Could not parse response code. Server Reply: SSH-1.99-OpenSSH_3.9p1” error. - So, this method is valid for only FTP protocol.
After the experience below, I keep searching and find SCP task (min Ant 1.6).
- Download jsch.jar0.1.42 or later from here
- You may see examples in the page. This task can used to transfer any data from remote and local machine.
- Add this jar to your ant lib folder using explorer or Define it from eclipse, “Window > Preferences > Ant > Runtime > Global Entries > Add Jar “
- Then write your scp command to ant script
<target name="deploy" description="War is deploying"> <scp file="${war.filepath}" sftp="true" trust="true" todir="userid:password@host:/to/dir/" /> </target>
- If you do not add trust=”true” property, you will get “com.jcraft.jsch.JSchException: reject HostKey” error.
Good coding
I’m setting trust=”true” but still geting “com.jcraft.jsch.JSchException: reject HostKey” error, why this occurs?
You may set the knownhosts property. Check the task page : http://ant.apache.org/manual/Tasks/scp.html