ANT在ECLIPSE中簡明入門

來源:互聯網
上載者:User
在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即可。

 

首先下載ant在http://jakarta.apache.org/builds,然後解壓縮到本地,我把它壓縮到C:/ant目錄下

在環境變數修改屬性

在path屬性前增加------C:/ant/bin;這樣就可以使用ant了

build.xml檔案都包含一個project和至少一個target元素,target元素中包含一個或多個任務元素,任務是一段可執行檔代碼。

根項目是project,三個屬性name,default,basedir

name指定工程的名字

default指定工程預設的target元素

basedir指定工程的基路徑,如果是“.”,表示為build.xml所在路徑

<project name="bookstore" default="about" basedir=".">

<target name="init">
        <tstamp/>

<!--初始化各個變數的名字-->
        <property name="build" value="build" /><!--編譯後的目錄-->
        <property name="src" value="src" /><!--源檔案的位置-->
       

        <!--用到的環境變數的包-->

          <property environment="myenv" />
        <property name="servletpath"  value="${myenv.CATALINA_HOME}/common/lib/servlet-api.jar" />
        <property name="mysqlpath" value="WEB-INF/lib/mysqldriver.jar" />

 <mkdir dir="${build}" /><!--調用前面的目錄參數是${xxx}-->
 <mkdir dir="${build}/WEB-INF" />
 <mkdir dir="${build}/WEB-INF/classes" />

      <copy todir="${build}" ><!--拷貝目錄檔案到新的目錄下-->
        <fileset dir="${basedir}"    ><!--原來的目錄是basedir參數的路徑-->
           <include name="*.jsp" />
           <include name="*.bmp" />
           <include name="WEB-INF/**" />
           <exclude name="build.xml" /><!--不拷貝這個檔案-->
        </fileset>
     </copy>
   </target>
       

<target name="compile" depends="init">        <!--它依賴init的執行,所以調用它先執行init-->

   <javac srcdir="${src}"
             destdir="${build}/WEB-INF/classes"
             classpath="${servletpath}:${mysqlpath}">
     </javac>
  </target>

 

<target name="bookstorewar" depends="compile">      <!--產生war的任務--> 

    <war warfile="${build}/bookstore.war" webxml="${build}/WEB-INF/web.xml">
 <lib dir="${build}/WEB-INF/lib"/>
 <classes dir="${build}/WEB-INF/classes"/>
 <fileset dir="${build}"/>
    </war> 
 </target>

  <target name="about" >    <!--預設的target-->
        <echo>
 This build.xml file contains targets
       for building bookstore web application
        </echo>
   </target>

</project>

 

我們這個build.xml在我們的應用的根目錄下

所以運行ant的方法是:在DOS下

1.進入C:/myApp,我們的應用目錄下 輸入: ant  (會搜尋當前路徑下的build.xml檔案)

2.直接輸入ant -buildfile c:/myApp/build.xml

3.直接輸入ant -buildfile c:/myApp/build.xml  about

以上三種方式都執行about的target,如果指想編譯java檔案,我們只要

ant -buildfile c:/myApp/build.xml  compile

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.