How to do an IF/ELSE in NANT 0.91?
Asked Answered
D

2

6

We're using Nant 0.91 so <choose> is not available. How would you do an IF/ELSE in Nant 0.91? I would like to do something like (using NAnt 0.92 syntax) in NAnt 0.91. [I am not allowed to modify the current installation of NAnt 0.91]:

<choose>
   <when test="${deploy.env=='PROD'}">
        <property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
    </when>
    <otherwise>
        <property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
    </otherwise>
</choose>
Democritus answered 22/9, 2015 at 14:18 Comment(0)
C
10

The simplest solution, which we use here, is to just use two if tasks, one with a negative test to the other:

<if test="${deploy.env == 'PROD'}">
    <property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
</if>
<if test="${deploy.env != 'PROD'}">
    <property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
</if>

However in your case, you can also make use of the fact that the property task has inbuilt if/unless functionality:

<property name="deploy.root.dir" if="${deploy.env == 'PROD'}" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
<property name="deploy.root.dir" unless="${deploy.env == 'PROD'}" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
Clisthenes answered 22/9, 2015 at 14:26 Comment(1)
Awesome! Exactly what I was looking for!Democritus
S
0

You can set a default property value and then an override if the condition is true:

<property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${deploy.env}\${app.dest.dir}\" />
<if test="${deploy.env == 'PROD'}">
    <property name="deploy.root.dir" value="\\${deploy.server}\${deploy.mode}\${app.dest.dir}\" />
</if>
Sorbian answered 21/3, 2018 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.