Apache Ant学习笔记之四(转载)

Apache Ant学习笔记之四

22控制Tomcat

<property name="catalina.home" location="D:"JBuilder7"jakarta-tomcat-4.0.3""/>
     <property name="tomcat.server.config" location="."Tomcat"conf"server8080.xml"/>

<target name="tomcat_start" depends="clean" >

<echo message="==================================="/>
<echo message=" Start Tomcat"/>
<echo message="==================================="/>

<java classname="org.apache.catalina.startup.Bootstrap" fork="yes" failonerror="true" >
  <jvmarg value="-Dcatalina.home=${catalina.home}" />
  <arg value="-config" />
  <arg path="${tomcat.server.config}" />

  <arg value="start"/>
  <classpath>
     <fileset dir="${catalina.home}">
         <include name="bin/bootstrap.jar"/>
     </fileset>
  </classpath>
 </java>
</target>

<target name="tomcat_stop">

<echo message="==================================="/>
<echo message=" Stop Tomcat"/>
<echo message="==================================="/>

<java classname="org.apache.catalina.startup.Bootstrap" fork="yes" failonerror="true" >
     <jvmarg value="-Dcatalina.home=${catalina.home}" />
     <arg value="-config" />
     <arg path="${tomcat.server.config}" />
     <arg value="stop"/>

     <classpath>
         <fileset dir="${catalina.home}">
            <include name="bin/bootstrap.jar"/>
         </fileset>
     </classpath>
  </java>
</target>

23.生成Javadoc另一:

<javadoc packagenames="hello.ant.*"
                 sourcepath="${src.main}"
                 defaultexcludes="yes"
                 destdir="${build.docs.api}"
                 author="true"
                 version="true"
                 use="true"
                 windowtitle="Docs API">
  <doctitle><![CDATA[<h1>hello ant Docs API</h1>]]></doctitle>
  <bottom><![CDATA[<i>${app.copyright}</i>]]></bottom>
  <tag name="todo" scope="all" description="To do:" />
</javadoc>

     foxgem 的一个Ant模板包括:编译、运行Junit测试用例、CheckStlye、从Vss取版本、javadoc等。

 

<?xml version="1.0" encoding="gb2312"?>
<!--
 
本模板文件提供了以下功能:
  1.
编译java代码;
  2.
产生java代码对应的javaDoc
  3.
检查java代码的编码规范;
  4.
编译并运行java代码对应的junit测试代码
  5.
Vss中获取程序
 -->
<project name="Fog Project" default="all" basedir=".">

    <!-- 环境变量 -->
    <property environment="env"/>
    <!--
设置junit报告的xsl文件目录 -->
    <property name="junit.styleDir" value="${env.ANT_HOME}/etc"/>
   
    <!--
源代码目录 -->
    <property name="src.code" value="src"/>
    <!--
源代码对应的junit目录 -->
    <property name="src.junit" value="junit"/>
    <!--
引用包所在目录 -->
    <property name="lib.dir" value="lib"/>
    <!--
目标jar名称 -->
    <property name="lib.jar" value="fog.jar"/>
   
    <!-- checkstyle configuration -->
    <property name="checkstyle.config" value="${lib.dir}/checkstyle33.xml"/>
    <!--
设置checkstylexsl文件 -->
    <property name="checkstyle.xsl" value="${lib.dir}/checkstyle-frames.xsl"/>
    <taskdef resource="checkstyletask.properties"
             classpath="${lib.dir}/checkstyle-all-3.3.jar"/>
   
    <!-- VSS
配置 -->
    <property name="vss.ssdir" value="D:"Program Files"vss"win32""/>
    <property name="vss.svrdir" value="Z:"/>
    <property name="vss.path" value="/fog/implement"/>
   
    <!--
输出文档 -->
    <property name="doc.dir" value="doc"/>
    <property name="doc.api" value="${doc.dir}/api"/>
    <!-- junit report -->
    <property name="doc.junitReport" value="${doc.dir}/junitReport"/>
    <!-- checkstyle report -->
    <property name="doc.checkstyleReport" value="${doc.dir}/checkstyleReport"/>
   
    <!--
与开发包结构相同 -->
    <property name="javadoc.package" value="fog.*"/>
   
    <!--
输出二进制文件 -->
    <property name="dist.root" value="projDist"/>
    <property name="dist.proj" value="${dist.root}/proj"/>
    <property name="dist.classes" value="${dist.proj}/classes"/>
    <property name="dist.lib" value="${dist.proj}/lib"/>
    <property name="dist.junit" value="${dist.root}/junit"/>
   
    <!-- classpath -->
    <path id="classpath">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar"/>
        </fileset>
       
        <fileset dir="web/WEB-INF/lib">
            <include name="**/*.jar"/>
        </fileset>

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

        <fileset dir="junit_lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>
   
    <target name="init">
        <mkdir dir="${doc.dir}"/>
        <mkdir dir="${dist.root}"/>
        <mkdir dir="${dist.proj}"/>
        <mkdir dir="${dist.lib}"/>
        <tstamp/>
        <echo message="${TSTAMP}"></echo>
    </target>
   
    <target name="all" depends="compilesrc, javadoc, checkstyle"/>
   
    <!--
编译源文件 -->
    <target name="compilesrc" depends="init">
        <mkdir dir="${dist.classes}"/>
       
        <javac destdir="${dist.classes}" deprecation="on">
            <src path="${src.code}"/>
            <classpath refid="classpath"/>
        </javac>

        <jar jarfile="${dist.lib}/${lib.jar}" basedir="${dist.classes}">
            <include name="**/*.class"/>
        </jar>     
    </target>
   
    <!-- 
产生javadoc -->
    <target name="javadoc" depends="init">
        <mkdir dir="${doc.api}"/>
       
        <javadoc packagenames="${javadoc.package}" sourcepath="${src.code}"
             private="yes" defaultexcludes="yes" destdir="${doc.dir}/api">
            <classpath refid="classpath"/>
        </javadoc>
    </target>
   
    <!-- 
编译Junit文件 -->
    <target name="compilejunit" depends="compilesrc">
        <mkdir dir="${dist.junit}"/>
       
        <javac destdir="${dist.junit}" deprecation="on">
            <src path="${src.junit}"/>
            <classpath refid="classpath"/>
        </javac>       
    </target>
   
    <!--
运行checkstyle检查代码规范 -->
    <target name="checkstyle" depends="init">
        <mkdir dir="${doc.checkstyleReport}"/>
       
        <checkstyle config="${checkstyle.config}">
            <fileset dir="${src.code}" includes="**/*.java"/>
            <formatter type="plain"/>
            <formatter type="xml" toFile="${doc.checkstyleReport}/checkstyle_report.xml"/>
        </checkstyle>
       
        <style in="${doc.checkstyleReport}/checkstyle_report.xml" out="${doc.checkstyleReport}/checkstyle_report.html" style="${checkstyle.xsl}"/>
    </target>
   
    <!-- 
运行junit  -->
    <target name="junit" depends="compilejunit">
        <mkdir dir="${doc.junitReport}"/>
        <copy todir="${dist.junit}">
            <fileset dir="junit_lib">
                <exclude name="**/*.jar"/>
            </fileset>

            <fileset dir="${src.code}">
                <include name="fog.hbm.xml"/>
            </fileset>
        </copy>
       
        <junit printsummary="yes" haltonfailure="no">
            <classpath>
                <path refid="classpath"/>
                <pathelement location="${dist.junit}"/>
            </classpath>
           
            <formatter type="brief" usefile="false"/>
            <formatter type="xml"/>
       
            <batchtest todir="${doc.junitReport}">
                <fileset dir="${dist.junit}" includes="**/*Test.class" />
            </batchtest>
        </junit>

        <junitreport todir="${doc.junitReport}">
            <fileset dir="${doc.junitReport}">
                <include name="TEST*-*.xml"/>
            </fileset>
            <report format="frames" styledir="${junit.styleDir}" todir="${doc.junitReport}"/>
        </junitreport>
    </target>

    <!-- Vss中获取最新版本 -->
    <target name="getVersion" depends="">
        <vssget
                vsspath="${vss.path}" localpath="." login="codeline,codeline"
                ssdir="${vss.ssdir}" serverPath="${vss.svrdir}" autoresponse="N" recursive="true"
                quiet="true" />
    </target>
   
    <!--
清除产生的类、junit相关类、文档 -->
    <target name="clean">
        <delete dir="${dist.classes}"/>
        <delete dir="${dist.junit}"/>
        <delete dir="${doc.api}"/>
        <delete dir="${doc.junitReport}"/>
        <delete dir="${doc.checkstyleReport}"/>
    </target>
   
    <!--
清除所有输出结果 -->
    <target name="cleanall" depends="clean">
        <delete dir="${doc.dir}"/>
        <delete dir="${dist.root}"/>
    </target>
</project>

       到此关于ant使用的所有方面基本总结完整了。

 

 

 

现在项目使用ant来管理,整理了一个常用的Ant模板文件,包括:编译、运行Junit测试用例、CheckStlye、从Vss取版本、javadoc等。希望有用。

<?xml version="1.0" encoding="gb2312"?>
<!--
 
本模板文件提供了以下功能:
 1.
编译java代码;
 2.
产生java代码对应的javaDoc
 3.
检查java代码的编码规范;
 4.
编译并运行java代码对应的junit测试代码
 5.
Vss中获取程序
-->
<project name="Fog Project" default="all" basedir=".">

   <!--
环境变量 -->
   <property environment="env"/>
   <!--
设置junit报告的xsl文件目录 -->
   <property name="junit.styleDir" value="${env.ANT_HOME}/etc"/>
   
   <!--
源代码目录 -->
   <property name="src.code" value="src"/>
   <!--
源代码对应的junit目录 -->
   <property name="src.junit" value="junit"/>
   <!--
引用包所在目录 -->
   <property name="lib.dir" value="lib"/>
   <!--
目标jar名称 -->
   <property name="lib.jar" value="fog.jar"/>
   
   <!-- checkstyle configuration -->
   <property name="checkstyle.config" value="${lib.dir}/checkstyle33.xml"/>
   <!--
设置checkstylexsl文件 -->
   <property name="checkstyle.xsl" value="${lib.dir}/checkstyle-frames.xsl"/>
   <taskdef resource="checkstyletask.properties"
            classpath="${lib.dir}/checkstyle-all-3.3.jar"/>
   
   <!-- VSS
配置 -->
   <property name="vss.ssdir" value="D:"Program Files"vss"win32""/>
   <property name="vss.svrdir" value="Z:"/>
   <property name="vss.path" value="/fog/implement"/>
   
   <!--
输出文档 -->
   <property name="doc.dir" value="doc"/>
   <property name="doc.api" value="${doc.dir}/api"/>
   <!-- junit report -->
   <property name="doc.junitReport" value="${doc.dir}/junitReport"/>
   <!-- checkstyle report -->
   <property name="doc.checkstyleReport" value="${doc.dir}/checkstyleReport"/>
   
   <!--
与开发包结构相同 -->
   <property name="javadoc.package" value="fog.*"/>
   
   <!--
输出二进制文件 -->
   <property name="dist.root" value="projDist"/>
   <property name="dist.proj" value="${dist.root}/proj"/>
   <property name="dist.classes" value="${dist.proj}/classes"/>
   <property name="dist.lib" value="${dist.proj}/lib"/>
   <property name="dist.junit" value="${dist.root}/junit"/>
   
   <!-- classpath -->
   <path id="classpath">
       <fileset dir="${lib.dir}">
           <include name="**/*.jar"/>
       </fileset>
       
       <fileset dir="web/WEB-INF/lib">
           <include name="**/*.jar"/>
       </fileset>

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

       <fileset dir="junit_lib">
           <include name="**/*.jar"/>
       </fileset>
   </path>
   
   <target name="init">
       <mkdir dir="${doc.dir}"/>
       <mkdir dir="${dist.root}"/>
       <mkdir dir="${dist.proj}"/>
       <mkdir dir="${dist.lib}"/>
       <tstamp/>
       <echo message="${TSTAMP}"></echo>
   </target>
   
   <target name="all" depends="compilesrc, javadoc, checkstyle"/>
   
   <!--
编译源文件 -->
   <target name="compilesrc" depends="init">
       <mkdir dir="${dist.classes}"/>
       
       <javac destdir="${dist.classes}" deprecation="on">
           <src path="${src.code}"/>
           <classpath refid="classpath"/>
       </javac>

       <jar jarfile="${dist.lib}/${lib.jar}" basedir="${dist.classes}">
           <include name="**/*.class"/>
       </jar>      
   </target>
   
   <!--  
产生javadoc -->
   <target name="javadoc" depends="init">
       <mkdir dir="${doc.api}"/>
       
       <javadoc packagenames="${javadoc.package}" sourcepath="${src.code}"
            private="yes" defaultexcludes="yes" destdir="${doc.dir}/api">
           <classpath refid="classpath"/>
       </javadoc>
   </target>
   
   <!--  
编译Junit文件 -->
   <target name="compilejunit" depends="compilesrc">
       <mkdir dir="${dist.junit}"/>
       
       <javac destdir="${dist.junit}" deprecation="on">
           <src path="${src.junit}"/>
           <classpath refid="classpath"/>
       </javac>        
   </target>
   
   <!--
运行checkstyle检查代码规范 -->
   <target name="checkstyle" depends="init">
       <mkdir dir="${doc.checkstyleReport}"/>
       
       <checkstyle config="${checkstyle.config}">
           <fileset dir="${src.code}" includes="**/*.java"/>
           <formatter type="plain"/>
           <formatter type="xml" toFile="${doc.checkstyleReport}/checkstyle_report.xml"/>
       </checkstyle>
       
       <style in="${doc.checkstyleReport}/checkstyle_report.xml" out="${doc.checkstyleReport}/checkstyle_report.html" style="${checkstyle.xsl}"/>
   </target>
   
   <!--  
运行junit  -->
   <target name="junit" depends="compilejunit">
       <mkdir dir="${doc.junitReport}"/>
       <copy todir="${dist.junit}">
           <fileset dir="junit_lib">
               <exclude name="**/*.jar"/>
           </fileset>

           <fileset dir="${src.code}">
               <include name="fog.hbm.xml"/>
           </fileset>
       </copy>
       
       <junit printsummary="yes" haltonfailure="no">
           <classpath>
               <path refid="classpath"/>
               <pathelement location="${dist.junit}"/>
           </classpath>
           
           <formatter type="brief" usefile="false"/>
           <formatter type="xml"/>
       
           <batchtest todir="${doc.junitReport}">
               <fileset dir="${dist.junit}" includes="**/*Test.class" />
           </batchtest>
       </junit>

       <junitreport todir="${doc.junitReport}">
           <fileset dir="${doc.junitReport}">
               <include name="TEST*-*.xml"/>
           </fileset>
           <report format="frames" styledir="${junit.styleDir}" todir="${doc.junitReport}"/>
       </junitreport>
   </target>

   <!--
Vss中获取最新版本 -->
   <target name="getVersion" depends="">
       <vssget
               vsspath="${vss.path}" localpath="." login="codeline,codeline"
               ssdir="${vss.ssdir}" serverPath="${vss.svrdir}" autoresponse="N" recursive="true"
               quiet="true" />
   </target>
   
   <!--
清除产生的类、junit相关类、文档 -->
   <target name="clean">
       <delete dir="${dist.classes}"/>
       <delete dir="${dist.junit}"/>
       <delete dir="${doc.api}"/>
       <delete dir="${doc.junitReport}"/>
       <delete dir="${doc.checkstyleReport}"/>
   </target>
   
   <!--
清除所有输出结果 -->
   <target name="cleanall" depends="clean">
       <delete dir="${doc.dir}"/>
       <delete dir="${dist.root}"/>
   </target>
</project>

posted @ 2008-12-03 17:12  zhanlh  阅读(493)  评论(0编辑  收藏  举报