Java構建工具Ant小記(一)

來源:互聯網
上載者:User

標籤:

Ant簡介   

Ant是基於java的構建工具。理論上來說它類似與make工具,但是卻克服了make的一些固有的缺陷.   

傳統的Make是基於作業系統shell的構建工具,雖然也可以基於工作的os對make工具進行擴充,但卻難以實現跨平台構建.Ant基於java擴充功能,並且通過在xml中的target中定義的task進行構建任務的定義.其中每一個任務都是實現特定任務介面的類.同時Ant也提供了exec任務允許調用不同的作業系統的shell命令.

Ant主要元素介紹

Ant使用xml檔案定義構建過程,每一個構建檔案都會包括一個project和至少一個target.Target會包含task元素。每一個task元素可以通過id屬性被引用.

1.Projects

Project元素有三個屬性:name、default、basedir.同時其可以包含一個description元素對項目進行描述.

name:project的名稱,不是必須屬性;

default:執行構建的時候不指定要執行的target,則預設執行此設定的target;

basedir:設定基準目錄,則後續所有的相對位置則以此設定為基準路徑;如果不設定則以包含構建檔案的目錄為基準目錄;

<?xml version="1.0" encoding="UTF-8"?><project name="ProjectExample" default="default"    basedir=".">    <description>            學習project的用法    </description>    <target name="default"  >        <echo>${basedir}</echo>    </target>    <target name="echoBasedir">            <echo>${basedir}</echo>        </target></project>
View Code

2.Targets

每個Project會定義一個或者多個target.Target是待執行的task的集合.當啟動執行ant的時候可以指定要執行的target,如果沒有指定則執行project設定的預設的target/一個target可以通過depends來定義其依賴的其他的target,比如打包的target依賴編譯的target.

<?xml version="1.0" encoding="UTF-8"?><!-- ======================================================================      2015年6月1日 下午9:35:09                                                             targetExample         description                        hou                                                                     ====================================================================== --><project name="targetExample" default="default">    <description>            description    </description>    <!-- =================================           target: default                       ================================= -->    <target name="default" depends="depends" description="description">        <echo>excuting default target</echo>    </target>    <!-- - - - - - - - - - - - - - - - - -           target: depends                               - - - - - - - - - - - - - - - - - -->    <target name="depends">        <echo>excuting depends  target</echo>    </target></project>
View Code

3.Tasks

 Task是一段可以被執行的代碼.一個task可以有多個屬性(attribute).這些屬性(attribute)的值可以引用屬性(property),被引用的屬性會在任務執行之前被解析成具體的值.task的通用結構如下

<name attribute1=”value1” attribute2=”value2”.../>

Ant既內建了一些常用的task,同時也提供了便利的擴充機制.

<?xml version="1.0" encoding="UTF-8"?><!-- ======================================================================      2015年6月1日 下午9:59:23                                           taskExample         description                        hou                                                                     ====================================================================== --><project name="taskExample" default="default">    <description>            description    </description>        <property name="dirName" value="taskdir "></property>    <!-- =================================           target: default                       ================================= -->    <target name="default"   description="description">        <echo>list  current directory </echo>        <exec  executable="ls"></exec>        <echo>make [${dirName}] directory </echo>        <mkdir dir="${dirName}"/>        <echo>list  current directory </echo>         <exec  executable="ls"></exec>        <delete  dir="${dirName}"></delete>            </target>        </project>
View Code

4.Properties

屬性是通過設定一個可以在構建過程中重複使用的字串來達到自訂構建過程的重要方式.通常情況下是在構建檔案中直接定義屬性,當然也可以在外部定義屬性.屬性的名字是大小寫敏感的.屬性的引用是通過${屬性名稱}來實現的.Ant中的屬性類似java裡的欄位,但是屬性一經設定就不可更改.

Ant 中有許多預定義的屬性.首先, Java 環境設定用於運行 Ant 的所有系統屬性, 均可作為 Ant 屬性使用,比如 ${user.home} .除了這些屬性之外,Ant 還定義了它自己的一小組屬性,包括${ant.version},這個屬性包含 Ant 的版本;以及 ${basedir},這個屬性是項目目錄的絕對路徑(由包含組建檔案的目錄所定義,或者由 project 元素的可選 basedir 屬性所定義). 屬性經常用於引用檔案系統上的檔案或目錄,但是對於使用不同路徑分隔字元(例如,/ 與 \)的平台來說,這樣可能在跨越不同平台時導致問題.Ant 的 location 屬性專門設計用於以平台無關的方式包含檔案系統路徑.

<project name="propertiesExample" default="dist" basedir="../">    <description>        simple propertiesExample build file    </description>  <!-- set global properties for this build -->  <property name="src" location="src"/>  <property name="build" location="build"/>  <property name="dist"  location="dist"/>  <target name="init">    <!-- Create the time stamp -->    <tstamp/>    <!-- Create the build directory structure used by compile -->    <mkdir dir="${build}"/>  </target>  <target name="compile" depends="init"        description="compile the source " >    <!-- Compile the java code from ${src} into ${build} -->    <javac srcdir="${src}" destdir="${build}"/>  </target>  <target name="dist" depends="compile"        description="generate the distribution" >    <!-- Create the distribution directory -->    <mkdir dir="${dist}/lib"/>    <!-- Put everything in ${build} into the wufengtinghai.alm-${DSTAMP}.jar file -->    <jar jarfile="${dist}/lib/wufengtinghai.alm-${DSTAMP}.jar" basedir="${build}"/>  </target>  <target name="clean"        description="clean up" >    <!-- Delete the ${build} and ${dist} directory trees -->    <delete dir="${build}"/>    <delete dir="${dist}"/>  </target></project>
View Code

運行Ant

Ant作為命令列工作,自然可以通過unix/linux的shell和windows的cmd進行條用.同時可以通過java的IDE進行調用,比如eclipse.

從命令列直接調用ant,這樣ant會從目前的目錄尋找預設的build.xml,並使用預設的target執行產生過程.同時也可以通過ant -f buildfilename targetname來指定構建檔案和target名稱.

Eclipse對ant提供了強大的支援.首先支援文法高亮/智能提示/大綱視圖.

在eclipse中選中構建檔案,右鍵選擇”Run As”-”Ant Build”,並執行即可.

 

 

Java構建工具Ant小記(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.