Ant上传文件到远程服务器

来源:互联网 发布:cms监控软件使用说明 编辑:程序博客网 时间:2024/05/20 18:44
Ant上传文件到服务器常见有两种方式:[FTP / SCP]

 首先设置Ant环境变量配置,可在Windows cmd 下执行如下命令: 

set ANT_HOME=E:\OpenSource\Ant\apache-ant-1.8.4set CLASSPATH=%CLASSPATH%;%ANT_HOME%\lib

(1)通过FTP上传:

FTP上传依赖JAR包:

  • commons-net-3.2.jar :可到 http://commons.apache.org/proper/commons-net/download_net.cgi 去下载
  • ant-commons-net.jar :在%ANT_HOME%\lib下可找到
 <!-- 通过FTP上传打包的EAR文件. --> <target name="ftpUpload" description="Upload TestWebEAR.ear to server">     <ftp server="192.168.134.190" action="put" passive="true" remotedir="/appdisk/SIT_EARs" userid="root" password="123456"          separator="\" verbose="yes" binary="yes">         <fileset dir="D:\AntTest">             <include name="**/*.*" />         </fileset>     </ftp> </target>

注:如果你的机器是在FireWall(防火墙)保护下,FTP上传时,请设置:passive="true"

执行Ant [ftpUpload] target (Windows命令):

cd /d %ANT_HOME%\binant.bat -f D:\AntTest\build.xml -lib D:\AntTest\lib\commons-net-3.2.jar ftpUpload

Buildfile: D:\AntTest\build.xml

ftpUpload:

_

漫长的等待上传进度:5kb/s


(2)通过SCP上传(推荐):

SCP上传依赖JAR包:

  • ant-jsch.jar       :在%ANT_HOME%\lib下可找到
  • jsch-0.1.49.jar :可点击 http://nchc.dl.sourceforge.net/project/jsch/jsch.jar/0.1.49/jsch-0.1.49.jar 下载

使用SCP上传需要先导入SSH/SSH2的相关账号和密码配置:

 SSH2配置<ssh2.properties>

ssh2.hostname=192.168.134.191ssh2.userid=adminssh2.password=admin ssh2.remotedir=/appdisk/SIT_EARs

Ant Target配置:

<property file="ssh2.properties" /><!-- 通过SCP上传打包的EAR文件. --><target name="scpUpload" description="Upload TestWebEAR.ear to server"><scp file="D:\AntTest\TestWebEAR.ear" todir="${ssh2.userid}:${ssh2.password}@${ssh2.hostname}:${ssh2.remotedir}" trust="true" /></target>

执行Ant [scpUpload] target (Windows命令):

cd /d %ANT_HOME%\binant.bat -f D:\AntTest\build.xml -lib D:\AntTest\lib\jsch-0.1.49.jar scpUpload

Buildfile: D:\AntTest\build.xmlscpUpload:
      [scp] Connecting to 192.168.134.191:22
      [scp] done.

BUILD SUCCESSFUL
Total time: 10 seconds

 完整的build.xml

<?xml version="1.0" encoding="UTF-8"?><project name="TestWeb" basedir="."><!-- 通过FTP上传打包的EAR文件. --><target name="ftpUpload" description="Upload TestWebEAR.ear to server">    <ftp server="192.168.134.190" action="put" passive="true" remotedir="/appdisk/SIT_EARs" userid="root" password="123456"         separator="\" verbose="yes" binary="yes">        <fileset dir="D:\AntTest">            <include name="**/*.*" />        </fileset>    </ftp></target><property file="ssh2.properties" /><!-- 通过SCP上传打包的EAR文件. --><target name="scpUpload" description="Upload TestWebEAR.ear to server"><scp file="D:\AntTest\TestWebEAR.ear" todir="${ssh2.userid}:${ssh2.password}@${ssh2.hostname}:${ssh2.remotedir}" trust="true" /></target></project>


小结:

    在上面的例子中,通过SCP方式比FTP方式要快很多,在上面的例子中TestWebEAR.ear有44M左右,FTP上传大约5kb/s,而通过SCP方式,上传只用了大约10秒钟就上传完了。


想了解SCP详细内容,可参考:SCP简介 或者SCP协议原理。

原创粉丝点击