Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant-contrib - if/then/else task

I am using ant, and I have a problem with if/then/else task, (ant-contrib-1.0b3.jar). I am running something that can be simplified with build.xml below.

I am expecting to obtain from 'ant -Dgiv=Luke' the message

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

but it seems property "giv" is not overwritten inside if/then/else..

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

Is it depending from the fact I am using equals task with ${giv} ? Otherwise what is wrong in my code?

build.xml CODE:

<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>
like image 349
Francesco Avatar asked Feb 25 '11 11:02

Francesco


People also ask

How to use if else in Ant?

So, you could use if="${copy-condition}" instead of if="copy-condition" . In ANT 1.7. 1 and earlier, you specify the name of the property. If the property is defined and has any value (even an empty string), then it will evaluate to true.

What is Macrodef in ant?

This is used to specify attributes of the new task. The values of the attributes get substituted into the templated task. The attributes will be required attributes unless a default value has been set.

What is target in Ant build?

A target is a container of tasks and datatypes that cooperate to reach a desired state during the build process. Targets can depend on other targets and Apache Ant ensures that these other targets have been executed before the current target.


1 Answers

In Ant a property is always set once, after that variable is not alterable anymore.

Here follows a solution using standard Ant (without ant-contrib) which could be useful for the people who does not want an extra dependency.

<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>
like image 123
рüффп Avatar answered Oct 04 '22 05:10

рüффп