ant-如何从macrodef 返回值?

标签 ant ant-contrib

我需要比较两个字符串 args,我曾经从中获取一个 arg 作为运行时输入(例如 platform=windows,ios,mac ),另一个具有值列表,在 build.properties 下定义(例如 project.supportedplatforms=windows,mac )。如果条件匹配,那么它应该从一个宏定义到某个目标返回“真”否则“失败”。

<for list="${platform}" param="platformparam" trim="true">
  <sequential>
    <if>
      <isItemExists retToProp="@{platformparam}" />
      <then>
        <antcall target="package.@{platformParam}" />
      </then>
    </if>
  </sequential>
</for>

<macrodef name="isItemExists">
  <attribute name="retToProp" />
  <property name="itemtosearch" value="@{retToProp}" />
  <for list="${project.supportedplatforms}" param="listparam" trim="true">
    <if>
      <equals arg1="@{listparam}" arg2="@{platformparam}" />
      <then>
        <!-- return true -->
      </then>
      <else>
        <!-- return false -->
      </else>
    </if>
  </for>
</macrodef>

${platforms}${project.supportedplatforms}具有相同的值,它应该调用指定的目标。但是在这个片段中,macrodef-for 循环将执行 n 次,最后为 @{returnproperty} 分配的值是什么。 , 将抛出目标“build”,如果它在有效输入下发生这种情况,它不会做我的事情,因为 for 循环将以顺序方式执行。 (例如 platforms=windows,mac,androidproject.supportedplatforms=ios,android,windows ,如果我的列表看起来像这样,是否有任何可能的方法来获得我的结果)。
<for list="${platforms}" param="platformparam" trim="true">
  <sequential>
    <isItemExists returnProperty="returnProp" platforms="@{platformparam}" />
    <if>
      <equals arg1="${returnProp}" arg2="true" />
      <then>
        <antcall target="package.@{platformparam}" />
      </then>
    </if>
  </sequential>
</for>

<macrodef name="isItemExists">
  <attribute name="platform" />
  <attribute name="returnProperty" />
  <sequential>
    <var name="@{returnProperty}" unset="true" />
    <for list="${project.supportedplatforms}" param="listparam" trim="true">
      <if>
        <equals arg1="@{listparam}" arg2="@{platform}" />
        <then>
          <property name="@{returnProperty}" value="true" />
        </then>
        <else>
          <property name="@{returnProperty}" value="false" />
        </else>
      </if>
  </sequential>
</macrodef>

最佳答案

<target name="some-test-target">
        <for list="${platform}" param="platformparam" trim="true">
            <sequential>
                <isItemExists platform="@{platformparam}" returnProperty="returnProp" />
                <if>
                    <equals arg1="${returnProp}" arg2="true"/>
                    <then>
                        <antcall target="package.@{platformparam}"/>
                    </then>
                </if>
            </sequential>
        </for>
</target>

使用 sequential用于运行 ant-contrib 任务的任务,例如:
<macrodef name="isItemExists">
        <attribute name="platform"/>
        <attribute name="returnProperty"/>
        <sequential>
            <var name="@{returnProperty}" unset="true"/>
            <for list="${project.supportedplatforms}" param="listparam" trim="true">
                <sequential>
                <if>
                    <equals arg1="@{listparam}" arg2="@{platform}"/>
                    <then>
                        <property name="@{returnProperty}" value="true"/>
                    </then>
                    <else>
                        <property name="@{returnProperty}" value="false"/>
                    </else>
                </if>
                </sequential>
            </for>
        </sequential>
</macrodef>

关于ant-如何从macrodef 返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24181748/

相关文章:

oracle - 检查数据库(连接)是否存在的 Ant 任务?

java - 从属性文件创建 Ant 本地属性

java - Macro Def 属性不会改变其值

java - ANT-从多个包加载多个Java文件

ant - 如何使用 "if-else"模拟 "condition"逻辑?

java - 发生 Ant BuildException : HTTP Authorization failure

java - 我应该在哪里存储我的图像?

java - 如何为Android库项目指定多个源目录

ant - 如何将 ivy 与 MSbuild 集成

ant - 需要 Ant 条件示例