Ant真的很强大,通过Ant可以进行ssh和scp操作,这一样来,我们就可以写一些有用的Ant脚本来实现,一些ssh和scp的功能,从而简化我们任务。下面以一个通过Ant来进行远程关闭tomcat、远程拷贝文件和启动tomcat为例来讲:
Ant使用ssh和scp需用到j2ssh和jsch包,在下面将提供j2ssh-0.2.9和jsch0.1.41下载。你需要把下载的j2ssh和jsch拷贝到$ANT_HOME/lib下,如果是Eclipse下的Ant环境必须在Window->Preferences->Ant->Runtime->Classpath中加入j2ssh和jsch。
<project name="project" default="remote-tomcat-start" basedir="."> <property name="host.name" value="131.1.2.217" /> <property name="user.name" value="root" /> <property name="password" value="admin" /> <property name="tomcat.home" value="/home/apache-tomcat-6.0.18" /> <property name="sleep.time" value="10" /> <target name="copy-tomcat-dist"> <scp file="build/workflow.war" trust="true" username="${user.name}" password="${password}" todir="${user.name@${host.name}:${tomcat.home}/webapps" post="22"/> </target> <target name="remote-tomcat-stop"> <sshexec host="${host.name}" username="${user.name}" password="${password}" command="${tomcat.home}/bin/shutdown.sh" trust="true" /> <sleep seconds="${sleep.time}" /> </target> <target name="remote-tomcat-start" depends="remote-tomcat-stop,copy-tomcat-dist"> <sshexec host="${host.name}" username="${user.name}" password="${password}" command="${tomcat.home}/bin/startup.sh" trust="true" /> <sleep seconds="${sleep.time}" /> </target> </project>