arm-linux

http://armboard.taobao.com/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

怎样用Ant测试Spring项目

 

   对于Spring项目,还有另外的讲究。

一、可以运行,但是要设置Eclipse,以使Eclipse自动往我的    <property name="run.classpath" value="${home.dir}/bin"></property>

中复制Spring配置文件。

   <?xml version="1.0"?>

<!--

   设置了路径以后就正常了

   Eclipse中,java构建路径.java:src,test,WebRoot;

     缺省输出文件夹  /bin  这个目录,否则会报Springbean未定义错误!

        原因就是因为Spring配置文件无法在classpath中找到!

-->

<project name="project" default="junit">

<!--      chu shi hua , ti gong huan jing bian liang  deng  zhun bei   -->

   <target name="init">

  

         <property name="home.dir" value="."/>

         

         <property name="run.classpath" value="${home.dir}/bin"></property>

    

       <!--

          <property name="run.classpath" value="${home.dir}/build/classes"></property>

          -->

         <property name="run.srcpath" value="${home.dir}/src"></property>

 

         <property name="test.srcpath" value="${home.dir}/test"></property>

        

         <property name="test.report" value="${home.dir}/report"></property>

          <!--

          <property name="test.config" value="${home.dir}/WebRoot"></property>

           -->

         <property name="lib.dir" value="${home.dir}/lib"/>   

 

       

 

         <path id="compile.path">

 

                  <fileset dir="${lib.dir}">

                     <include name="**/*.jar"/>

                      </fileset>

 

         </path>

     </target>  

<!-- Compilation Classpath

    <path id="compile.classpath">

       <fileset dir="${telecom_LDBS.lib}">

           <include name="**/*.jar"/>

       </fileset>

    </path>

  -->     

 

  <!-- Compile ,wei JUnit ti gong .class wen jian.  zai jia shang yu xian ti gong de

     jar zhong de .class  wen jian , ji ke yun xing JUnit. 

       dang ran, hai xu yao junit.jar he ant-junit.jar(huo  optional.jar) he ant.jar zhe 3 ge jar bao de zhi chi

    -->

         <target name="compile"  depends="init">

 

                   <javac destdir="${run.classpath}" srcdir="${run.srcpath}"

 

                            classpathref="compile.path"/>

 

                   <javac destdir="${run.classpath}" srcdir="${test.srcpath}"

 

                            classpathref="compile.path"/>

 

         </target>

 

       

<!-- junit renwu, zhi xing junit ce shi  .  bie wang le,classpath ,the classpath to use.

  xu yao zhi ding classpath yuansu ,ta bao kuo compile lu jing,he  bian yi de jie gou classpath

  (bao kuo sheng cheng de suo you .class  wen jian)

 -->

         <target name="junit" depends="compile">

 

                   <tstamp/>

                <mkdir dir="${test.report}"/>

                <mkdir dir="${test.report}/framework-${DSTAMP}-${TSTAMP}"/>

 

                   <junit printsummary="true">

 

                            <classpath >

                              <pathelement path="${run.classpath}"/>

                              <!-- 

                              <pathelement path="${test.config}"/>

                              -->

                             <fileset dir="${lib.dir}">

                             <include name="**/*.jar"/>

                            </fileset>

 

                            

                            </classpath>

                           

                           

                        <!--

                            <test name="Ldbs1AdslSectionBaseinfoModelServiceTest"></test>

                         -->

    <formatter type="plain"/>

         <!-- she zhi yao ce shi de wen jian ji he .-->

         <batchtest fork="yes" todir="${test.report}/framework-${DSTAMP}-${TSTAMP}">

         <fileset dir="${test.srcpath}">

             <include name="**/*Test.java"/>

            

         </fileset>

         </batchtest>

 

                   </junit>

 

         </target>

<!-- =============================== all ==================================== -->

  <target name="all" depends="junit" />

</project>

 

 

二、不需要借助于Eclipse自动构建功能的Ant文件:

<?xml version="1.0"?>

 

<project name="project" default="junit">

<!--      chu shi hua , ti gong huan jing bian liang  deng  zhun bei   -->

   <target name="init">

  

         <property name="home.dir" value="."/>

         

         <property name="test.war" value="${home.dir}/build/war"></property>

        

       <property name="run.classpath" value="${home.dir}/build/war/WEB-INF/classes"></property>

       <!--

          <property name="run.classpath" value="${home.dir}/build/classes"></property>

          -->

         <property name="run.srcpath" value="${home.dir}/src"></property>

 

         <property name="test.srcpath" value="${home.dir}/test"></property>

        

         <property name="test.report" value="${home.dir}/report"></property>

        

          <property name="test.config" value="${home.dir}/WebRoot"></property>

        

         <property name="lib.dir" value="${home.dir}/lib"/>   

 

        <property name="telecom_LDBS.config" value="${test.war}/WEB-INF/classes/config"/>

 

         <path id="compile.path">

 

                  <fileset dir="${lib.dir}">

                      <include name="**/*.jar"/>

                       </fileset>

 

         </path>

     </target>  

<!-- Compilation Classpath

    <path id="compile.classpath">

        <fileset dir="${telecom_LDBS.lib}">

            <include name="**/*.jar"/>

        </fileset>

    </path>

  -->     

 

  <!-- Compile ,wei JUnit ti gong .class wen jian.  zai jia shang yu xian ti gong de

     jar zhong de .class  wen jian , ji ke yun xing JUnit. 

       dang ran, hai xu yao junit.jar he ant-junit.jar(huo  optional.jar) he ant.jar zhe 3 ge jar bao de zhi chi

    -->

         <target name="compile"  depends="init">

 

                   <javac destdir="${run.classpath}" srcdir="${run.srcpath}"

 

                            classpathref="compile.path"/>

 

                   <javac destdir="${run.classpath}" srcdir="${test.srcpath}"

 

                            classpathref="compile.path"/>

 

         </target>

 

   <!-- =============================== war ==================================== -->

     <target name="war" depends="compile">

    <mkdir dir="${test.war}/WEB-INF"/>

    <mkdir dir="${test.war}/WEB-INF/classes/config/db"/>

    <mkdir dir="${test.war}/WEB-INF/classes/config/acl"/>

    <copy todir="${test.war}">

        <fileset dir="${test.config}"  excludes="**/vssver.scc">

            <include name="**/*.*"/>

             <exclude name="**/vssver.scc"/>

             <exclude name="**/build.xml"/>

            <exclude name="**/build.properties"/>

            <exclude name="**/targets.xml"/>

        </fileset>

    </copy>

   

   

    <copy todir="${test.war}/WEB-INF/classes/config/db">

      <fileset dir="${telecom_LDBS.config}/db">          

      </fileset>

    </copy>

    <copy todir="${test.war}/WEB-INF/classes/config/acl">

       <fileset dir="${telecom_LDBS.config}/acl">

            <include name="*.xml" />

             <exclude name="**/vssver.scc" />

       </fileset>

    </copy>

 

  </target>

       

<!-- junit renwu, zhi xing junit ce shi  .  bie wang le,classpath ,the classpath to use.

  xu yao zhi ding classpath yuansu ,ta bao kuo compile lu jing,he  bian yi de jie gou classpath

  (bao kuo sheng cheng de suo you .class  wen jian)

 -->

         <target name="junit" depends="war">

 

                   <tstamp/>

                <mkdir dir="${test.report}"/>

                <mkdir dir="${test.report}/framework-${DSTAMP}-${TSTAMP}"/>

 

                   <junit printsummary="true">

 

                            <classpath >

                              <pathelement path="${test.war}"/>

                              <pathelement path="${run.classpath}"/>

                              <!-- 

                              <pathelement path="${test.config}"/>

                              -->

                             <fileset dir="${lib.dir}">

                              <include name="**/*.jar"/>

                             </fileset>

 

                            

                            </classpath>

                           

                           

                        <!--

                            <test name="Ldbs1AdslSectionBaseinfoModelServiceTest"></test>

                         -->

    <formatter type="plain"/>

         <!-- she zhi yao ce shi de wen jian ji he .-->

         <batchtest fork="yes" todir="${test.report}/framework-${DSTAMP}-${TSTAMP}">

         <fileset dir="${test.srcpath}">

             <include name="**/*Test.java"/>

            

         </fileset>

         </batchtest>

 

                   </junit>

 

         </target>

<!-- =============================== all ==================================== -->

  <target name="all" depends="junit" />

</project>

 

 

这样,我手工把Spring等文件复制到war目录中。然后,再将这个目录也加到Junit任务的classpath路径中。 

也就是说,现在,classpath中由3部分组成:

1,jar文件集

2,.class文件集

3,SpringHibernate等配置文件集。

实际上,我们也可以进一步改进。将JUnit任务和发布war包的任务合在一起。

 

 

三、下面就是将JUnit融合进原有war包发布的ant文件后的东东:

 

<?xml version="1.0"?>

 

<!-- =================================================================== -->

<!-- $Id: build.xml,v 1.3 2004/02/08 09:04:42 cvs Exp $              -->

<!-- =================================================================== -->

 

<project name="telecom_LDBS" default="all" basedir=".">

 

<!-- =============================== init ==================================== -->

  <target name="init">

    <!-- change this property to use a compiler other than javac. -->

    <property name="build.compiler" value="modern"/>

 

    <!-- The root directory of the workspace -->

    <property name="telecom_LDBS.home" value="."/>

    <property name="telecom_LDBS.lib" value="${telecom_LDBS.home}/lib"/>

   

    <property name="telecom_LDBS.src" value="${telecom_LDBS.home}/src"/>

    <!-- The destination directory for the build -->

    <property name="telecom_LDBS.build" value="${telecom_LDBS.home}/build"/>

    <property name="telecom_LDBS.buildconfigdir" value="${telecom_LDBS.home}/build/config"/>

    <property name="telecom_LDBS.buildwardir" value="${telecom_LDBS.home}/build/war"/>

    <property name="telecom_LDBS.buildeardir" value="${telecom_LDBS.home}/build/ear"/>

 

    <!-- The destination directory for all the compiled classes. -->

    <property name="telecom_LDBS.classbindir" value="${telecom_LDBS.build}/classes"/>

    <property name="telecom_LDBS.srcdocroot" value="${telecom_LDBS.home}/WebRoot"/>

    <property name="telecom_LDBS.config" value="${telecom_LDBS.srcdocroot}/WEB-INF/classes/config"/>

 

<!-- junit tina jia-->

     <property name="test.report" value="${telecom_LDBS.home}/report"></property>

     <property name="test.srcpath" value="${telecom_LDBS.home}/test"></property>

 <!--   ^ junit tina jia-->   

    

    <!-- Compilation Classpath -->

    <path id="compile.classpath">

        <fileset dir="${telecom_LDBS.lib}">

            <include name="**/*.jar"/>

        </fileset>

    </path>

 

 

    <!-- The destination appServer for War. -->

    <property name="tomcat.deployhome" value="D:/java/tomcat/webapps"/>

    <property name="tomcat.deploywar" value="${tomcat.deployhome}/telecom_LDBS_ADSL"/>

 

  </target>

 

  <!-- =============================== compile ==================================== -->

  <target name="compile" depends="init">

    <echo message="${telecom_LDBS.classpath}" />

    <mkdir dir="${telecom_LDBS.classbindir}"/>

    <javac srcdir="${telecom_LDBS.src}"

           destdir="${telecom_LDBS.classbindir}"

           includes="com/**"

           debug="yes">

    <classpath refid="compile.classpath"/>

    </javac>

 

    <!-- Convert resource file's character -->

    <copy file="${telecom_LDBS.src}/com/telecom/ldbs/common/util/ApplicationResources_ISO.properties"

            tofile="${telecom_LDBS.src}/com/telecom/ldbs/common/util/ApplicationResources.properties"/>

    <native2ascii encoding="gb2312" src="${telecom_LDBS.src}" dest="${telecom_LDBS.classbindir}"

            includes="**/*Resources.properties"/>

 

    <copy todir="${telecom_LDBS.classbindir}">

      <fileset dir="${telecom_LDBS.src}">

        <include name="**/*_ISO.properties"/>

        <include name="**/*Methods.properties"/>

      </fileset>

    </copy>

 

    <copy todir="${telecom_LDBS.classbindir}">

      <fileset dir="${telecom_LDBS.src}">

      </fileset>

    </copy>

  </target>

 

 

 

   <!-- =============================== war ==================================== -->

  <target name="war" depends="init">

    <mkdir dir="${telecom_LDBS.buildwardir}/WEB-INF"/>

    <mkdir dir="${telecom_LDBS.buildwardir}/WEB-INF/classes/config/db"/>

    <mkdir dir="${telecom_LDBS.buildwardir}/WEB-INF/classes/config/acl"/>

    <copy todir="${telecom_LDBS.buildwardir}">

        <fileset dir="${telecom_LDBS.srcdocroot}"  excludes="**/vssver.scc">

            <include name="**/*.*"/>

             <exclude name="**/vssver.scc"/>

             <exclude name="**/build.xml"/>

            <exclude name="**/build.properties"/>

            <exclude name="**/targets.xml"/>

        </fileset>

    </copy>

    <copy todir="${telecom_LDBS.buildwardir}/WEB-INF/lib">

        <fileset dir="${telecom_LDBS.lib}">

            <include name="**/*.jar"/>

             <exclude name="**/vssver.scc" />

            <exclude name="**/servlet.jar" />

            <exclude name="**/j2ee.jar" />

        </fileset>

    </copy>

    <copy todir="${telecom_LDBS.buildwardir}/WEB-INF/classes">

      <fileset dir="${telecom_LDBS.classbindir}">

             <include name="**/**.class" />

             <include name="**/**.properties" />

             <exclude name="**/vssver.scc" />

      </fileset>

    </copy>

    <copy todir="${telecom_LDBS.buildwardir}/WEB-INF/classes">

      <fileset dir="${telecom_LDBS.srcdocroot}/WEB-INF/menu">

             <include name="**/**.vm" />

      </fileset>

    </copy>

    <copy todir="${telecom_LDBS.buildwardir}/WEB-INF/classes/config/db">

      <fileset dir="${telecom_LDBS.config}/db">          

      </fileset>

    </copy>

    <copy todir="${telecom_LDBS.buildwardir}/WEB-INF/classes/config/acl">

       <fileset dir="${telecom_LDBS.config}/acl">

            <include name="*.xml" />

             <exclude name="**/vssver.scc" />

       </fileset>

    </copy>

 

  </target>

 

 

  <!-- =============================== deploy ==================================== -->

  <target name="deploy" depends=" compile, war">

    <mkdir dir="${tomcat.deploywar}" />

 

    <copy todir="${tomcat.deploywar}">

             <fileset dir="${telecom_LDBS.buildwardir}">

                 <include name="**/**.*"/>

         </fileset>

    </copy>

  </target>

 

    <target name="deploy_jsp" depends="init">

        <copy todir="${tomcat.deploywar}">

             <fileset dir="${telecom_LDBS.srcdocroot}">

                 <include name="**/*.jsp"/>

                 <include name="**/*.inc"/>

                 <include name="**/*.xml"/>

                 <include name="**/*.js"/>

                 <include name="**/*.htm*"/>

             </fileset>

        </copy>

    </target>

 

  <!-- =============================== deploy_all ==================================== -->

    <target name="deploy_all" depends="init">

        <copy todir="${tomcat.deploywar}">

             <fileset dir="${telecom_LDBS.srcdocroot}">

                 <include name="**/*.jsp"/>

                 <include name="**/*.inc"/>

                 <include name="**/*.xml"/>

                 <include name="**/*.js"/>

                 <include name="**/*.htm*"/>

                 <include name="**/*.class"/>

                 <include name="**/*.properties"/>

             </fileset>

        </copy>

    </target>

 

  <!-- =============================== clean ==================================== -->

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

    <mkdir dir="${telecom_LDBS.build}" />

    <mkdir dir="${tomcat.deploywar}" />

   

    <delete includeEmptyDirs="true">

         <fileset dir="${telecom_LDBS.build}" includes="**/**"/>

    </delete>

    <delete includeEmptyDirs="true" failonerror="false">

         <fileset dir="${tomcat.deployhome}/telecom_LDBS" includes="**/**"/>

    </delete>

  </target>

 

 

  <!-- junit tina jia-->

  <!-- junit renwu, zhi xing junit ce shi  .  bie wang le,classpath ,the classpath to use.

  xu yao zhi ding classpath yuansu ,ta bao kuo compile lu jing,he  bian yi de jie gou classpath

  (bao kuo sheng cheng de suo you .class  wen jian)

 -->

         <target name="junit" depends="war">

 

                   <tstamp/>

                <mkdir dir="${test.report}"/>

                <mkdir dir="${test.report}/framework-${DSTAMP}-${TSTAMP}"/>

 

                   <junit printsummary="true">

 

                            <classpath >

                              <pathelement path="${telecom_LDBS.buildwardir}"/>

                              <pathelement path="${telecom_LDBS.classbindir}"/>

                              <!-- 

                              <pathelement path="${test.config}"/>

                              -->

                             <fileset dir="${telecom_LDBS.lib}">

                              <include name="**/*.jar"/>

                             </fileset>

 

                            

                            </classpath>

                           

                           

                        <!--

                            <test name="Ldbs1AdslSectionBaseinfoModelServiceTest"></test>

                         -->

    <formatter type="plain"/>

         <!-- she zhi yao ce shi de wen jian ji he .-->

         <batchtest fork="yes" todir="${test.report}/framework-${DSTAMP}-${TSTAMP}">

         <fileset dir="${test.srcpath}">

             <include name="**/*Test.java"/>

            

         </fileset>

         </batchtest>

 

                   </junit>

 

         </target>

  <!-- ^ junit tina jia-->

  <!-- =============================== all ==================================== -->

  <target name="all" depends=" clean, compile, war ,junit"/> 

</project>

 

 

只是添加了几个目录,然后又加上了junit任务即可。

 

 

posted on 2005-11-18 21:40  arm-linux  阅读(661)  评论(0编辑  收藏  举报