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

本文介绍如何利用Ant脚本实现自动化程序打包和远程服务器上传,通过配置jar和scp任务,简化更新服务器程序的过程,提高效率并减少出错可能性。包括详细步骤、依赖库导入方法及build.xml示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值