nantnantcontrib

Does NAnt have an if/then/else task?


Other build systems, e.g. Ant, have an if/then/else construct that allows for simplifying the script logic in many cases. The CIFactory NAnt variant has this as well (<ifthenelse/>), but is extremely outdated and does not support .NET 4.0 - nevertheless, updating your NAnt version from CIFactory to an official build complicates your build scripts needlessly, because you now need two <if/> tasks, one with the original condition, one with negation.

Is it possible in NAnt to achieve the if/then/else flow with a single condition?


Solution

  • In NAnt 0.92 a <choose/> task has been promoted from NAnt-contrib and allows you to achieve the if/then/else effect with only a single evaluation of the test condition. An example:

    <property name="operatingSystem"
      value="${operating-system::to-string(environment::get-operating-system())}" /> 
    <choose>
      <when test="${string::contains(operatingSystem, 'Windows')}">
        <echo message="Running on Microsoft Windows" />
      </when>
      <otherwise>
        <echo message="Are we running on Linux?" />
      </otherwise>
    </choose>