【问题标题】:ant-how can i return value from macrodef?ant-我如何从宏定义中返回值?
【发布时间】:2014-08-02 14:55:23
【问题描述】:

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

<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} 具有相同的值时,它应该调用指定的目标。但是在这个 sn-ps 中,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>

【问题讨论】:

    标签: ant ant-contrib


    【解决方案1】:

    我有一个例子,它返回调用&lt;tstamp&gt;的时间

    希望这会有所帮助!

    <macrodef name="gettime">
        <attribute name="time-stamp" default=""/>
        <sequential>
            <tstamp>
                <format property="time-stamp" pattern="yyyyMMdd_HHmmss" />
            </tstamp>
        </sequential>   
    </macrodef>
    
    <target name="test-time">
        <gettime/>
        <echo message="${time-stamp}" />
    </target>
    

    【讨论】:

      【解决方案2】:
      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多