如何使用ant构建junit测试

 

build.bat:

echo on

set PROJET_HOME=..
set JAVA_HOME=%PROJET_HOME%/../jdk/windows
set ANT_HOME=%PROJET_HOME%/../ant

call %ANT_HOME%/bin/ant -f build.xml build_all -Dkkk=123456

pause
 

 

 

 

 

build.xml:

<project name="MyProject" default="dist" basedir="..">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="testsrc" location="test"/>
  <property name="tmpclass" location="tmp/class"/>
  <property name="tmptest" location="tmp/testclass"/>
  <property name="testrep" location="testrep"/>
  <property name="dist"  location="dist"/>

  <path id="lib">
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
    </path>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <echo message= "${kkk}"/>
    <delete dir="${tmpclass}"/>
    <mkdir dir="${tmpclass}"/>
    <delete dir="${tmptest}"/>
    <mkdir dir="${tmptest}"/>
    <delete dir="${testrep}"/>
    <mkdir dir="${testrep}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${tmpclass}" includeAntRuntime="false">
    	<classpath>
    		      <path refid="lib"/>
    		</classpath>
    </javac>
  </target>

  <target name="testcompile" depends="compile"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${testsrc}" destdir="${tmptest}" includeAntRuntime="false">
    	<classpath>
    		      <path refid="lib"/>
    		      <pathelement path="${tmpclass}"/>
    		</classpath>
    	</javac>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${tmpclass}"/>
  </target>

  <target name="test" depends="testcompile">
  	
				  	<junit printsummary="true">
				  <classpath>
    		      <path refid="lib"/>
				    <pathelement path="${tmptest}"/>
				    <pathelement path="${tmpclass}"/>
				  </classpath>
				
				  <formatter type="plain"/>
							
				  <batchtest fork="yes" todir="${testrep}">
				    <fileset dir="${testsrc}">
				      <include name="**/*Test.java"/>
				    </fileset>
				  </batchtest>
				</junit>

  	</target>

  <target name="build_all" depends="compile,testcompile,test,dist"/>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
 

 

目录结构:

build 存放构建脚本

lib 存放依赖包

test 存放单元测试源码

src 

 

刚开始只在lib下存放junit.jar会抛找不到类的异常,可能跟版本也有关系:

org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

再下载这个包:

hamcrest-core-1.2.jar

 

参考链接:

http://stackoverflow.com/questions/1171264/ant-junit-noclassdeffounderror

http://ant.apache.org/manual/index.html

 

你可能感兴趣的:(ant,JUnit)