ant使用備忘

來源:互聯網
上載者:User

標籤:des   style   color   os   io   使用   java   ar   for   

ant是一個指令碼構建工具,可能就是持續整合裡面所需要的構建工具。

如果使用eclipse,裡面會內建有ant工具,不需要再安裝了,建立一個build.xml(或者其他的名字都可以),使用ant來運行就可以了

如果使用的命令列來運行ant,需要安裝的步驟如下:

(1)下載ant,解壓,無需安裝過程。

(2)將ant的bin目錄加入到系統的環境變數path中。

(3)運行ant,可以查看是否加成功了。

(4)在cmd中,將目錄切換到工程所在的build.xml目錄下,運行ant(這裡預設為build.xml檔案,還可以自己制定需要的檔案)

關於build.xml檔案的目錄結構

<project name="**">

<property name="src" value="test" />

<path id="classpath1.path">
<fileset dir="${lib.dir}">

<property name="dest" value="classes" />

<property name="lib.dir" value="lib"/>

<include name="*.jar" />

</fileset>

<pathelement location="${src}" />

<pathelement location="${dest}"/>
</path>

       <!-- 定義一些初始化的操作 !-->

<target name="init">
    <delete verbose="true" includeemptydirs="true">
    <fileset dir="${dest}">
        <include name="**/*"/>
    </fileset>
</delete>
<mkdir dir="${dest}" />
</target>  

<!--編譯 !-->

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="classpath1.path"/>
<echo message="${TIME}" />
</target>

<!--接下來有一些你需要做的事情,比如打包部署,或者運行一個程式等 !-->

...

</project>


一些需要記錄的東西:

(1)運行一個java程式的定義:

<target name="sendmail" depends="compile">
   <java classname="com.sogou.maps.testApi.Integration" classpathref="classpath1.path">
       <classpath path="${dest}"></classpath>
       <arg value="${basedir}/${TIME}/emailable-report.html"></arg>
   </java>
</target>

如果引用了第三方的包,那麼需要在classpathref中定義,否則會提示錯誤資訊。


(2)定義一個時間戳記

<tstamp> 
         <format property="TIME" pattern="yyyyMMdd-hhmm" locale="en,UK"/> 
    </tstamp>

只要定義了這個,就定義了三個參數:DSTAMP,TSTAMP,TODAY,在下面的內容中就可以使用${DSTAMP}等了。在上面我們定義了自己的參數TIME,可以使用。這個時間戳記在整個build過程中是不變的。


(3)在命令列裡啟動並執行參數

命令列選項總結:
ant [options] [target [target2 [target3] ...]]
Options:
-help print this message
-projecthelp print project help information
-version print the version information and exit
-quiet be extra quiet
-verbose be extra verbose
-debug print debugging information
-emacs produce logging information without adornments
-logfile file use given file for log output
-logger classname the class that is to perform logging
-listener classname add an instance of class as a project listener
-buildfile file use specified buildfile
-find file search for buildfile towards the root of the filesystem and use the first one found
-Dproperty=value set property to value 


(4)一些ant內建的內建變數

 

os.name:       作業系統的名稱。

basedir: 項目引用的根路徑。

ant.home:     Ant工具的根目錄位置。

ant.file: 構件檔案的絕對路徑。

ant.version:   ant的版本資訊。

ant.java.version: ant檢測到的JVM版本。

ant.project.name: 當前執行的項目名稱及資訊。

java.version: Java的版本資訊。

java.home:    Java的根目錄位置。

java.class.path: Java類庫的檔案的位置。

line.separator:   分行符號.


(5)編譯的時候如果在windows下面使用命令列來運行ant的話,工程使用UTF-8,編譯會有編碼錯誤。需要設定編譯的編碼

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="classpath1.path">
<compilerarg line="-encoding UTF-8"></compilerarg>
</javac>
<echo message="${TIME}" />
</target>

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.