ant学习笔记(五)基于单元测试的j2se项目,包括编译、测试、打包、生成报告、上传ftp

这里只是描写ant的build.xml文件,其他的数据库连接文件,请自行编写


<?xml version="1.0" encoding="UTF-8"?>
<project default="ftp">
	<property name="src.dir" location="src"></property>
	<property name="test.src.dir" location="test"></property>
	<property name="lib.dir" location="lib"></property>
	<property file="build.properties"></property>
	
	<property name="build.dir" location="build"></property>
	<property name="build.classes" location="${build.dir}/classes"></property>
	<property name="build.test.dir" location="${build.dir}/test"></property>
	<property name="build.test.classes" location="${build.test.dir}/classes"></property>
	<property name="build.test.report" location="${build.test.dir}/report"></property>
	<property name="build.jar.dir" location="${build.dir}/dist"></property>
	<property name="build.zip.dir" location="${build.dir}/zip"></property>
	<property name="build.doc.dir" location="${build.dir}/doc"></property>
	<property name="build.src" location="${build.dir}/src"></property>
	<property name="metadata.dir" location="metadata"/>
	
	<path id="compile-classpath">
		<fileset dir="${lib.dir}" includes="*.jar"></fileset>
	</path>
	
	<path id="compile-test-classpath">
		<path refid="compile-classpath"></path>
		<pathelement location="${build.classes}"/>
	</path>
	
	<path id="run-test-classpath">
		<path refid="compile-test-classpath"/>
		<pathelement location="${build.test.classes}"/>
	</path>
	
	<target name="clean">
		<delete dir="${build.dir}"/>
	</target>
	
	<target name="init" depends="clean,metadata">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${build.classes}"/>
		<mkdir dir="${build.test.dir}"/>
		<mkdir dir="${build.test.classes}"/>
		<mkdir dir="${build.test.report}"/>
		<mkdir dir="${build.jar.dir}"/>
		<mkdir dir="${build.zip.dir}"/>
		<mkdir dir="${build.doc.dir}"/>
		<mkdir dir="${build.src}"/>
		<mkdir dir="${metadata.dir}"/>
	</target>
	
	<target name="compile" depends="init">
		<javac destdir="${build.classes}" srcdir="${src.dir}" 
			classpathref="compile-classpath" 
			   includeantruntime="true" failonerror="true"></javac>
		<copy todir="${build.classes}">
			<fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java"></fileset>
		</copy>
	</target>
	
	<target name="compile-test" depends="compile">
		<javac destdir="${build.test.classes}" srcdir="${test.src.dir}"
			   includeantruntime="true" failonerror="true" 
			   classpathref="compile-test-classpath"></javac>
	</target>
	
	<target name="run-test" depends="compile-test">
		<junit fork="true" haltonfailure="false" failureproperty="junit.fail">
			<classpath refid="run-test-classpath"></classpath>
			<formatter type="brief" usefile="false"/>
			<formatter type="xml"/>
			<batchtest todir="${build.test.report}">
				<fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
			</batchtest>
		</junit>
		<junitreport todir="${build.test.report}">
			<fileset dir="${build.test.report}" includes="TEST*.xml"></fileset>
			<report format="frames" todir="${build.test.report}/html"/>
		</junitreport>
		<fail if="${junit.fail}" message="单元测试失败,具体情况请查询${build.test.report}"/>
	</target>
	
	<target name="doc" depends="run-test">
		<javadoc sourcepath="${src.dir}" use="true" packagenames="cn.*" 
			   charset="UTF-8" encoding="UTF-8" docencoding="UTF-8"
			   destdir="${build.doc.dir}">
			<classpath refid="compile-classpath"></classpath>
		</javadoc>
	</target>
	
	<target name="jar" depends="doc">
		<tstamp>
			<format property="now" pattern="yyyy-MM-dd HH:mm:ss"/>
		</tstamp>
		<jar destfile="${build.jar.dir}/${project.jar.name}" 
				basedir="${build.classes}" duplicate="preserve">
			<manifest>
				<attribute name="Build-By" value="${user.name}"/>
				<attribute name="Build-time" value="${now}"/>
			</manifest>
		</jar>		
	</target>
	
	<target name="copy-src" depends="jar">
		<copy todir="${build.src}">
			<fileset dir="${src.dir}" includes="**/*.*"></fileset>
		</copy>
	</target>
	
	
	<target name="metadata">
		<mkdir dir="${metadata.dir}"/>
		<buildnumber/>
		<propertyfile file="${metadata.dir}/build.propterties">
			<entry key="Build-time" type="date" value="now" pattern="yyyy-MM-dd HH:mm:ss"/>
			<entry key="build-number" type="int" value="${build.number}"/>
		</propertyfile>
	</target>
	
	<target name="zip" depends="copy-src">
		<zip destfile="${build.zip.dir}/${project.zip.name}" duplicate="preserve">
			<zipfileset file="${build.jar.dir}/${project.jar.name}" prefix="${project.prefix}"/>
			<zipfileset dir="${build.src}" includes="**/*.*" prefix="${project.prefix}/src"></zipfileset>
			<zipfileset dir="${build.doc.dir}" includes="**/*.*" prefix="${project.prefix}/doc/api"></zipfileset>
			<zipfileset dir="${build.test.dir}" includes="**/*.*" 
					excludes="report/*.*" prefix="${project.prefix}/test"></zipfileset>
			<zipfileset dir="." includes="build.xml,build.properties" prefix="${project.prefix}"></zipfileset>
		</zip>
	</target>
	
	<!--相当于函数的使用-->
	<presetdef name="upload">
		<ftp userid="${ftp.username}" password="${ftp.password}" 
			 server="${ftp.server}" remotedir="${ftp.dir}"></ftp>
	</presetdef>
	
	<target name="ftp" depends="zip">
		<!-- 这里进行upload的函数使用-->
		<upload action="mkdir"/>
		<upload action="put">
			<fileset dir="${build.zip.dir}" includes="${project.zip.name}"></fileset>
			</upload>
	</target>
	
</project>

build.properties

ftp.username = ant
ftp.password = ant
ftp.server = localhost
ftp.dir = user_manager
project.version = SNAPSHOT-0.1
project.prefix = usermanager-${project.version}
project.jar.name = ${project.prefix}.jar
project.zip.name = ${project.prefix}.zip
run.test.class = **/Test*.class


你可能感兴趣的:(单元测试)