1. 在工程目錄下新建一個build.xml檔案,如下圖所示,注意必須是在工程目錄下,而不是在工程目錄的src目錄裡。
2.編寫ant指令碼,內容如下,jar檔名稱(com.anllin.rup.bookshop.stub.jar)需要更改,其他通用。
<?xml version="1.0" encoding="utf-8"?> <project name="buildJarFile" default="compile" basedir="."> <!-- the directory containing source code--> <property name="src.dir" value="src" /> <!-- Temporary build directories --> <property name="build.dir" value="build" /> <property name="build.classes" value="${build.dir}/classes" /> <property name="build.lib" value="${build.dir}/lib" /> <!-- Target to create the build directories prior to the compile target--> <target name="init"> <mkdir dir="${build.dir}" /> <mkdir dir="${build.classes}" /> <mkdir dir="${build.lib}" /> </target> <target name="clean" description="Remove all generted files"> <delete dir="${build.dir}" /> </target> <target name="compile" depends="init" description="Compiles all source code"> <javac srcdir="${src.dir}" destdir="${build.classes}" /> </target> <target name="jar" depends="compile" description="Generates jar file in 'dist' diretory"> <!-- Exclude unit tests from the final JAR file--> <jar jarfile="${build.lib}/com.anllin.rup.bookshop.stub.jar" basedir="${build.classes}" excludes="**/*Test.class" /> </target> <target name="alll" depends="clean, jar" description="Cleans, complies, then builds the JAR files."> </target> </project>
3.選中build.xml,右鍵選擇run as -> ant build, 執行指令碼後,生成build目錄如下圖: