您的位置:首页 > 运维架构 > Apache

apacheAnt的构建脚本编写以及集成junit进行自动测试

2017-02-21 14:22 579 查看
一、apche Ant 的构建脚本编写

1. 框架

<?xml version = '1.0' encoding = 'GB2312' ?>
<project name="ZXCloud-isware-cos" default="main" basedir=".">
<property name="src-thirdparty-page" value="${webroot}/page/console/thirdparty"/>
<target name="main" >
<antcall target="welcome" />
<antcall target="clean" />
<antcall target="compile"/>
<antcall target="junit"/>
<antcall target="genzip"/>
<antcall target="done" />
</target>

<target name="welcome">
</target>

<target name="clean">
</target>

<target name="compile">
<javac  fork="true" destdir="${web-classes}" debug="on" encoding="utf-8" memoryinitialsize="256m" memorymaximumsize="1024m">
<classpath>
<fileset dir="${thirdparty}">
<include name="**/*.jar"/>
</fileset>
</classpath>
<src path="${src}"/>
<include name="**"/>
</javac>
</target>

<!--junit-->
<target name="junit">
<path id="junit.lib">
<fileset dir="${thirdparty}">
<include name="*.jar" />
</fileset>
<pathelement location="${web-classes}"/>
</path>

<junit printsummary="false">
<classpath refid="junit.lib"></classpath>
<formatter type="xml"/>
<batchtest todir="${isware.reports.xml}">
<fileset dir="${web-classes}">
<include name="**/Test*.class"/>
<exclude name="**/*$*.class"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${isware.reports.xml}">
<fileset dir="${isware.reports.xml}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${isware.reports.html}"/>
</junitreport>
</target>

<target name="genzip">
</target>

<target name="genwar">
......
<war destfile="${output}/${output-file-war-name}" webxml="${webroot}/WEB-INF/web.xml">
<zipfileset dir="${output}" excludes="${output-file-jar-name}"/>
</war>
</target>

<target name="done">
</target>

</project>
2.标签解释

(1)project  每个构建文件有一个project标签。它有三个属性,分别是name:指定项目的名称,default:指定默认调用的target,basedir=指定相对路径计算的基准路径。如果不指定,则默认为build文件所在的目录。

(2)target 任务容器,name属性指定名称, depends属性指定其依赖的其他target,ant会保证他所以来的target执行完以后再执行该target。

(3)task 任务是可执行的代码片段,ant提供了很多内置的task

(4)properties 通常以key/value的形式定义在所有的targets外,会先于所有的targets被解析。可以通过${var}的形式在targets或者其内置的task中使用。

(5)build-in properties 内置的属性,ant预定义的属性,不需要定义,用法和properties一样

(6)classpath 和 path用来指定任务所需要的jar包的位置、需要加载的classpath的位置等。二者区别在于,classpath用于某个任务中;path定义在target下,与任务平级,可以被该target下的所有任务使用。二者的内置元素可以有 fileset、dirset、filelist、pathelment等。内置元素pathelement 又可以有两个属性,分别是location(指定单个文件或文件夹的位置)和path(指定文件或文件夹列表的路径,用分号或冒号分开)。path和location属性也可以直接作为classpath和path的属性作为简写形式。

3.内置task

参考地址:点击打开链接

二、集成junit遇到的问题

1.每次运行到junit target时,都会失败,查看junit reports,可以发现抛出异常为 ClassNotFoundException。意思是找不到测试类的位置,无法加载该类的class文件。找到指定目录,存在该class文件。问题在于,需要通过classpath来指定batchtest任务查找测试类文件的路径。

src

-com.my.service

-test.com.my.testService

首先,要清楚类的全名,类的全名=类的包路径.类名。如test.com.my.testService

其次,classpath里设置的路径必须是包路径中最顶层包所在的目录,此处为test所在的目录,即源代码编译完的输出文件夹,一般为build。

2.javac动作编译所使用的编译器的问题

机器上安装了jdk1.6和jdk1.7,设置JAVA_HOME为1.6,通过java -version命令查看为1.6。在执行ant -f build.xml之前,用set JAVA_HOME=%JAVA_HOME%,也就是设置了ant命令执行时基于1.6,但是编译出来的class文件为1.7(通过字节码编辑器可以查看版本号为51,对应于jdk1.7)。百思不得其解!查看javac动作中的参数,fork表示使用外部编译器,excecuble指出使用的外部编译器(fork=true时有效,不指定默认使用ant运行的环境),includeAntRuntime指定是否使用ant命令运行的环境,这个参数是重点,默认为true表示使用Ant的运行时环境,设置为false表示不使用。因为我指定为false,所以导致javac
命令使用了jdk1.7。修改为true时,恢复成1.6,问题解决

<javac fork="true" includeAntRuntime="true" destdir="${web-classes}" debug="on" encoding="utf-8" memoryinitialsize="256m" memorymaximumsize="1024m">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息