`

自用Ant脚本 自动编译打包部署

    博客分类:
  • Ant
 
阅读更多

ant.properties

 

# JDK 路径
jdk.home.1.5=D\:/Java/jdk1.5.0_22

#项目名称
webapp.name=Demo

# war 排除的文件
war.exclude=**/WEB-INF/web.xml,**/WEB-INF/weblogic.xml,**/WEB-INF/resources/FineReport.lic,**/WEB-INF/resources/datasource.xml
war.exclude.classes= **/WEB-INF/classes/App.properties,**/WEB-INF/classes/log4j.properties,**/WEB-INF/classes/DbConfig.properties,**/WEB-INF/classes/JoltConfig.properties


#ftp(普通 FTP上传) #
ftp.server=192.168.1.202
ftp.password=1234
ftp.userid=1234
ftp.path=/home/app/

#ssh ( SFTP 上传) #
ssh.host=127.0.0.1
ssh.path=/home/app/bea/wls/user_projects/domains/bzsk/webapp
ssh.pwd=password
ssh.uname=username

# 执行ssh 命令   #
#ssh.path.webapp=/home/app/bea/wls/user_projects/domains/bzsk/webapp
ssh.server.bin=/home/app/bea/wls/user_projects/domains/bzsk/bin
ssh.cmd.openwar=source ~/.bash_profile ;cd / ; cd ${ssh.path} ; jar -xvf
ssh.server.start= cd / ; cd ${ssh.server.bin} ; ./startWebLogic.sh
ssh.server.stop= cd / ; cd ${ssh.server.bin} ; ./stopWebLogic.sh

 

   ant.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="usage" name="${project.name}">

    <!--下句是import进ant属性配置文件,properties文件里存放基本的配置变量。该变量可以在build.xml中直接引用 -->
    <property file="bzsk.properties"/>


    <!--以下的几个属性是系统自带的,初始了tstamp之后,它们就有值了
     ${DSTAMP}    ${TSTAMP}      ${TODAY}
     -->
    <tstamp/>
    <property name="war.name" value="${webapp.name}${DSTAMP}" />

    <!-- =================================================================== -->
    <!-- Help on usage -->
    <!-- =================================================================== -->
    <target name="usage" depends="init">
        <echo message="ant build ${project.name} -- ${DSTAMP}"/>
    </target>

    <target name="init">

        <property name="debug" value="off"/>
        <property name="optimize" value="on"/>
        <property name="deprecation" value="on"/>

        <!--    java源文件路径 -->
        <property name="src.dir" value="${basedir}/src"/>

        <!--    jar包路径 -->
        <property name="lib.dir" value="${basedir}/lib"/>

        <!--    webapp路径 -->
        <property name="webapp.dir" value="${basedir}/jsp"/>
        <property name="packages" value="com.*"/>

        <!--    准备源文件路径 -->
        <property name="build.src" value="${basedir}/AntBuild/build"/>

        <!--    编译源文件路径 -->
        <property name="build.dest" value="${basedir}/AntBuild/bin"/>

        <!--    准备webapp文件路径 -->
        <property name="buildwar.dest" value="${basedir}/AntBuild/warsrc"/>


        <!--    打包war文件路径 -->
        <property name="war.dest" value="${basedir}/AntBuild/war"/>

        <!--    jre lib路径 -->
        <property name="jre.lib" value="${jdk.home.1.5}/jre/lib"/>


        <!--    classpath -->
        <path id="classpath">
            <!--j2ee-->
            <!--<fileset dir="${j2eelib.dir}">-->
            <!--<include name="**/*.jar"/>-->
            <!--</fileset>-->

            <fileset dir="${jre.lib}">
                <include name="**/*.jar"/>
            </fileset>

            <fileset dir="${lib.dir}">
                <include name="**/*.jar"/>
            </fileset>
            <!--<pathelement location="lib/"/>-->
        </path>

    </target>


    <!-- =================================================================== -->
    <!-- 编译源文件-->
    <!-- =================================================================== -->
    <target name="build" depends="init">
        <mkdir dir="${buildwar.dest}/WEB-INF/classes"/>
        <delete>
            <fileset dir="${buildwar.dest}/WEB-INF/classes" includes="**/*.*"/>
        </delete>
        <javac srcdir="${src.dir}" destdir="${buildwar.dest}/WEB-INF/classes" debug="${debug}" optimize="${optimize}">
            <classpath refid="classpath"/>
        </javac>
        <copy todir="${buildwar.dest}/WEB-INF/classes">
            <fileset dir="${src.dir}">
                <include name="**/*.*"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>


    <!-- =================================================================== -->
    <!-- 打war包-->
    <!-- =================================================================== -->

    <target name="ant.war" depends="build">
        <delete>
            <fileset dir="." includes="**/*.war"/>
        </delete>

        <!--needxmlfile 设为false才不会报错   web.xml不存在 ant会报错-->
        <war destfile="${war.name}.war" needxmlfile="false">
            <lib dir="${basedir}/lib"/>
            <classes dir="${buildwar.dest}/WEB-INF/classes" excludes="${war.exclude.classes}"/>
            <fileset dir="${webapp.dir}" excludes="${war.exclude}"/>
        </war>

    </target>


    <!-- =================================
    target: upload with  ftp
   ================================= -->
    <target name="upload-ftp" depends="ant.war">
        <ftp password="${ftp.password}"
             server="${ftp.server}"
             userid="${ftp.userid}"
             remotedir="${ftp.path}"
             binary="true">

            <fileset file="${basedir}/${war.name}.war"/>
        </ftp>
    </target>


    <!-- ===================================================================
            上传本地文件到远程服务器,执行远程命令
        =================================================================== -->
    <target name="upload_ssh" depends="ant.war">
        <!--上传-->
        <scp file="${basedir}/${war.name}.war" todir="${ssh.uname}:${ssh.pwd}@${ssh.host}:${ssh.path}"
             trust="true"/>

        <!--执行命令 -->
        <sshexec host="{ssh.host}"
                 username="${ssh.uname}"
                 password="${ssh.pwd}" trust="true"
                 command="${ssh.server.stop} ;
                       ${ssh.cmd.openwar} ${war.name}.war ;
                       ${ssh.server.start}"/>
    </target>


   

    <!-- =================================================================== -->
    <!-- 清除临时文件-->
    <!-- =================================================================== -->
    <target name="clean" depends="init">
        <delete>
            <fileset dir="${build.dest}" includes="**/*.class"/>
        </delete>
    </target>


</project>
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics