ant + svn 自动部署项目

本文介绍如何使用Ant结合SVN实现Java项目的自动化构建与部署。通过定义build.properties和build.xml文件,实现从SVN检出代码、编译、打包到Tomcat部署的全过程自动化。

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

ant + svn 自动部署项目

Java代码   收藏代码
  1. build.properties  
  2.   
  3. # -----------------------------------------------------------------------------  
  4. # build.properties  
  5. # This file is referenced by the sample build.xml file.  
  6. # -----------------------------------------------------------------------------  
  7.   
  8. build.version=1.0.0  
  9.    
  10. #you need these jars  
  11. svnant.jar=/usr/local/ant/lib/svnant.jar  
  12. svnClientAdapter.jar=/usr/local/ant/lib/svnClientAdapter.jar  
  13. svnjavahl.jar=/usr/local/ant/lib/svnjavahl.jar  
  14. javaEE.lib=/opt/tomcat-7/lib  
  15.    
  16. #tomcat information  
  17. #tomcat home  
  18. tomcat.home=/opt/tomcat-7  
  19. #tomcat project deploy dir                
  20. tomcat.deploy=${tomcat.home}/webapps/${ant.project.name}  
  21.    
  22. debuglevel=source,lines  
  23. target=1.6  
  24. source=1.6  
  25.    
  26. work.space=/tmp/project  
  27.    
  28. build.dir=${work.space}/WebRoot/WEB-INF/classes  
  29. lib.dir=${work.space}/WebRoot/WEB-INF/lib  
  30.    
  31. java.source=${work.space}/src  
  32. java.config=${work.space}/config  
  33.    
  34. web.dir=${work.space}/WebRoot  
  35. resource.dir=${work.space}/resources  
  36.   
  37. zip.file=${tomcat.deploy}/${ant.project.name}.zip  
  38.    
  39. #project information in SVN   
  40. urlRepos=svn://192.168.1.114/project/lottery  
  41. svn.user=xxx  
  42. svn.passwd=xxxx  
  43.   
  44.   
  45. build.xml  
  46.   
  47. <?xml version="1.0" encoding="UTF-8"?>  
  48. <project basedir="." name="lottery" default="auto">  
  49.  <!--  all properties are in build.properties -->  
  50.  <property file="build.properties" />  
  51.  <!-- svn runtime libs -->  
  52.  <path id="svnant.lib">    
  53.          <pathelement location="${svnjavahl.jar}" />    
  54.          <pathelement location="${svnant.jar}" />    
  55.          <pathelement location="${svnClientAdapter.jar}" />    
  56.  </path>    
  57.  <!--java EE  库 -->    
  58.      <path id="javaEE">    
  59.          <fileset dir="${javaEE.lib}">    
  60.              <include name="**/*.jar" />    
  61.          </fileset>    
  62.      </path>    
  63.   
  64.  <!-- load the libs in classpath -->  
  65.  <path id="project.classpath">  
  66.  <pathelement location="${build.dir}"/>  
  67.  <fileset dir="${lib.dir}"/>  
  68.  </path>  
  69.    
  70.  <!-- clean up -->  
  71.   
  72.  <target name="clear">  
  73.  <delete dir="${work.space}"></delete>  
  74.  <delete dir="${tomcat.home}/work/Catalina/localhost/${ant.project.name}"></delete>  
  75.  <delete dir="${tomcat.deploy}/${ant.project.name}"></delete>  
  76.  <delete dir="${tomcat.deploy}/${ant.project.name}.war"></delete>  
  77.  </target>  
  78.  <!-- load the svn task -->  
  79.  <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classdata-path="svnant.jar" />  
  80.  <svnSetting id="svn.settings" javahl="false" svnkit="true" username="${svn.user}" password="${svn.passwd}" />  
  81.   
  82.  <!--svn checkout-->  
  83.  <target name="svn" depends="clear">  
  84.  <mkdir dir="${work.space}"/>  
  85.  <svn refid="svn.settings">  
  86.  <checkout url="${urlRepos}" destdata-path="${work.space}" />  
  87.  </svn>  
  88.  </target>  
  89.  <!--compile the project-->  
  90.    
  91.  <target name="compile" depends="svn" description="----------compile ${ant.project.name}-----------">  
  92.  <echo message="----------compile ${ant.project.name}:${ant.file}-----------"></echo>  
  93.  <mkdir dir="${build.dir}"/>  
  94.  <copy includeemptydirs="false" todir="${build.dir}">  
  95.  <fileset dir="${java.source}" excludes="**/*.launch, **/*.java"></fileset>  
  96.  <fileset dir="${java.config}" excludes="**/*.launch, **/*.java"></fileset>  
  97.  </copy>  
  98.    
  99.  <javac  includeantruntime="false" includejavaruntime="true" debug="true" debuglevel="${debuglevel}" destdir="${build.dir}" source="${source}" target="${target}" encoding="utf-8">    
  100.              <src data-path="${java.source}" />    
  101.              <exclude name="config/"/>    
  102.              <classpath>    
  103.                  <path refid="project.classpath">    
  104.                  </path>    
  105.                  <path refid="javaEE">    
  106.                  </path>    
  107.      
  108.              </classpath>    
  109.              <compilerarg value="-Xlint:unchecked"/>    
  110.          <compilerarg value="-Xlint:deprecation"/>    
  111.      </javac>  
  112.    
  113.  </target>  
  114.  <!--compress and pack -->  
  115.  <!--deploy to the tomcat project dir-->  
  116.     <target name="deploy" depends="compile">  
  117.      <mkdir dir="${tomcat.home}/webapps/${ant.project.name}"/>  
  118.      <zip destfile="${zip.file}">  
  119.           <zipfileset dir="${web.dir}"/>  
  120.      </zip>  
  121.      <unzip dest="${tomcat.home}/webapps/${ant.project.name}" overwrite="true" src="${zip.file}"/>  
  122.        
  123.  </target>  
  124.    
  125.  <!--shutdowntomcat-->  
  126.   
  127.  <target name="shutdowntomcat" description="========shutdowntomcat===========">  
  128.  <exec executable="${tomcat.home}/bin/shutdown.sh" failonerror="false">  
  129.  </exec>  
  130.  <sleep seconds="10" />  
  131.  </target>  
  132.  <!--startuptomcat-->  
  133.   
  134.  <target name="startuptomcat" description="========startuptomcat===========">  
  135.  <sleep seconds="5" />  
  136.  <exec executable="${tomcat.home}/bin/startup.sh" failonerror="true" >  
  137.  </exec>  
  138.  </target>  
  139.   
  140.  <!--Done,restart tomcat-->  
  141.   
  142.  <target name="auto" depends="shutdowntomcat,deploy,startuptomcat">  
  143.  <echo message="All DONE!!!!" />  
  144.  </target>  
  145. </project>  

 

  在安装了ant的环境下面运行 build.xml

 

上面的tomcat的目录需要自己制定,svn的信息也需要自己设定,配置信息都在build.properties文件中



转载地址:http://chen106106.iteye.com/blog/1912426

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值