Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ant contrib-if/then/else任务_Ant_Ant Contrib - Fatal编程技术网

ant contrib-if/then/else任务

ant contrib-if/then/else任务,ant,ant-contrib,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 wi

我使用的是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>

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>

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



我们也可以使用
var
task来取消设置属性

我知道这很古老,但对于其他寻找解决方案的人来说应该很方便

要在不使用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>

此示例仅作为编写宏以替换ant contrib中的命令的示例。在这样的情况下,如果正在使用该命令,则在已加载ant contrib并且处理速度可能更快的情况下,使用Since ant contrib更有意义


希望这有帮助。

可以使用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”以外的值。

谢谢您,ruffp,我更喜欢使用DoctorRuss变量的其他解决方案。我不知道属性是不可重写的。例如:[echo]color=red没有问题,我根据我的平台给出了解决方案,并且我能够对其进行测试(因为我不使用ant contrib)。无论如何,我的答案可以帮助一些像我这样不使用ant contrib的人。干杯,谢谢。正是我的情况-需要一个没有ant contrib的答案+1无法在maven antrun插件中使用,因为有多个目标节点。谢谢DoctorRuss,正如对ruffp的评论,我不知道属性不可重写。我将使用变量。这并不是完全正确的,我在Ant1.8中做到了以下几点:ant属性是不可变的。这意味着一旦设置了属性,就不能更改它的值。有用的宏。如果您出于某种原因不能/不想使用ant-contrib,可能会很方便。谢谢。我现在经常使用这些类型的脚本来增强我的ant环境,而无需添加JAR文件。
<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>