在开发过程当中,经常须要同步更新服务器上的程序。若是每次都将程序从新打包,而后再登录服务器进行上传,这样过程显得比较繁琐,特别是更新步骤较多时,很容易出错。咱们能够经过Ant来实现打包和上传过程,若是是与Eclipse集成的,那整个过程将更加简化。 ant脚本 其实整个过程比较简单,主要用到两个task,jar和scp。其中,scp是ant的扩展task,须要第三方的库jsch的支持。能够到http://www.jcraft.com/jsch/index.html进行下载,目前的最新版本为jsch-0.1.34.jar。下载之后,将其放在Ant_Home/lib便可。注意,若是是在Eclipse中使用Ant,须要从新加载Ant_Home,肯定jsch-0.1.34.jar被导入到Eclipse中才能正常使用scp。将该jar包导入到eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib文件夹下面,而后在eclipse的window->references->ant->runtime->classpath下面ant home entries 或者global entries下面引入一下刚才放入的jsch-0.1.34.jar。 具体build.xml以下: <?xml version="1.0" encoding="UTF-8"?> <project name="testForAnt" default="run" basedir="."> <!--properities--> <property name="src.dir" value="src"/> <property name="report.dir" value="report" /> <property name="classes.dir" value="classes" /> <property name="lib.dir" value="lib"/> <property name="dest.dir" value="dest" /> <property name="test_jar" value="test1.jar"/> <property name="remote.user" value="root" /> <property name="remote.password" value="123456" /> <property name="remote.host" value="10.2.41.207" /> <property name="remote.home" value="~" /> <!-- 每次都要找主类 --> <property name="main.class" value="test.Test1"></property> <!-- 基本的编译路径设置 --> <path id="compile.classpath"> <fileset dir="${lib.dir}"> <include name="*.jar" /> </fileset> </path> <!-- 运行路径设置 --> <path id="run.classpath"> <path refid="compile.classpath" /> <pathelement location="${classes.dir}"></pathelement> </path> <!--初始化任务--> <target name="init"> <mkdir dir="${dest.dir}"/> </target> <!--编译--> <target name="compile" depends="init" description="compile the source files"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}" includeAntRuntime="false" > <compilerarg line="-encoding UTF-8" /> <classpath refid="run.classpath"/> </javac> </target> <!--测试--> <target name="test" depends="compile" description="run junit test"> <mkdir dir="${report.dir}"/> </target> <!--打包成jar--> <target name="build" depends="compile"> <jar jarfile="${dest.dir}/${test_jar}" basedir="${classes.dir}"> </jar> </target> <!-- 上传服务器(须要将lib目录下的jsch-0.1.51.jar文件拷贝到$ANT_HOME/lib下,若是是Eclipse下的Ant环境必须在Window->Preferences->Ant->Runtime->Classpath中加入jsch-0.1.51. --> <target name="upload" depends="build" description="upload the file to remote server"> <scp file="${dest.dir}/${test_jar}" todir="${remote.username}@${remote.host}:${remote.home}" password="${remote.password}" trust="true" verbose="false"/> <!-- <scp file="${dest.dir}/test1.jar" todir="${remote.username}:${remote.password}@${remote.host}:${remote.home}" trust="true" verbose="true"/>--> </target> <!-- <target name="sshexec" depends="upload"> <sshexec host="${remote.host}" username="${remote.username}" password="${remote.password}" trust="true" commond="source /etc/profile; sudo -u root hadoop jar ${remote.home}/${test_jar} ${main.class}" </target> --> <target name="run" depends="build"> <java classname="${main.class}" classpath="${dest.dir}/${test_jar}"/> </target> <target name="clean" > <delete dir="${test.dir}" /> </target> <target name="rerun" depends="clean,run"> <ant target="clean" /> <ant target="run" /> </target> </project> 其中,upload任务的trust属性必须设置为true,不然会出现以下错误: com.jcraft.jsch.JSchException