[圖文]Eclipse快速上手指南 (3)

Eclipse快速上手指南 (3)
 
 

如果您有任何疑問,請到開發論壇上提問。

 

 

5. 在Eclipse中使用Ant

Ant是Java平臺下非常棒的批處理命令執行程序,能非常方便地自動完成編譯,測試,打包,部署等等一系列任務,大大提高開發效率。如果你現在還沒有開始使用Ant,那就要趕快開始學習使用,使自己的開發水平上一個新臺階。

Eclipse中已經集成了Ant,我們可以直接在Eclipse中運行Ant。

以前面建立的Hello工程爲例,創建以下目錄結構:


 
新建一個build.xml,放在工程根目錄下。build.xml定義了Ant要執行的批處理命令。雖然Ant也可以使用其它文件名,但是遵循標準能更使開發更規範,同時易於與別人交流。

通常,src存放Java源文件,classes存放編譯後的class文件,lib存放編譯和運行用到的所有jar文件,web存放JSP等web文件,dist存放打包後的jar文件,doc存放API文檔。

然後在根目錄下創建build.xml文件,輸入以下內容:

<?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>

    <!-- 初始化任務 -->
    <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(打包)任務,可以作爲模板。

選中Hello工程,然後選擇“Project”,“Properties”,“Builders”,“New…”,選擇“Ant Build”:

填入Name:Ant_Builder;Buildfile:build.xml;Base Directory:${workspace_loc:/Hello}(按“Browse Workspace”選擇工程根目錄),由於用到了junit.jar包,搜索Eclipse目錄,找到junit.jar,把它複製到Hello/lib目錄下,並添加到Ant的Classpath中:

然後在Builder面板中鉤上Ant_Build,去掉Java Builder:
 

再次編譯,即可在控制檯看到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依次執行初始化,編譯,測試,打包,生成API文檔一系列任務,極大地提高了開發效率。將來開發J2EE項目時,還可加入部署等任務。並且,即使脫離了Eclipse環境,只要正確安裝了Ant,配置好環境變量ANT_HOME=<Ant解壓目錄>,Path=…;%ANT_HOME%/bin,在命令行提示符下切換到Hello目錄,簡單地鍵入ant即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章