利用ant自动编译Java部署到tomcat

利用ant自动编译Java文件,实现自动部署的原理跟android自动编译打包的方式类似。
不同点是,编译好后,还需要将编译后的class文件打成war包,将war包直接部署在tocmat的webapps目录下,重启tomcat即可完成部署。
build.xml完整代码如下:

    <target name="init" description="设置初始化打 war 包需要的路径变量">
        <!-- 当前的时间 -->
        <tstamp>
            <format property="CURRENT-TIME" pattern="kkmm"></format>
        </tstamp>
        <tstamp>
            <format property="CURRENT-DATE" pattern="yyyyMMdd"></format>
        </tstamp>
        <property name="java.encoding" value="UTF-8"></property>
        <property name="java.target" value="1.6"></property>
        <property name="java.source" value="1.6"></property>
        <property name="src" value="${basedir}/src"></property>
        <property name="lib" value="${basedir}/WebRoot/WEB-INF/lib"></property>
        <property name="webapp" value="${basedir}/WebRoot"></property>

        <property name="build.src" value="${basedir}/AntBuild/src"></property>
        <property name="build.dest" value="${basedir}/AntBuild/WebRoot/WEB-INF/classes"></property>
        <property name="buildwar.dest" value="${basedir}/AntBuild/WebRoot"></property>
        <property name="jar.dest" value="${basedir}/AntBuild/jar"></property>
        <property name="war.dest" value="${basedir}/AntBuild/war"></property>
        <property name="warfilename" value="${ant.project.name}-${CURRENT-DATE}${CURRENT-TIME}.war"></property> 
        <property name="jarfilename" value="${ant.project.name}-${CURRENT-DATE}${CURRENT-TIME}.jar"></property>  
        <property name="appconfigfile" value="${basedir}/AntBuild/WebRoot/WEB-INF/applicationContext.xml"></property>
        <path id="classpath">
            <fileset dir="${lib}">
                <include name="**/*.jar"></include>
            </fileset>
        </path>
    </target>

    <target name="prepare" depends="init" description="创建打包需要的路径,拷贝源文件到打包路径下">
        <delete dir="${basedir}/AntBuild"></delete>
        <mkdir dir="${build.src}"></mkdir>
        <mkdir dir="${build.dest}"></mkdir>
        <mkdir dir="${buildwar.dest}"></mkdir>
        <mkdir dir="${jar.dest}"></mkdir>
        <mkdir dir="${war.dest}"></mkdir>
        <copy todir="${build.src}">
            <fileset dir="${src}"></fileset>
        </copy>
        <copy todir="${buildwar.dest}">
            <fileset dir="${webapp}"></fileset>
        </copy>
        <echo level="info">修改配置文件[${appconfigfile}]:数据库改为测试环境</echo>
        <replaceregexp file="${appconfigfile}" match="jdbc:mysql://([0-9]{1,3}.){4}3306" replace="jdbc:mysql://192.168.1.243:3306" byline="true" flags="gs" encoding="utf-8"></replaceregexp>
        <replaceregexp file="${appconfigfile}" match='<property name="user" value=".*"/>' replace='<property name="user" value="root"/>' byline="true" flags="gs" encoding="utf-8"></replaceregexp>
        <replaceregexp file="${appconfigfile}" match='<property name="password" value=".*"/>' replace='<property name="password" value="yunlai123"/>' byline="true" flags="gs" encoding="utf-8"></replaceregexp>
    </target>

    <target name="build" depends="prepare" description="编译 java 文件,拷贝 properties 属性配置文件到编译后的路径下">
        <javac encoding="${java.encoding}" source="${java.source}" target="${java.target}" srcdir="${build.src}" destdir="${build.dest}" debug="true" extdirs="" includeantruntime="false">
            <classpath refid="classpath"></classpath>
        </javac>
        <copy todir="${build.dest}">
            <fileset dir="${build.src}">
                <include name="**/*.properties"></include>
            </fileset>
        </copy>
    </target>

    <target name="antwar" depends="build" description="打 war 包,不将 java 文件打入包内">
        <delete dir="${build.src}"></delete>
        <war warfile="${war.dest}/${warfilename}" webxml="${buildwar.dest}/WEB-INF/web.xml">
            <lib dir="${buildwar.dest}/WEB-INF/lib"></lib>
            <classes dir="${build.dest}"></classes>
            <fileset dir="${buildwar.dest}"></fileset>
        </war>
        <antcall target="clean"></antcall>
    </target>
    <!--target name="jar" depends="build" description="make jar file">  
        <jar jarfile="${jarfilename}" basedir="${build.dest}">   
            <manifest>  
                <attribute name="Main-Class" value="demo.SayHello"></attribute>  
                <attribute name="Class-Path" value="${buildwar.dest}/WEB-INF/lib"></attribute>  
            </manifest>  
        </jar>  

    <target name="clean" depends="init" description="清除打包用临时文件">
        <delete dir="${build.src}"></delete>
        <delete>
            <fileset dir="${build.dest}" includes="**/*.classes"></fileset>
        </delete>
        <delete dir="${buildwar.dest}"></delete>
    </target>
    <!-- =================================
      target: 做Findbugs检查,和Jenkins结合起来          
     ================================= -->
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"></taskdef>
    <property name="findbugs.home" value="/opt/findbugs-2.0.3"></property>
    <target name="findbugs">
        <findbugs home="${findbugs.home}" output="xml" outputFile="findbugs_report.xml" >
          <auxclasspath>
            <fileset dir="${lib}" includes="*.jar"></fileset>
          </auxclasspath>
          <sourcepath path="${basedir}/src"></sourcepath>
            <class location="${basedir}/WebRoot/WEB-INF/classes"></class>
        </findbugs>
    </target>

    <!-- =================================
          target: 做Pmd检查,和Jenkins结合起来          
         ================================= -->
    <property name="pmd.home" value="/opt/pmd-src-5.1.1"></property>
    <path id="pmd.classpath">
        <fileset dir="${pmd.home}/lib">
            <include name="*.jar"></include>
        </fileset>
    </path>
    <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"  classpathref="pmd.classpath"></taskdef>
    <target name="pmd">
        <pmd rulesetfiles="rulesets/java/design.xml" encoding="utf-8">
            <sourcelanguage name="java" version="1.6"></sourcelanguage>
            <formatter type="xml" toFile="pmd_report.xml"></formatter>
            <fileset dir="${basedir}/">
                <include name="src/**/*.java"></include>
            </fileset>
        </pmd>
    </target>

    <!-- =================================
          target: 做Checkstyle检查,和Jenkins结合起来          
         ================================= -->
    <property name="checkstyle.home" value="/opt/checkstyle-5.7"></property>
    <path id="checkstyle.classpath">
        <fileset dir="${checkstyle.home}">
            <include name="*.jar"></include>
        </fileset>
    </path>
    <taskdef resource="checkstyletask.properties" classpathref="checkstyle.classpath"></taskdef>
    <target name="checkstyle" description="Generates a report of code convention violations.">
        <checkstyle config="${checkstyle.home}/sun_checks.xml" failureProperty="checkstyle.failure" failOnViolation="false">
            <formatter type="xml" tofile="checkstyle_report.xml"></formatter>

            <fileset dir="${basedir}/">
                <include name="src/**/*.java"></include>
            </fileset>
        </checkstyle>
    </target> 
</project>