首先设置Ant环境变量配置,可在Windows cmd 下执行如下命令:
set ANT_HOME=E:\OpenSource\Ant\apache-ant-1.8.4 set CLASSPATH=%CLASSPATH%;%ANT_HOME%\lib
(1)通过FTP上传:
FTP上传依赖JAR包:
<!-- 通过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%\bin ant.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包:
使用SCP上传需要先导入SSH/SSH2的相关账号和密码配置:
SSH2配置<ssh2.properties>
ssh2.hostname=192.168.134.191 ssh2.userid=admin ssh2.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%\bin ant.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协议原理。