Ant 常用语法及选项

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>

你可能感兴趣的:(ant)