使用Ant實現打包jar包上傳到服務器

在開發過程當中,經常須要同步更新服務器上的程序。若是每次都將程序從新打包,而後再登錄服務器進行上傳,這樣過程顯得比較繁瑣,特別是更新步驟較多時,很容易出錯。咱們能夠經過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
相關文章
相關標籤/搜索