使用Ant实现打包jar包上传到服务器

来源:互联网 发布:印度火箭 知乎 编辑:程序博客网 时间:2024/04/19 07:25

在开发过程中,常常需要同步更新服务器上的程序。如果每次都将程序重新打包,然后再登陆服务器进行上传,这样过程显得比较繁琐,特别是更新步骤较多时,很容易出错。我们可以通过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

0 0
原创粉丝点击