13

我正在使用 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

这取决于我使用 equals 任务的事实${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>
4

5 回答 5

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>
于 2011-02-25T15:05:03.107 回答
17

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>
于 2011-02-25T14:13:37.053 回答
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任务来取消设置属性。

于 2011-12-27T10:01:13.680 回答
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>  

然后在蚂蚁的任何地方,就像属性标签一样调用它

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

此示例仅作为编写宏以替换 ant-contrib 中的 <var> 命令的示例。在这种情况下,使用 <if> 命令时,使用 <var> 更有意义,因为 ant-contrib 已经加载,并且 <var> 的处理速度可能更快。

希望这可以帮助。

于 2012-11-27T18:04:54.997 回答
-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”以外的值。

于 2013-01-31T02:30:19.917 回答