Ant之build.xml配置詳解【轉】


原文:https://blog.csdn.net/mevicky/article/details/72828554

前言
國內關於build.xml的配置資料太零散了,實在是受不了,故而將自己的筆記整理成博文,方便大家查閱和理解。

build.xml配置參數
構建文件默認叫build.xml,其有很多配置參數。

project
每個構建文件都有一個project標簽,有以下屬性:
- default:表示默認的運行目標,這個屬性是必須的。
- basedir:表示項目的基准目錄。
- name:表示項目名。
- description:表示項目的描述。

如下:

每個項目對應一個構建文件,但是如果項目比較復雜,業務線比較多,則有可能對應很多個構建文件,比如:

 

這時我們需要注意,每個構建文件都需要以project標簽包含起來。

 

property

類似於常量,可以供給build.xml中的其他標簽使用。有兩個特點:
    - 大小寫敏感
    - 不可改變,誰先設定,之后的都不能改變。

該標簽可以與多個屬性配合使用。
    - name和value:

<property name="module_name" value="admin"/> 

后面直接使用即可:

<echo message="begin nej-build ${module_name}..."/> 

- name和refid:

<property name="srcpath" refid="dao.compile.classpath"/> 

其中的dao.compile.classpath在別的地方進行了定義。當然,也可以通過直接引用的方式:

<property name="baseline.dir" value="${ob_baseline.dir}"/> 

- name和location:

<property name="srcdir" location="src"/> 

將srcdir的值設置為當前文件路徑/src。
- file:

<property file="./omad/build.properties"/> 

導入相對文件中的所有變量,這里的build.properties專門用來存放各種變量,示例如下:

 

url:

<property url="http://www.mysite.com/bla/props/foo.properties"/> 

導入對應文件的屬性


environment:

<property environment="env"/> 

設置系統的環境變量前綴為env。比如

<property name="tomcat.home" value="${env.CATALINA_HOME}"/> 

將系統的tomcat安裝目錄設置到tomcat.home屬性中。


import
引入別的xml文件,提高復用性:

<import file="./env-judge.xml"/>
<import file="./tasks.xml"/>

甚至可以批量匹配:

<copy todir="${basedir}/src/html/${html.dir}" overwrite="true" includeEmptyDirs="true">
<fileset dir="${basedir}/lib">
<include name="module-*/**" />
</fileset>
</copy>

 

target
任務,一個project標簽下有一個或多個target標簽,代表任務,任務間可以存在依賴關系。有如下屬性:
- name:用於標識,這個是必須的
- depends:用來指定所依賴的任務。

 

<!-- 初始化任務 --> 
<target name="init"> 
<echo message=" init ${init} ..."/> 
</target>

<!-- 編譯 --> 
<target name="compile" depends="init"> 
<delete dir="${classes.dir}" /> 
<mkdir dir="${classes.dir}" /> 
<javac srcdir="${src.dir}" destdir="${classes.dir}"> 
<classpath refid="master-classpath" /> 
</javac> 
</target> 
1

if:當屬性設置時才執行該任務。
<target name="sync_module_k12_teach" if="${is_k12_teach}">
<antcall target="sync_module_item">
<param name="html.dir" value="org"/>
</antcall>
</target>

<target name="sync_module_backend" if="${is_backend}">
<antcall target="sync_module_item">
<param name="html.dir" value="admin"/>
</antcall>
</target>

<target name="sync_module_k12_backend" if="${is_k12_backend}">
<antcall target="sync_module_item">
<param name="html.dir" value="admin"/>
</antcall>
</target>

通過判斷變量是否存在,執行不同的任務。
- unless:當屬性未設置時才執行。
- description:任務描述。

echo
控制台顯示

<echo message="begin clean res/module-xx、component-xx、res-base..."/>


delete
刪除文件或文件目錄,有如下屬性
- file:刪除文件
- dir:刪除目錄
- includeEmptyDirs:值得是否刪除空目錄,默認是true
- failonerror:報錯是否停止,默認是true
- verbose:是否列出刪除的文件,默認是false

示例如下:

 

<!--clean other dir-->
<target name="clean_other_dir">
<echo message="begin clean_other_dir..."/>
<delete dir="${basedir}/${compress.dir}"/>
<delete dir="${basedir}/pub"/>
<echo message="begin clean html module-xx..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/src/html" >
<include name="**/module-*/**"/>
</fileset>
</delete>
<echo message="begin clean res/module-xx、component-xx、res-base..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/res" >
<include name="module-*/**"/>
<include name="component-*/**"/>
<include name="res-base/**"/>
</fileset>
</delete>
</target>

 

mkdir
創建一個目錄

<mkdir dir=”${class.root}”/>


copy
拷貝文件或文件目錄,屬性如下:
- file:表示源文件。
- tofile:表示目標文件。
- todir:表示目標目錄。
- overwrite:是否覆蓋目標文件,默認為false。
- includeEmptyDirs:是否拷貝空目錄,默認為true。
- failonerror:如目標沒有發現是否自動停止,默認值true。
- verbose:是否顯示詳細信息,默認值false。

示例:

<target name="cp">
<copy todir="${compress.dir}" overwrite="true">
<fileset dir="${ob_baseline.dir}">
<include name="pub/" />
<include name="res/" />
<include name="mail_template/" />
</fileset>
</copy>
</target>

 

fileset
文件集標簽,通常與任務結合來使用,例如上面的copy的demo中,通過將fileset定義的文件路徑下的文件,拷貝到todir指定的路徑中。
也可以用於批量刪除:

<delete includeemptydirs="true">
<fileset dir="${basedir}/src/html" >
<include name="**/module-*/**"/>
</fileset>
</delete>
<echo message="begin clean res/module-xx、component-xx、res-base..."/>
<delete includeemptydirs="true">
<fileset dir="${basedir}/res" >
<include name="module-*/**"/>
<include name="component-*/**"/>
<include name="res-base/**"/>
</fileset>
</delete>

 

也就是說,但凡遇到文件集操作,都需要用到fileset標簽。

exec
用來執行系統命令,或者指定環境的命令。
比如:

<target name="test">
<exec executable="cmd.exe">
<arg line="/c dir"/>
</exec>
</target>

打開命名行,並轉到c盤執行dir命令。

能夠執行系統命令,就相當於可以執行各種環境比如node、gulp、bower等等:

 

<!--build style-->
<target name="build_style">
<echo message="begin build_style..."/>
<exec dir="." executable="gulp" failonerror="true">
<arg line="scss"/>
</exec>
</target>

<!--bower cache clean if必須是${]才是判斷true,false, 否則只要有設定值即可執行-->
<target name="bower_cache_clean" if="${is_bower_cache_clean}">
<echo message="begin bower_cache_clean ..."/>
<exec dir="." executable="bower" failonerror="true">
<arg line="cache clean" />
</exec>
</target>

 

antcall
執行某個定義的任務。

<target name="sync_module_teach" if="${is_teach}">
<antcall target="sync_module_item">
<param name="html.dir" value="org"/>
</antcall>
</target>

執行sync_module_item任務,並設置參數html.dir的值為org。
該任務定義如下:

<target name="sync_module_item">
<echo message="begin sync_module ${html.dir}..."/>
<copy todir="${basedir}/src/html/${html.dir}" overwrite="true" includeEmptyDirs="true">
<fileset dir="${basedir}/lib">
<include name="module-*/**" />
</fileset>
</copy>
</target>

或者更為簡單的表達:

<target name="deploy">
<echo message="begin auto deploy......"/>
<antcall target="clean"/>
<antcall target="bower_install"/>
<antcall target="cnpm_install"/>
<antcall target="sync_module"/>
<antcall target="build_style"/>
<antcall target="nej_build" />
<antcall target="cp"/>
</target>

 

parallel
並行執行多個子任務。

 

<parallel failonany="true">
<antcall target="sync_module_corp"/>
<antcall target="sync_module_main"/>
<antcall target="sync_module_teach"/>
<antcall target="sync_module_backend"/>
<antcall target="sync_module_passport"/>
<antcall target="sync_module_business"/>
<antcall target="sync_module_k12_teach"/>
<antcall target="sync_module_k12_backend"/>

<antcall target="build_style"/>
</parallel>

通過failonany控制如果一個失敗,則不執行。通過並行執行,來提升性能,降低構建花費的時間。

regexp
用於正則的定義的使用,可以與matches結合使用。
比如,定義正則:

<regexp id="regexp_env_test" pattern="^${root_dir}/(${test_dir}|${test_k12_dir})/.+"/>
<regexp id="regexp_env_pre" pattern="^${root_dir}/(${pre_dir}|${pre_k12_dir})/.+"/>

通過pattern指定正則內容,通過id標識。
在需要匹配的時候,使用之:

<condition property="is_test">
<matches string="${basedir}">
<regexp refid="regexp_env_test"/>
</matches>
</condition>

 

condition
用來判斷,如果包含的內容符合條件,則將property指定的屬性設置為true,否則為false。
比如上面的例子中,就是將basedir變量的值和regexp_env_test對應的正則匹配,如果正確,就將is_test設置為true,然后后面的流程再去判斷。
與之配合的標簽有很多,下面一一介紹:
- istrue,isfalse:斷言

<condition property="is_test_backend">
<and>
<istrue value="${is_test}"/>
<istrue value="${is_backend}"/>
</and>
</condition>

只有is_test和is_backend變量的值均為true,is_test_backend的值才為true。
- and:邏輯與,需要都滿足條件才行,如上例所述。
- not:邏輯非,反過來的結果。
- or,xor:邏輯或和邏輯異或。
- isset:指定屬性是否存在:

<condition property="scondition">
<!--如果屬性name不存在則返回false-->
<isset property="name"/>
</condition>


equils:指定屬性是否相等:

<condition property="scondition">
<!--如果arg1的值與arg2的值相等返回true,否則為false-->
<equals arg1="${name}" arg2="this is name"/>
</condition>

 

filesmatch:指定文件是否相等:

<condition property="scondition">
<!--如果file1所代表的文件與file2所代表的文件相等返回true,否則為false-->
<filesmatch file1="testfile1.txt" file2="testfile2.txt"/>
</condition>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM