上一篇转载的ant学习指南主要讲了ant的命令,讲到的地方比较细。
但是实际运用中我们除了做打包(类似c中的makefile)外,也在junit测试中使用到ant,并且用marcodef宏定义的方法会让脚本更加简洁,效率更高。
下面就讲解一下marcodef和junit命令:
1.Marcodef
MacroDef——“宏”,宏是一种规则或模式,或称语法替换 ,当我们target任务不多的时候,我们可以一个个写作。但是实际工作中,常常会有大量的任务需要执行。例如100个目录需要编译,那么我的脚本中就要重复100次相同的代码;所以自然的就想到要和写其他脚本或者程序中那样构造复用,使用MacroDef的情况下,只用写一个通用的<macrodef></macrodef>,再在其他地方调用它就可以了,既减少了代码量,又提高了工作效率。
在官网使用手册(http://ant.apache.org/manual/)我们可以找到MacroDef的具体书写格式和参数。
<!-- =================================================================== -->
<!-- 宏定义 testModule -->
<!-- =================================================================== -->
<macrodef name="testModule"> //macrodef的定义,定义了name属性
<attribute name="module"/>//参数定义
<attribute name="prefix"/>//参数定义
<sequential>//实际宏执行的部分
<junit printsummary="withOutAndErr" fork="true" showOutput="true" haltonfailure="false" maxmemory ="512m">
<classpath refid="project.classpath"/>
<formatter type="xml"/>
<formatter type="plain"/>
<sysproperty key="testroot" value="${testcasesdir}@{module}"/>
<sysproperty key="work.directory" value="${workdir}" />
<sysproperty key="bi.home" value="${workdir}@{module}"/>
<batchtest fork="yes" todir="${reportdir}" >
<fileset dir="${currentdir}">
<include name="**/@{prefix}Test.class"/>
</fileset>
</batchtest>
</junit>
</sequential>
</macrodef>
有了宏定义之后,我们下面的任务只需要添加一条语句即可进行junit测试啦!
<!-- ==========================================================&