如何从macrodef返回值?



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

<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}赋值,将抛出目标"构建",如果它发生在有效输入的情况下,它不会做我的东西,因为for循环将以顺序的方式执行。(例如platforms=windows,mac,android, project.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任务运行反贡献任务,如:

<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>

我有一个例子,从调用<tstamp>返回时间

希望这对你有帮助!

<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>

相关内容

  • 没有找到相关文章

最新更新