完整Ant自动化归档脚本

刚做公司的时候,项目还没有实现自动打包,需要自己手工的上传代码到服务器上,然后再解压,在转码付权限,很是麻烦,以前也写过一个转码付权限的帖子,但是不全面,现在把一个完整的ant脚本贴出,备用,注意,执行这个脚本需要依赖2个包:commons-net-1.4.1.jar;jakarta-oro-2.0.8.jar.

<project name="project" default="all" basedir=".">
	<!--工程根目录-->
	<property name="base" value="." />
	<!--本次安装包包名,注意修改日期-->
	<property name="ins_name" value="project_INS" />
	<!--本次源码包包名,注意修改日期-->
	<property name="src_name" value="project_SRC" />
	<!--打包标签,如project迭代一归档-->
	<property name="vss.label" value="project xxx 归档" />
	<!--配置库登陆用户名-->
	<property name="vss.username" value="name" />
	<!--配置库登陆密码-->
	<property name="vss.password" value="password" />
	<!--本地vss的ss.exe所在目录-->
	<property name="vssdir" location="C:/Program Files/Microsoft Visual Studio/VSS/win32" />
	<!--配置库中工程所在结构目录-->
	<property name="vsspath" value="/1.CI/1.04Code/project" />
	<!--vss上项目工程目录,包含srcsafe.ini文件的路径-->
	<property name="serverPath" location="\\10.10.10.10/vss/project" />
	<!--默认打包服务器-->
	<property name="server_address" value="10.10.10.20" />
	<!--登陆用户-->
	<property name="login_name" value="root" />
	<!--登陆用户秘码-->
	<property name="login_pwd" value="password" />
	<!--服务器打包文件夹名,注意此文件加在打包过程中会先删除再创建-->
	<property name="package_name" value="projectMember" />
	<!--服务器命令回显-->
	<property name="read_rsp" value="linuxxxx" />
	<!--打包临时目录-->
	<property name="package_temp" value="project_package" />
	<!--源文件目录-->
	<property name="src" value="${package_temp}/src" />
	<!--classes目录-->
	<property name="classes" value="${package_temp}/classes" />
	<!--配置文件目录-->
	<property name="conf" value="${package_temp}/config" />
	<!--外部包目录-->
	<property name="lib" value="${package_temp}/lib" />

	<!--自动化打包-->
	<target name="all" depends="downloadLocation" />

	<!--打包初始化-->
	<target name="init">
		<!-- 删除缓存文件 -->
		<delete dir="${package_temp}" />
		<!-- 格式化当天时间 -->
		<tstamp>
			<format property="TODAY" pattern="yyyyMMdd" />
		</tstamp>
		<!-- 定义包名,含当天时间 -->
		<property name="ins_package" value="${ins_name}_${TODAY}" />
		<property name="src_package" value="${src_name}_${TODAY}" />
	</target>

	<!-- 在配置库上设置标签并从标签处获取最新代码 -->
	<target name="VssGetFile" depends="init">
		<vsslabel label="${TODAY}_${vss.label}" vsspath="${vsspath}" login="${vss.username},${vss.password}" ssdir="${vssdir}" serverPath="${serverPath}" autoresponse="Y" />
		<vssget label="${TODAY}_${vss.label}" localPath="${package_temp}" serverPath="${serverPath}" ssdir="${vssdir}" login="${vss.username},${vss.password}" vsspath="${vsspath}" recursive="true" quiet="true" writable="true" />
	</target>

	<!-- 源码包 -->
	<target name="zip_src" depends="VssGetFile">
		<delete>
			<fileset dir="${base}">
				<include name="**/*.scc" />
				<include name="${base}/${src_package}.zip" />
			</fileset>
		</delete>
		<!-- 删除只用Ant才使用的两个包 -->
		<delete file="${lib}/commons-net-1.4.1.jar" />
		<delete file="${lib}/jakarta-oro-2.0.8.jar" />
		<zip zipfile="${base}/${src_package}.zip" basedir="${package_temp}" includes="**" />
	</target>

	<!-- 编译 -->
	<target name="compile" depends="zip_src">
		<delete dir="${classes}" />
		<mkdir dir="${classes}" />
		<javac srcdir="${src}" destdir="${classes}">
			<classpath path="">
				<fileset dir="${lib}">
					<include name="*.jar" />
					<include name="*.zip" />
				</fileset>
			</classpath>
		</javac>
	</target>

	<!-- 清除不要的文件 -->
	<target name="delete" depends="compile">
		<delete file="${lib}/jakarta-oro-2.0.8.jar" />
		<delete file="${lib}/commons-net-1.4.0.jar" />
		<delete dir="${src}" />
		<delete dir="${package_temp}/test" />
		<delete>
			<fileset dir="${package_temp}">
				<include name="*build.xml" />
			</fileset>
		</delete>
	</target>

	<!-- 如果无服务器可以直接在本地打zip包 -->
	<target name="zipFile" depends="delete">
		<delete file="${base}/${ins_package}.zip" />
		<zip zipfile="${base}/${ins_package}.zip" basedir="${base}/${package_temp}" includes="**" />
	</target>

	<!-- 初始化服务器目录 -->
	<target name="initServer" depends="zipFile">
		<telnet userid="${login_name}" password="${login_pwd}" server="${server_address}">
			<read>${read_rsp}</read>
			<write echo="true">rm -rf ${package_name}</write>
			<read>${read_rsp}</read>
			<write echo="true">mkdir ${package_name}</write>
			<read>${read_rsp}</read>
			<write echo="true">cd ${package_name}</write>
			<read>${read_rsp}</read>
		</telnet>
	</target>
	<!-- 将本地zip文件ftp到服务器指定目录下 -->
	<target name="ftpServer" depends="initServer">
		<ftp server="${server_address}" remotedir="${package_name}" userid="${login_name}" password="${login_pwd}">
			<fileset file="${base}/${ins_package}.zip" />
		</ftp>
	</target>

	<!-- 在服务器指定目录下对工程进行dos2unix,chmod,以及打包 -->
	<target name="buildGzip" depends="ftpServer">
		<telnet userid="${login_name}" password="${login_pwd}" server="${server_address}">
			<read>${read_rsp}</read>
			<write echo="true">cd ${package_name}</write>
			<read>${read_rsp}</read>
			<write echo="true">unzip ${ins_package}.zip</write>
			<read>${read_rsp}</read>
			<write echo="true">rm -rf ${ins_package}.zip</write>
			<read>${read_rsp}</read>
			<write echo="true">cd bin</write>
			<read>${read_rsp}</read>
			<write echo="true">dos2unix *</write>
			<read>${read_rsp}</read>
			<write echo="true">chmod 777 *</write>
			<read>${read_rsp}</read>
			<write echo="true">cd ../config</write>
			<read>${read_rsp}</read>
			<write echo="true">dos2unix *</write>
			<read>${read_rsp}</read>
			<write echo="true">chmod 777 *</write>
			<read>${read_rsp}</read>
			<write echo="true">cd ../</write>
			<read>${read_rsp}</read>
			<write echo="true">tar -zcvf ${ins_package}.tar.gz ./*</write>
			<read>${read_rsp}</read>
		</telnet>
	</target>

	<!-- 将服务器指定目录下打好的gz包下载到本地 -->
	<target name="downloadLocation" depends="buildGzip">
		<delete file="${base}/${ins_package}.tar.gz" />
		<ftp action="get" server="${server_address}" userid="${login_name}" password="${login_pwd}" remotedir="${package_name}">
			<fileset file="${ins_package}.tar.gz" />
		</ftp>
		<!-- 删除本地临时zip包 -->
		<delete file="${base}/${ins_package}.zip" />
	</target>
</project>

相关推荐