【问题标题】:Structure ant projects结构蚂蚁项目
【发布时间】:2013-04-28 13:48:59
【问题描述】:

我目前正在编写一个 ant 项目 xml 文件,我正在寻找一些提示和技巧来改进项目的结构和可读性。

<target name="eatnutsOnClient" >
<monkey.eatnuts clientName="${clientName}" label="${nutLabel}" />
<if><not> <equals arg1="${returnCode}" arg2="0"/> </not><then>
 <echo message="eatnuts-[${nutlabel}]_[${returnCode}]${line.separator}" file="${reachedFile}" append="true" />
</then></if>
</target>
<target name="eatnuts" depends="createClient,eatnutsOnClient,destroyClient"/>

为了管理返回代码,我希望能够通过一种可以调用来处理返回代码逻辑的函数来替换我需要在相当多的目标上复制的完整 if 部分。我想一个选择是创建一个仅包含 if 部分的目标并将其添加到每个任务的依赖列表中?有更好的方法吗?

【问题讨论】:

    标签: ant ant-contrib


    【解决方案1】:

    Ant &lt;macrodef&gt; 提供了一种类似函数的方式来共享代码:

    <project name="ant-macrodef-echo" default="run">
        <taskdef resource="net/sf/antcontrib/antlib.xml" />
    
        <macrodef name="echo-macrodef">
            <attribute name="returnCode"/>
            <sequential>
                <if>
                    <not>
                        <equals arg1="@{returnCode}" arg2="0"/>
                    </not>
                    <then>
                        <echo message="@{returnCode}" />
                    </then>
                </if>
            </sequential>
        </macrodef>
    
        <target name="run">
            <echo-macrodef returnCode="42"/>
            <echo-macrodef returnCode="0"/>
            <echo-macrodef returnCode="-9"/>
        </target>
    </project>
    

    结果:

    run:
       [echo] 42
       [echo] -9
    

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2011-09-12
      相关资源
      最近更新 更多