以前一直都用eclipse進行項目構建、編譯、測試、部署,所有的一切都依賴於IDE。如果離開IDE(比如需要在某臺linux機器上進行開發,沒有GUI界面),則想調試將比較難(因爲本地有時無法模擬生產機器的環境),於是最近利用空餘時間看了下Ant構建方面的資料,整出如下build.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== 2011-6-23 下午05:23:55 chenza ====================================================================== --> <project name="monkey-build" default="package" basedir="."> <property name="src.dir" location="src"/> <property name="src.main.dir" location="${src.dir}/main"/> <property name="src.test.dir" location="${src.dir}/test"/> <property name="build.dir" location="build"/> <property name="build.main.dir" location="${build.dir}/main"/> <property name="build.test.dir" location="${build.dir}/test"/> <property name="lib.dir" location="lib"/> <property name="dist.dir" location="dist"/> <property name="bin.dir" location="bin"/> <property name="test.data.dir" location="${build.dir}/data"/> <property name="test.report.dir" location="${build.dir}/report"/> <property name="test.suite" value="com.monkey.util.HelloMonkeyTest"/> <property name="svn.remote.url" value="http://mushroom-monkey.googlecode.com/svn/mushroom/"/> <path id="path.svnant"> <pathelement location="${lib.dir}/svnjavahl.jar"/> <pathelement location="${lib.dir}/svnant.jar"/> <pathelement location="${lib.dir}/svnClientAdapter.jar"/> <pathelement location="${lib.dir}/ganymed.jar"/> <pathelement location="${lib.dir}/jna.jar"/> <pathelement location="${lib.dir}/svnkit.jar"/> <!-- --> </path> <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="path.svnant" /> <target name="init"> <tstamp/> <mkdir dir="${src.main.dir}"/> <mkdir dir="${src.test.dir}"/> <mkdir dir="${build.main.dir}"/> <mkdir dir="${build.test.dir}"/> <mkdir dir="${test.data.dir}"/> <mkdir dir="${test.report.dir}"/> <mkdir dir="${lib.dir}"/> <mkdir dir="${dist.dir}"/> <mkdir dir="logs"/> <mkdir dir="conf/spring"/> </target> <path id="project.classpath"> <fileset dir="${lib.dir}" id="lib_dir"> <include name="**/*.jar"/> </fileset> </path> <path id="build.main.path"> <pathelement location="${build.main.dir}"/> <pathelement location="${build.test.dir}"/> </path> <target name="compile.main" depends="init"> <javac encoding="UTF-8" srcdir="${src.main.dir}" destdir="${build.main.dir}" debug="true" includeantruntime="true" includejavaruntime="true"> <classpath refid="project.classpath"/> </javac> </target> <target name="compile.test" depends="compile.main"> <javac encoding="UTF-8" srcdir="${src.test.dir}" destdir="${build.test.dir}" debug="true" includeantruntime="true" includejavaruntime="true"> <classpath refid="project.classpath"/> <classpath refid="build.main.path"/> </javac> </target> <target name="package" depends="compile.main"> <delete file="${dist.dir}/monkey-sdk.jar"></delete> <pathconvert property="mf.classpath" pathsep=" "> <path refid="project.classpath" /> <globmapper from="${lib.dir}*.jar" to="lib*.jar" /> </pathconvert> <jar destfile="${dist.dir}/monkey-sdk.jar" basedir="${build.main.dir}" includes="com/**"> <manifest> <attribute name="Main-Class" value="com.monkey.util.HelloMonkey"/> <attribute name="Class-Path" value="${mf.classpath}"/> </manifest> </jar> </target> <target name="package.src"> <delete file="${dist.dir}/monkey-sdk-src.jar"></delete> <jar destfile="${dist.dir}/monkey-sdk-src.jar" basedir="${src.dir}"/> </target> <target name="ipconfig"> <exec executable="ipconfig"> </exec> </target> <target name="test-basic" depends="compile.test"> <!-- printsummary="withOutAndErr" --> <junit haltonfailure="true" printsummary="false"><!-- build process halt when test fail --> <classpath refid="project.classpath"/> <classpath refid="build.main.path"/> <formatter type="brief" usefile="false"/> <formatter type="xml"/> <!-- <test name="${test.suite}"/> --> <batchtest todir="${test.data.dir}"> <fileset dir="${build.test.dir}" includes="**/*Test.class" /> </batchtest> </junit> <junitreport todir="${test.data.dir}"> <fileset dir="${test.data.dir}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${test.report.dir}"/> </junitreport> </target> <target name="test-report" depends="compile.test"> <junit printsummary="false" failureproperty="test.fail" errorproperty="test.fail"> <classpath refid="project.classpath"/> <classpath refid="build.main.path"/> <formatter type="brief" usefile="false"/> <formatter type="xml"/> <batchtest todir="${test.data.dir}"> <fileset dir="${build.test.dir}" includes="**/*Test.class" /> </batchtest> </junit> <junitreport todir="${test.data.dir}"> <fileset dir="${test.data.dir}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${test.report.dir}"/> </junitreport> <fail if="test.fail"> Tests failed. Check ${test.report.dir} </fail> </target> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="update"> <svn javahl="true" username="[email protected]" password="*********"> <checkout url="${svn.remote.url}" revision="HEAD" destpath="${build.dir}" /> </svn> </target> </project>
且當做筆記,同時也供有此困惑的朋友們參考,如有問題,請批評指正。