[Ant] Ant之MacroDef—“宏

    转载自:http://lhq1013.iteye.com/blog/1157234   

Ant是一个基于Java的跨平台构建工具,它易于使用,并且可扩展、可升级。
    MacroDef——“宏”,在Ant中是非常实用的,它的作用相当于java里面的为防止代码重复而提取的公共方法。比如我现在需要将某几个目录分别进行编译和打包,不使用MacroDef的情况下,如果有10个目录,那么我就至少要写10个类似的<target></target>来做完对这10个目录的操作;但在使用MacroDef的情况下,你只用写一个通用的<macrodef></macrodef>,再在其他地方调用它就可以了,既减少了代码量,又提高了工作效率。
    MacroDef的具体书写格式和参数,在官网使用手册(http://ant.apache.org/manual/)上都有,这里就不做一一解释了,下面直接举了个具体的例子。


不使用MacroDef的情况下,编译和打包math和io等目录:

<target name="build_math" depands="base">
    <echo message="... building jck-api-math-cases  now ..."/>
    <javac debug="false"  destdir="bin" source="1.5" target="1.5">
        <src path="./java_math"/>
        <classpath refid="java_jck.classpath"/>
    </javac>
</target>

<target name="make_math" depends="build_math">
    <echo message="... make math jar ...."></echo>
    <jar jarfile="math.jar" basedir="./bin"/>
    <move file="./math.jar" tofile="./lex/math.jar" overwrite="true"/>
</target>

<target name="build_io" depands="base">
    ……
</target>

<target name="make_io" depends="build_io">
    ……
</target>
……

使用MacroDef的情况下,编译和打包math和io等目录:
———————————————————————————————————————

<macrodef name="dealWithTestCases">            //macrodef的定义,定义了name属性
    <attribute name="tarName" />              //参数定义
    <attribute name="srcPath" />
    <!--element name="dex-elements"/-->
    <sequential>               //实际执行的内容在<sequential>里
        <echo message="... building jck-api-@{tarName}-cases  now ...."/>
        <javac debug="false"  destdir="bin" source="1.5" target="1.5" includeantruntime="on">
            <src path="./@{srcPath}"/>
            <classpath refid="java_jck.classpath"/>
            <excludesfile name="exclude.@{tarName}"/>
        </javac>
        <echo message="... make @{tarName} jar ..."></echo>
        <jar jarfile="@{tarName}.jar" basedir="./bin"/>
        <move file="./@{tarName}.jar" tofile="./lex/@{tarName}.jar" overwrite="true"/>
    </sequential>
</macrodef>

<target name="lex" depands="base">
    <dealWithTestCases tarName="math" srcPath="./java_math"/>     //外部调用宏
    <dealWithTestCases tarName="io" srcPath="./java_io"/>    
    ……    
</target>

从例子中可以明显看出,MacroDef的使用不仅仅是减少了重复的工作量,性能影响可能非常显著,并且还可能产生更易读和更易于维护的编译文件。

 ------------------------------------------------------------------------

Android 使用zipalign进行优化的代码如下:

macrodef定义如下:

 <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
         debug, -debug-with-emma and release.-->
    <macrodef name="zipalign-helper">
        <attribute name="in.package" />
        <attribute name="out.package" />
        <sequential>
            <zipalign
                    executable="${zipalign}"
                    input="@{in.package}"
                    output="@{out.package}"
                    verbose="${verbose}" />
        </sequential>
    </macrodef>

调用macro的地方:

<target name="-release-sign" if="has.keystore" >
        <!-- only create apk if *not* a library project -->
        <do-only-if-not-library elseText="Library project: do not create apk..." >
            <sequential>
                <property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-release-unaligned.apk" />

                <!-- Signs the APK -->
                <echo level="info">Signing final apk...</echo>
                <signapk
                        input="${out.packaged.file}"
                        output="${out.unaligned.file}"
                        keystore="${key.store}"
                        storepass="${key.store.password}"
                        alias="${key.alias}"
                        keypass="${key.alias.password}"/>

                <!-- Zip aligns the APK -->
                <zipalign-helper
                        in.package="${out.unaligned.file}"
                        out.package="${out.final.file}" />
                <echo level="info">Release Package: ${out.final.file}</echo>
            </sequential>
        </do-only-if-not-library>
        <record-build-info />
    </target>

需要注意的是:
1、在整个build文件里,<macrodef>是和<target>平级的,上例中定义的<dealWithTestCases>若不将其放在<target>里面,执行Ant时,脚本会直接执行这部分的宏代码,而不会去执行<target>中定义的‘depands="base"‘依赖关系。
2、<macrodef>可以调用其他的<macrodef>,但不可以调用<target>;<target>可用调用<macrodef>,也可用调用其他<target>。

 3、所有任务的特性通过@{}展开而不是${},它们可以随宏的每一次调用而改变,且只在宏定义程序块内部可用。

原文地址:https://www.cnblogs.com/0616--ataozhijia/p/3732576.html