使用Ant实现打包jar包上传到服务器
生活随笔
收集整理的這篇文章主要介紹了
使用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如下:
[java]?view?plain?copy?在CODE上查看代碼片派生到我的代碼片
<?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
轉載于:https://blog.51cto.com/shu1983/1905217
總結
以上是生活随笔為你收集整理的使用Ant实现打包jar包上传到服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过钉钉群聊机器人推送zabbix告警
- 下一篇: awk 实例演示