Ant入門-配置和使用

來源:互聯網
上載者:User

在環境變數修改屬性

在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.