关于Linux下使用ANT(偷懒的好方式)

    最近公司项目上线了,这一段时间我频繁地按照Release Guide做重复性操作,不胜其烦。今天刚好有空,就用ant加Linux的sh文件做了一个自动Release的脚本,哈哈,以后方便了,一条命令就可以全部搞定。

    首先把ant解压到路径/opt/app下,然后用vi编辑/etc/profile文件,加入以下几个语句:
    export ANT_HOME=/opt/app/apache-ant-1.7.0
    export JAVA_HOME=/opt/app/jdk1.5.0_16
    export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

    按Esc后同时按shift:键,接着输入命令wq退出vi。现在我们可以写ant脚本跟sh文件了

/*  release.sh  */
cd /opt/app/apache-tomcat-5.5.26/bin
./shutdown.sh
killall -9 java         //因为有时候shutdown不能完全关闭tomcat,所以就用这条命令把java线程全部kill掉

kill -TERM `ps -ef | grep 5.5.26 | grep -v grep | awk '{print $2}'`  //或者仅仅kill掉tomcat-5.5.26的线程
rm -rf /opt/app/apache-tomcat-5.5.26/work/Catalina/localhost
cd /opt/app/my
ant all
cd /opt/app/my_1.0_dynamic
unzip  my_dynamic.war
mv /opt/app/my/trunk/my.xml /opt/app/apache-tomcat-5.5.26/conf/Catalina/localhost/     //这个文件指定Context
cd /opt/app/apache-tomcat-5.5.26/bin
./startup.sh

/*  my.xml  */
<?xml version="1.0" encoding="UTF-8"?>

<Context docBase="/opt/app/my_1.0_dynamic" crossContext="true" >
</Context>

/*  build.properties  */
# -----------------------------------------------------------------------------
# build.properties
# This file is referenced by the build.xml file.
# -----------------------------------------------------------------------------

svnant.version=1.0.0

# -----------------------------------------------------------------------------
# all jar needed
# -----------------------------------------------------------------------------
svnlib.dir=trunk/lib/svnant
svnant.jar=${svnlib.dir}/svnant.jar
svnClientAdapter.jar=${svnlib.dir}/svnClientAdapter.jar
svnjavahl.jar=${svnlib.dir}/svnjavahl.jar

svnant.latest.url=http://192.168.1.1/svn/projects/my/coding/trunk/

svnant.repository.user=your username
svnant.repository.passwd=your password

dev.apache.DocumentRoot=/opt/app/httpd-2.2.9-ssl-rewrite/htdocs

/*  build.xml  */
<?xml version="1.0"?>

<project name="myProject" basedir="." default="usage">

    <!--  all properties are in build.properties -->
    <property file="build.properties" />

    <property name="name" value="${ant.project.name}"/>

    <property name="src.dir" value="trunk/src"/>
    <property name="src.webapp" value="trunk/web"/>
    <property name="lib.dir" value="trunk/lib"/>
    <property name="static.dir" value="trunk/static"/>
   
    <property name="build.dir" value="trunk/build"/>
    <property name="build.webapp" value="${build.dir}/webapp"/>
    <property name="build.classes" value="${build.webapp}/WEB-INF/classes"/>

    <property name="dist.dir" value="/opt/app/my_1.0_dynamic"/>

    <!-- path to the svnant libraries. -->
    <path id="svnant.classpath">
        <pathelement location="${svnjavahl.jar}" />
        <pathelement location="${svnant.jar}" />
        <pathelement location="${svnClientAdapter.jar}" />
    </path>
    <taskdef resource="svntask.properties" classpathref="svnant.classpath"/>   
   
    <!-- another way is to put all lib to ant_home/lib and just uncomment the following line. -->
    <!-- <taskdef resource="svntask.properties"/>-->
 
  <target name="checkout">
    <svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
      <checkout url="${svnant.latest.url}" revision="HEAD" destPath="trunk" />
    </svn>
  </target>
 

    <path id="master-classpath">
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/test">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${lib.dir}/servlet">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="usage">
        <echo message=""/>
        <echo message="my build"/>
        <echo message="------------------------------------------------------"/>
        <echo message=""/>
        <echo message="Available targets are:"/>
        <echo message=""/>
        <echo message="clean    --> Clean output dirs"/>
        <echo message="checkout --> Check out latest source from svn server"/>
        <echo message="build    --> Compile main Java sources and copy libraries"/>
        <echo message="war      --> Build a war file"/>
        <echo message="all      --> Clean, checkout,build, war, tests"/>
        <echo message=""/>
    </target>

    <target name="init" description="Clean output dirs (.classes and .testclasses)">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${build.webapp}"/>
        <mkdir dir="${build.webapp}/WEB-INF"/>
        <mkdir dir="${build.webapp}/WEB-INF/lib"/>
        <mkdir dir="${build.classes}"/>
    </target>
   
    <target name="clean" description="Clean output dirs (.classes and .testclasses)">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
        <delete>
            <fileset dir="${src.webapp}/WEB-INF/lib" includes="**/*.jar"/>
        </delete>
        <delete dir="${src.webapp}/WEB-INF/classes/au"/>
    </target>

    <target name="build" depends="init" description="Compile main source tree java files into class files, generate jar files">
        <javac destdir="${build.classes}" source="1.5" target="1.5" debug="true"
               deprecation="false" optimize="false" failοnerrοr="true" encoding="utf-8">
            <src path="${src.dir}"/>
        <classpath refid="master-classpath"/>
        </javac> 
    </target>

    <target name="webapp" depends="init, build">
        <copy todir="${build.webapp}">
            <fileset dir="${src.webapp}"/>
        </copy>
        <copy todir="${build.webapp}/WEB-INF/lib">
            <fileset dir="${lib.dir}">
                <exclude name="**/servlet*"/>
                <exclude name="**/servlet/*"/>
                <exclude name="**/test*"/>
                <exclude name="**/test/*.jar"/>
                <exclude name="**/svnant/*.jar"/>
                <exclude name="**/svnant*"/>
            </fileset>
        </copy>
        <copy todir="${build.webapp}/WEB-INF/classes">
            <fileset dir="${src.dir}">
                <include name="**/resources/*"/>
            </fileset>
        </copy>
        <copy todir="${build.webapp}/WEB-INF/classes">
            <fileset dir="${src.dir}">
                <include name="**/*validation.xml"/>
            </fileset>
        </copy>
        <copy todir="${dev.apache.DocumentRoot}">
            <fileset dir="${static.dir}">
            </fileset>
        </copy>
    </target>

    <target name="war" depends="init, build, webapp">
        <jar jarfile="${build.webapp}/WEB-INF/lib/my.jar" basedir="${build.webapp}/WEB-INF/classes" >
        <include name="**/au/**"/>
        <include name="**/com/**"/>
        </jar>
        <jar jarfile="${build.dir}/my_dynamic.war" basedir="${build.webapp}" >
                <exclude name="**/jai_*.jar"/>
        <exclude name="**/classes/au/**"/>
        <exclude name="**/classes/com/**"/>
        </jar>
  </target>

    <target name="dist" depends="init, build, webapp, war">
           <mkdir dir="${dist.dir}"/>
        <copy file="${build.dir}/my_dynamic.war" todir="${dist.dir}"/>
     </target>


    <target name="all" depends="clean,checkout,dist" description="Clean,checkout,build,warfile,dist"/>
   
</project>
虽然很简单,不过好记性不如烂笔杆,记下来留作参考,呵呵!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值