我需要比较两个字符串 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,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
用于运行 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/