project
項目定義,一個ant文件就是一個 project,定義了項目名稱,起始位置以及默認執行的 target。
<project name="Easily" basedir="." default="build">
property
屬性定義,可以定義的屬性包括:文件屬性、字符串定義。
<property file="build.properties"/>
<property name="WIDTH" value="1200"/>
<property name="HEIGHT" value="750"/>
<property name="PROJECT_DIR" value="${basedir}/../"/>
<property name="SOURCE_DIR" value="${PROJECT_DIR}/src"/>
taskdef
任務定義,可以理解為具體執行的任務所需要的第三方庫,比如編譯 as3 就需要引入 flexTasks.jar ,比如在代碼中需要用到 Math 的時候,需要引入 include Math,同樣的道理。
<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar"/>
target
可以理解為 method,是 ant 執行的最小單位,每個 target 會有一個名稱,可以主動的調用執行 。
<target name="build">
antcall
target 調用,用於執行 target 。
<antcall target="buildswf"/>
mxmlc
編譯 as3 項目,生成 swf 文件,需要指定入口文件 file,輸出文件 output 。
- incremental 是否增量編譯
- define 編譯參數
- load-config 項目配置文件,有需要的話可以自己編寫,沒有不聲明也可以
- static-link-runtime-shared-libraries 運行時庫是否靜態鏈接
- compiler.debug 調試信息
- default-size 缺省尺寸
- compiler.include-libraries 將指定目錄下的 swc 文件編譯進目標文件,不管項目中是否引用
- compiler.library-path 將指定目錄下的 swc 文件引入項目中,並將引用到的部分代碼編譯進目標文件
- compiler.external-library-path 將指定目錄下的 swc 文件引入項目中,作為外部鏈接,注意,運行時如果沒有找到相關定義會報錯
- source-path 外部文件引用
<mxmlc file="${SOURCE_DIR}/Easily.as"
output="${OUTPUT_DIR}/Easily.swf"
show-actionscript-warnings="false"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="true"
use-resource-bundle-metadata="true"
incremental="false">
<define name="CONFIG::debug" value="true"/>
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<static-link-runtime-shared-libraries>true</static-link-runtime-shared-libraries>
<compiler.debug>true</compiler.debug>
<!-- Set size of output SWF file. -->
<default-size width="${WIDTH}" height="${HEIGHT}"/>
<!-- Include all these swcs -->
<compiler.include-libraries dir="${LIBS_DIR}" append="true">
<include name="*.swc" />
<exclude name="data.swc"/>
</compiler.include-libraries>
<!-- Include the useful swcs -->
<compiler.library-path dir="${LIBS_DIR}" append="true">
<include name="*.swc"/>
</compiler.library-path>
<!-- Reference the external swcs -->
<compiler.external-library-path dir="${LIBS_DIR}" append="true">
<include name="*.swc" />
</compiler.external-library-path>
<source-path path-element="F:/My Documents/SVN/Box2D/src"/>
</mxmlc>
compc
編譯 as3 項目,輸出 swc 庫,大部分選項都同 mxmlc ,需要注意的是 include-classes ,這個參數需要指定哪些類需要編譯進 swc 中,格式是以空格為分隔符的類的字符串列表,比如: org.easily.astar.AStar org.easily.astar.BinaryHeap org.easily.astar.Grid org.easily.astar.Node
<compc output="${OUTPUT_DIR}/easily.swc"
include-classes="${CLASSES}"
optimize="true"
benchmark="true"
strict="true"
actionscript-file-encoding="utf-8"
locale="en_US"
allow-source-path-overlap="true"
use-resource-bundle-metadata="true"
incremental="false">
<static-link-runtime-shared-libraries>true</static-link-runtime-shared-libraries>
<compiler.debug>false</compiler.debug>
<show-actionscript-warnings>false</show-actionscript-warnings>
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<source-path path-element="${SOURCE_DIR}"/>
<library-path dir="${PROJECT_DIR}/libs" includes="*" append="true"/>
<source-path path-element="F:/My Documents/SVN/Box2D/src"/>
</compc>
可以將類的定義放到另外一個文件中,比如 class.properties ,定義一個屬性為 CLASSES=xx xx xx,ant 是有相關的 api 可以將相關的類定義找到並處理好,只是語法過於擰巴,我寫了個 python 腳本來干這個事情:
import os
def findmatch(file_name, ext, excludes):
for exclude in excludes:
if file_name.find(exclude) != -1:
return False
return file_name.endswith(ext)
def list_file(dir_name, ext, excludes):
result = []
for root, dirs, files in os.walk(dir_name):
result.extend(os.path.join(root, file_name) for file_name in files if findmatch(file_name, ext, excludes))
return result
def list_class(root, root_sep, ext, excludes):
return (format_name(root_sep, file_name, ext) for file_name in list_file(root, ext, excludes))
def format_name(root_sep, file_name, ext):
return file_name.replace(ext, "").replace(root_sep, "").replace("\\", ".")
def export_file(root_list, ext, excludes, out_file):
with open(out_file, "w") as f:
f.write("CLASSES=")
for root in root_list:
f.writelines(class_name + " " for class_name in list_class(root, root + "\\", ext, excludes))
def main():
root_list = [os.getcwd()+"\\..\\src"]
ext = ".as"
excludes = ["Test.as"]
out_file = "class.properties"
export_file(root_list, ext, excludes, out_file)
if __name__ == "__main__":
main()
exec
執行腳本或者應用程序,可以指定應用程序和命令行參數。
<exec executable="/bin/sh">
<arg line = "-c 'php ${basedir}/../xml.php'" />
</exec>
