ANT实现自动化软件部署

  1. JDK安装;
  2. ANT安装;
  3. 环境变量设置;
  4. ANT脚本编写;
  5. 执行自动化的部署。

JDK的安装

linux下安装jdk十分简单,只需要到oracle的技术网站下载.tar.gz格式的jdk就可以。然后一般在

/usr/lib/jvm/

目录下,执行
tar -zxvf filename
命令。

ANT的安装

与jdk一样的进行,下载一个ant,然后解压缩,设置环境变量 (单独在后面设置)。
这里需要注意的是,在使用svn的时候,需要svn的插件,叫svnant,这个可以从网上下载,然后将下载来的jar文件都放到ant下的Lib目录中就可以了。

设置环境变量

执行

# vim /etc/profile

然后设置环境变量 如下:

JAVA_HOME=/usr/lib/jvm/jdk7
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

ANT_HOME=/data/ant/apache-ant-1.9.6
PATH=$ANT_HOME/bin:$PATH
export ANT_HOME
export JAVA_HOME
export CLASSPATH
export PATH

然后,执行

# java -version
# ant -version

检查是否装成功了。

ANT脚本编写

这里有一个窍门,因为现在的JAVA工程都是使用eclipse进行开发的,而最新的eclipse内置集成了对于ANT的支持,所以在

FILE->export

中可以选择导出为ant的build.xml文件,编写脚本的时候可以作为参考。一个典型的例子如下


<project basedir="." default="build" name="xxxx">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="tomcat.home" value="/var/tomcat/>
    tomcatlib" value="${tomcat.home}/lib"/>
    <property name="antlib" value="/data/ant/apache-ant-1.9.6/lib"/>
    <property name="webapp" value="WebRoot"/>
    <property name="webapplib" value="${webapp}/WEB-INF/lib"/>
    <property name="deploy" value="/data/forPatentBack"/>
    <property name="work.space" value="."/>
    <property name="svn.url" value="svn://XXXXX.ddd.net/project"/>
    <property name="svn.user" value="XXXX"/>
    <property name="svn.passwd" value="YYYYY"/>
    <property name="target" value="1.7"/>
    <property name="source" value="1.7"/>

    <path id="antclasspath">
        <fileset dir="${antlib}" includes="***.jar">fileset>
    path>
    
      
     <target name="clear"> 
         <echo message="----------clean up dirs-----------">echo>   
         <delete dir="${work.space}/src">
         delete>  
         <delete dir="${work.space}/WebRoot">
         delete> 
         <delete dir="${deploy}">
         delete>   
     target>  
       
     <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="antclasspath"/>  

       
     <target name="svncheckout" depends="clear">
        <mkdir dir="${work.space}"/>
        <echo message="---------- checkout patent -----------">echo>
        <svnSetting id="svnparams" username="${svn.user}" password="${svn.passwd}" javahl="false" svnkit="true"/>  
        <svn refid="svnparams">  
          <checkout url="${svn.url}" destPath="${work.space}" force="true" revision="HEAD" />  
        svn>  
        <echo message="---------- checkout complete -----------">echo>
     target>  
       

     <target name="delsvn" depends="svncheckout">  
      <echo message="Delete *.svn file and folder">echo>  
      <delete verbose="true" includeemptydirs="true">  
        <fileset dir="${work.space}" includes="**/.svn/" defaultexcludes="false"/>  
      delete> 
    target> 
    

    <path id="t7.userclasspath">
        <fileset dir="${tomcatlib}" includes="***.jar">fileset>
    path>
    <path id="patentTrunk.classpath">
        <pathelement location="${webapp}/WEB-INF/classes"/>
        <fileset dir="${webapplib}" includes="***.jar">fileset>
        <fileset dir="${antlib}" includes="***.jar">fileset>
        <path refid="t7.userclasspath"/>
    path>
    <target name="init">
        <mkdir dir="${webapp}/WEB-INF/classes"/>
        <copy includeemptydirs="false" todir="WebRoot/WEB-INF/classes">
            <fileset dir="src">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            fileset>
        copy>
    target>
    <target name="clean">
        <delete dir="${webapp}/WEB-INF/classes"/>
    target>
    <target depends="clean" name="cleanall"/>
    <target depends="delsvn,cleanall,build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="${webapp}/WEB-INF/classes" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="patentTrunk.classpath"/>
            <compilerarg line="-encoding UTF-8"/>
        javac>
    target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
      
     <target name="deploy" depends="build">  
       <echo message="----------copy build classes to ${deploy.dir} complete -----------">echo>  
        
        <copy includeemptydirs="false" todir="${deploy}">
         <fileset dir="${webapp}" >fileset>
       copy>
     target>
     
     <target name="shutdowntomcat" description="========shutdowntomcat===========">
        <exec executable="${tomcat.home}/bin/shutdown.sh" failonerror="false">
        exec>
        <sleep seconds="10" />
     target>
     
     <target name="startuptomcat" description="========startuptomcat===========">
         <sleep seconds="5" />
         <exec executable="${tomcat.home}/bin/startup.sh" failonerror="true" >
         exec>
     target>
     
     <target name="auto" depends="shutdowntomcat,deploy,startuptomcat,showlogs">
        <echo message="All DONE!!!!" />
     target>
     <target name="showlogs">  
        <exec executable="tail">  
            <arg value="-f" />  
            <arg value="${tomcat.home}/logs/catalina.out" />  
        exec>  
    target>  
project>

你可能感兴趣的:(ant)