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文件,编写脚本的时候可以作为参考。一个典型的例子如下

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. --><project basedir="." default="build" name="xxxx">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="tomcat.home" value="/var/tomcat/>
    <property name="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>
    <!-- *** svn start ****-->
    <!-- clean up -->  
     <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>  
     <!-- load the svn task -->  
     <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="antclasspath"/>  

     <!--svn checkout-->  
     <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>  
     <!--compile the project-->  

     <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> 
    <!-- **** svn end ** -->

    <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"/>
    <!--deploy to the tomcat project dir-->  
     <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>
     <!--shutdowntomcat-->
     <target name="shutdowntomcat" description="========shutdowntomcat===========">
        <exec executable="${tomcat.home}/bin/shutdown.sh" failonerror="false">
        </exec>
        <sleep seconds="10" />
     </target>
     <!--startuptomcat-->
     <target name="startuptomcat" description="========startuptomcat===========">
         <sleep seconds="5" />
         <exec executable="${tomcat.home}/bin/startup.sh" failonerror="true" >
         </exec>
     </target>
     <!--Done,restart tomcat-->
     <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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值