ant-contrib - if/then/else任务

Fra*_*sco 13 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
Run Code Online (Sandbox Code Playgroud)

但似乎属性"giv"不会被覆盖在if/then/else中.

input name: Luke
should be overwritten with John except for Mark: Luke
Run Code Online (Sandbox Code Playgroud)

是否取决于我使用等于任务的事实${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>
Run Code Online (Sandbox Code Playgroud)

рüф*_*ффп 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>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.正是我的情况 - 需要一个没有ant-contrib的答案.+1 (5认同)

Doc*_*uss 16

Ant属性很难覆盖(如果不是不可能的话).你需要的是一个变量.这些也在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>
Run Code Online (Sandbox Code Playgroud)