ant svn 自动更新和发布

团队管理中,当项目非常多,而且发布频繁,手动发布往往非常繁琐,决定用自动化处理。

采用ant +svn+svnant

 

在网络上参考很多人的处理,组合成对自己比较实用的。

准备环境:

1、安装svn client 这个就不多说了。

2、安装ant,在这里我采用ant 1.8.3版本:http://ant.apache.org/

     官网上下载zip 解压缩,并配置环境变量。

 

3、下载svnant ,并解压缩 ,把对应的lib\jar包放入 ant lib 包中。

      下载地址: http://subclipse.tigris.org/files/documents/906/49042/svnant-1.3.1.zip

 

      这步很关键,不然svn命令运行不了!

 

思路:

       1、固定一个目录,专门做发布目录,根据不同项目,在期底下建立与项目名称相同的目录。

       2、建立build等目录,打成war包后删除对应的build包等操作

       3、自动发布到tomcat

 

以下代码实现的是J2EE web工程打包发布

 

<?xml version="1.0" encoding="UTF-8"?>

<project name="deploy" default="autodeploy" basedir=".">
	<property environment="env" />
	<property name="projectName" value="svnant" />
	<property name="temporary.dir" value="D:/SVNTest/${projectName}"/>

	<property name="deployVersion.dir" value="${temporary.dir}/deployVersion"/>
	<property name="tomcat.home" value="d:/apache-tomcat-6.0.32"/>
	<property name="ant-lib.dir" value="F:/apache-ant-1.8.3/lib"/>

	<property name="svn.url" 
                         value="http://subclipse.tigris.org/svn/subclipse/trunk/svnant/"/>
	<property name="svn.name" value="guest"/>
	<property name="svn.password" value=""/>

	<tstamp>
		<format property="deploy.time" pattern="yy-MM-dd HH.mm" />
	</tstamp>

	<delete dir="${temporary.dir}" />
	<mkdir dir="${temporary.dir}" />

	<path id="svnant.dir">
		<fileset dir="${ant-lib.dir}">
			<include name="ganymed.jar" />
			<include name="jna.jar" />

			<include name="svnant.jar" />
			<include name="svnClientAdapter.jar" />
			<include name="svnjavahl.jar" />
			<include name="svnkit.jar" />
		</fileset>
	</path>
	<!-- define the svn task-->
	<taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask"
                       classpathref="svnant.dir" />
	<!-- prepare for the checking out:clean the directory  -->
	<target name="prepare"
		description="Prepare the dirs for the task">
		<delete dir="${temporary.dir}/${projectName}" />
		<mkdir dir="${temporary.dir}/${projectName}" />
	</target>


	<target name="checkout" depends="prepare">
		<echo>+=========checkout start============+</echo>
		<echo>+                                                                      +</echo>
		<echo>+                                                                      +</echo>
		<svn username="${svn.name}" password="${svn.password}" 
                         javahl="false" failonerror="false" svnkit="true">
			<checkout url="${svn.url}" revision="HEAD" 
                                         destPath="${temporary.dir}/${projectName}" />
		</svn>
		<echo>+                                                                      +</echo>
		<echo>+========= checkout end ============+</echo>
	</target>

	<target name="ant_app" depends="checkout">
		<echo>+ ======================== +</echo>
		<echo>+                                                       +</echo>
		<echo>+  anting application to war and ear +</echo>
		<echo>+                                                       +</echo>
		<echo>+ ======================== +</echo>
		<ant dir="./" antfile="build.xml" inheritall="off"/>
	</target>

	<target name="mv_to_pack" depends="ant_app">
		<delete dir="${deployVersion.dir}\${deploy.time}" />
		<mkdir dir="${deployVersion.dir}\${deploy.time}" />
		<move file="${temporary.dir}/${projectName}.war" todir="${deployVersion.dir}/${deploy.time}" />
	</target>
	<target name="execute" depends="mv_to_pack">
	</target>

	<!--shutdown-->
	<target name="shutdown" description="========shutdowntomcat===========">
		<exec executable="${tomcat.home}/bin/shutdown.bat" failonerror="false">
		</exec>
		<sleep seconds="1"/>
		<delete file="${tomcat.home}/webapps/${projectName}.war">
		</delete>
		<delete dir="${tomcat.home}/webapps/${projectName}">
		</delete>
	</target>

	<!--startup-->
	<target name="startup" depends="shutdown" description="========startuptomcat===========">
		<copy file="${deployVersion.dir}/${deploy.time}/${projectName}.war" todir="${tomcat.home}/webapps" />
		<sleep seconds="3"/>
		<exec executable="${tomcat.home}/bin/startup.bat" failonerror="false">
		</exec>
	</target>

	<!--autodeploy-->
	<target name="autodeploy" depends="execute,startup">
		<delete file="${temporary.dir}/${projectName}">
		</delete>
	</target>
</project>
 
<?xml version="1.0" encoding="UTF-8"?>
	<project name="ear" default="deploy" basedir=".">
	 <property environment="env" />
	 <property name="projectName" value="svnant" />	
	 <property name="temporary.dir" value="D:/SVNTest/${projectName}"/>

	 <property name="builder.dir" value="${temporary.dir}/builder" />
	 <property name="project.dir" value="${temporary.dir}/${projectName}" />	
		
	 <property name="src.dir" value="${builder.dir}/src" />
	 <property name="WebRoot.dir" value="${builder.dir}/WebRoot" />
	 <property name="classes.dir" value="${WebRoot.dir}/WEB-INF/classes" />
	 <property name="lib.dir" value="${WebRoot.dir}/WEB-INF/lib" />
		
	 <path id="build.classpath" description="complie 's classpath">
	  <fileset dir="${temporary.dir}/${projectName}/WebRoot/WEB-INF/lib">
	   <include name="*.jar" />
	  </fileset>
	 	<!-- junit jar -->
	 	 <fileset dir="E:\eclipse\plugins">
 		   <include name="org.hamcrest.core_1.1.0.v20090501071000.jar" />
 		  </fileset>
	 	
	 	 <fileset dir="E:\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705">
 		   <include name="junit.jar" />
 		  </fileset>
	 	
	  <pathelement location="${classes.dir}" />
	 </path>
		
	 <target name="copy-files" description="copy,java files">
	  <delete dir="${builder.dir}" />
	  <mkdir dir="${builder.dir}" />
	  <mkdir dir="${classes.dir}" />
	  <mkdir dir="${lib.dir}" />
	  <!-- 
	  <mkdir dir="${WebRoot.dir}/apps" />
	  -->
	  <mkdir dir="${src.dir}" />

	  <copy todir="${src.dir}" overwrite="YES"> 
	   <fileset dir="${project.dir}/src" />
	  </copy>

	  <copy todir="${WebRoot.dir}" overwrite="yes">
	   <fileset dir="${temporary.dir}/${projectName}/WebRoot" excludes=".svn" />
	  </copy>

	 
	 </target>

	 <!-- compile file to share lib -->
	 <target name="compile" depends="copy-files">
	  <mkdir dir="${classes.dir}" />
	 	
	 	
	  <javac fork="true" memoryInitialSize="256m" memoryMaximumSize="500m" srcdir="${src.dir}" destdir="${classes.dir}" includes="**" encoding="UTF-8" debug="true">
	   <classpath refid="build.classpath" />
	  </javac>
	  <copy todir="${classes.dir}">
	   <fileset dir="${src.dir}" excludes="**/*.java" />
	  </copy>
	 	<!-- resources -->	
		<copy todir="${classes.dir}">
		   <fileset dir="${temporary.dir}/${projectName}/config"/>
		</copy>
	 </target>

	 <!-- create the war  -->
	 <target name="web-war" depends="compile" description="create war">
	 	
	 	
	 	
	  <war destfile="${builder.dir}/../${projectName}.war" webxml="${WebRoot.dir}/WEB-INF/web.xml">
	   <fileset dir="${WebRoot.dir}">
	   </fileset>
	  </war>
	 </target>

	 <!-- create the ear   -->
	 <target name="ear" depends="web-war" description="create ear">
	 <!--
	  <ear destfile="${builder.dir}/../${projectName}.ear" 
	  	appxml="${basedir}/META-INF/META-INF/application.xml" 
	  	duplicate="preserve">
	   <fileset dir="${basedir}/META-INF" includes="**/*.*" />
	   <fileset dir="${builder.dir}/..">
	    <include name="${projectName}.war" />
	   </fileset>
	  </ear>
	  -->
	 </target>
 	
	 <target name="deploy" depends="ear">
	  <echo>+ ===================================================== +</echo>
	  <echo>+                                                       +</echo>
	  <echo>+  Building Complete, Cleaning Temporary Files......    +</echo>
	  <echo>+                                                       +</echo>
	  <echo>+ ===================================================== +</echo>
	  <delete dir="${builder.dir}">
	  </delete>
	 </target>

	</project> 
 

 

 

bat :

 

@echo off
del deploy.log
call ant -f ./deploy.xml -debug -l deploy.log

start  deploy.log
 

OK ,搞定!

你可能感兴趣的:(SVN)