Ant是Java平臺(tái)下非常棒的批處理命令執(zhí)行程序,能非常方便地自動(dòng)完成編譯,測試,打包,部署等等一系列任務(wù),大大提高開發(fā)效率。如果你現(xiàn)在還沒有開始使用Ant,那就要趕快開始學(xué)習(xí)使用,使自己的開發(fā)水平上一個(gè)新臺(tái)階。
Eclipse中已經(jīng)集成了Ant,我們可以直接在Eclipse中運(yùn)行Ant。
以前面建立的Hello工程為例,創(chuàng)建以下目錄結(jié)構(gòu):
新建一個(gè)build.xml,放在工程根目錄下。build.xml定義了Ant要執(zhí)行的批處理命令。雖然Ant也可以使用其它文件名,但是遵循標(biāo)準(zhǔn)能更使開發(fā)更規(guī)范,同時(shí)易于與別人交流。
通常,src存放Java源文件,classes存放編譯后的class文件,lib存放編譯和運(yùn)行用到的所有jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文檔。
然后在根目錄下創(chuàng)建build.xml文件,輸入以下內(nèi)容:
<?xml version="1.0"?> <project name="Hello world" default="doc">
<!-- properies --> <property name="src.dir" value="src" /> <property name="report.dir" value="report" /> <property name="classes.dir" value="classes" /> <property name="lib.dir" value="lib" /> <property name="dist.dir" value="dist" /> <property name="doc.dir" value="doc"/>
<!-- 定義classpath --> <path id="master-classpath"> <fileset file="${lib.dir}/*.jar" /> <pathelement path="${classes.dir}"/> </path>
<!-- 初始化任務(wù) --> <target name="init"> </target>
<!-- 編譯 --> <target name="compile" depends="init" description="compile the source files"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4"> <classpath refid="master-classpath"/> </javac> </target>
<!-- 測試 --> <target name="test" depends="compile" description="run junit test"> <mkdir dir="${report.dir}"/> <junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true"> <classpath refid="master-classpath" /> <formatter type="plain"/> <batchtest todir="${report.dir}"> <fileset dir="${classes.dir}"> <include name="**/*Test.*"/> </fileset> </batchtest> </junit> <fail if="tests.failed"> *********************************************************** **** One or more tests failed! Check the output ... **** *********************************************************** </fail> </target>
<!-- 打包成jar --> <target name="pack" depends="test" description="make .jar file"> <mkdir dir="${dist.dir}" /> <jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}"> <exclude name="**/*Test.*" /> <exclude name="**/Test*.*" /> </jar> </target>
<!-- 輸出api文檔 --> <target name="doc" depends="pack" description="create api doc"> <mkdir dir="${doc.dir}" /> <javadoc destdir="${doc.dir}" author="true" version="true" use="true" windowtitle="Test API"> <packageset dir="${src.dir}" defaultexcludes="yes"> <include name="example/**" /> </packageset> <doctitle><![CDATA[<h1>Hello, test</h1>]]></doctitle> <bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom> <tag name="todo" scope="all" description="To do:" /> </javadoc> </target> </project> |
以上xml依次定義了init(初始化),compile(編譯),test(測試),doc(生成文檔),pack(打包)任務(wù),可以作為模板。
選中Hello工程,然后選擇“Project”,“Properties”,“Builders”,“New…”,選擇“Ant Build”:
填入Name:Ant_Builder;Buildfile:build.xml;Base Directory:${workspace_loc:/Hello}(按“Browse Workspace”選擇工程根目錄),由于用到了junit.jar包,搜索Eclipse目錄,找到j(luò)unit.jar,把它復(fù)制到Hello/lib目錄下,并添加到Ant的Classpath中:
然后在Builder面板中鉤上Ant_Build,去掉Java Builder:
再次編譯,即可在控制臺(tái)看到Ant的輸出:
Buildfile: F:\eclipse-projects\Hello\build.xml
init:
compile: [mkdir] Created dir: F:\eclipse-projects\Hello\classes [javac] Compiling 2 source files to F:\eclipse-projects\Hello\classes
test: [mkdir] Created dir: F:\eclipse-projects\Hello\report [junit] Running example.HelloTest [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec
pack: [mkdir] Created dir: F:\eclipse-projects\Hello\dist [jar] Building jar: F:\eclipse-projects\Hello\dist\hello.jar
doc: [mkdir] Created dir: F:\eclipse-projects\Hello\doc [javadoc] Generating Javadoc [javadoc] Javadoc execution [javadoc] Loading source files for package example... [javadoc] Constructing Javadoc information... [javadoc] Standard Doclet version 1.4.2_04 [javadoc] Building tree for all the packages and classes... [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes... [javadoc] Generating F:\eclipse-projects\Hello\doc\stylesheet.css... [javadoc] Note: Custom tags that could override future standard tags: @todo. To avoid potential overrides, use at least one period character (.) in custom tag names. [javadoc] Note: Custom tags that were not seen: @todo BUILD SUCCESSFUL Total time: 11 seconds |
Ant依次執(zhí)行初始化,編譯,測試,打包,生成API文檔一系列任務(wù),極大地提高了開發(fā)效率。將來開發(fā)J2EE項(xiàng)目時(shí),還可加入部署等任務(wù)。并且,即使脫離了Eclipse環(huán)境,只要正確安裝了Ant,配置好環(huán)境變量ANT_HOME=<Ant解壓目錄>,Path=…;%ANT_HOME%\bin,在命令行提示符下切換到Hello目錄,簡單地鍵入ant即可。
相關(guān)文章:
http://www-128.ibm.com/developerworks/cn/java/j-lo-ant-eclipse/index.html