ant-contrib - if / then / else任务

时间:2011-02-25 11:23:57

标签: ant ant-contrib

我正在使用ant,我遇到if / then / else任务的问题,(ant-contrib-1.0b3.jar)。 我正在运行一些可以使用build.xml进行简化的东西。

我期待从'ant -Dgiv = Luke'获得消息

input name: Luke
should be overwritten with John except for Mark: John

但似乎属性“giv”在if / then / else中没有被覆盖..

input name: Luke
should be overwritten with John except for Mark: Luke

是否取决于我使用${giv}等于任务的事实? 否则我的代码有什么问题?

build.xml代码:

<project name="Friend" default="ifthen" basedir=".">

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
    <equals arg1="${giv}" arg2="Mark" />
    <then>
    </then>
    <else>
        <property name="giv" value="John" />
    </else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>

5 个答案:

答案 0 :(得分:34)

在Ant中,属性总是设置一次,之后该变量不再可变。

以下是使用标准Ant(不含ant-contrib)的解决方案,这对于不想要额外依赖的用户非常有用。

<target name="test"  >
    <echo message="input name: ${param}" />

    <condition property="cond" >
        <equals arg1="${param}" arg2="Mark" />
    </condition>
</target>

<target name="init" depends="test" if="cond"> 
    <property name="param2" value="Mark" />
</target>

<target name="finalize" depends="init"> 
    <property name="param2" value="John" />
    <echo message="should be overwritten with John except for Mark: ${param2}" />
</target>

答案 1 :(得分:15)

Ant属性很难覆盖(如果不是不可能的话)。你需要的是Variable。这些也在Ant Contrib JAR中定义。

编辑您的示例:

  <target name="ifthen"> 
    <var name="Evangelist" value="${giv}" />
    <echo message="input name: ${Evangelist}" />
    <if>
      <equals arg1="${Evangelist}" arg2="Mark" />
      <then>
      </then>
      <else>
        <var name="Evangelist" value="John" />
      </else>
    </if>   
    <echo message="should be overwritten with John except for Mark: ${Evangelist}" />
 </target>

答案 2 :(得分:2)

<project name="Friend" default="ifthen" basedir=".">

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
    <equals arg1="${giv}" arg2="Mark" />
    <then>
    </then>
    <else>
        <var name="giv" unset="true"/>
        <property name="giv" value="John" />
    </else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>

我们也可以使用var任务取消设置属性。

答案 3 :(得分:2)

我知道这已经过时了,但对于寻求解决方案的其他人来说应该会很方便。

在不使用ant-contrib的情况下重新分配属性,将macrodef与脚本一起使用。

<macrodef name="property-change"> 
    <attribute name="name"/>
    <attribute name="value"/>
    <sequential> 
        <script language="javascript"><![CDATA[
            project.setProperty("@{name}", "@{value}");
        ]]></script>
    </sequential> 
</macrodef>  

然后在ant的任何地方,只需将其称为属性标记

<property-change name="giv" value="John"/>

要在原始版本的xml中实现它,它看起来像这样:

<project name="Friend" default="ifthen" basedir=".">

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
    <equals arg1="${giv}" arg2="Mark" />
    <then>
    </then>
    <else>
        <property-change name="giv" value="John" />
    </else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>

此示例仅作为编写宏以替换&lt; var&gt;的示例。 ant-contrib中的命令。在像这样的情况下,&lt; if&gt;正在使用命令,使用&lt; var&gt;更有意义已经加载了sinnce ant-contrib,并且&lt; var&gt;可能会更快处理。

希望这有帮助。

答案 4 :(得分:-1)

可以使用ant-contrib'propertycopy'重新分配属性的值。这是使用ant-contrib变量的替代方法。 这样可以覆盖属性“giv”。

<target name="ifthen">
  <echo message="input name: ${giv}" />
  <if>
    <equals arg1="${giv}" arg2="Mark" />
    <then>
    </then>
    <else>
      <property name="tempName" value="John" />
      <propertycopy name="giv" from="tempName" override="true" />
    </else>
  </if>
  <echo message="should be overwritten with John except for Mark: ${giv}" />
</target>

请注意,这假设属性tempName尚未设置为“John”以外的值。

相关问题