Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Problem using <if> task

Help
2006-12-05
2013-04-24
  • Didier Bretin
    Didier Bretin
    2006-12-05

    Hello,

    I try to use a <if> task with the output of a <sshexec> command. Here is my task:

    <target name="verify" description="test if the war file exists on the serveur">
        <sshexec host="${host}" username="${username}" password="${password}" trust="yes"
            outputproperty="onNausicaa"
            command="${script}/exist_war.sh ${remote}/webapps/${tomcat.manager.war}" />
        <echo message="onNausicaa=${onNausicaa}" />
        <if>
            <equals arg1="${onNausicaa}" arg2="${ok}" />
            <then>
                <echo message="${tomcat.manager.war} on server" />
            </then>
            <else>
                <echo message="${tomcat.manager.war} not on server" >
            </else>
        </if>
    </target>

    My <sshexec> task return "yes" or "no", I tested it. in ${ok} I have "yes". And apparently the <if> task is always launching the <else> command.

    Did I forget something ?

    Regards.

     
    • Curt Arnold
      Curt Arnold
      2006-12-05

      I'd be concerned that maybe there is whitespace around the response that may cause the string comparison to not give the result that you expect.

      To test how about adding a:

      <condition property="on-server" value="true">
      <equals arg1="${onNausicaa}" arg2="${ok}" />
      </condition>
      <fail unless="on-server">"${tomcat.manager.war} not on server</fail>

      If you get past the fail task and still go into the else clause, then something is wrong.  Otherwise, you need to check how you are formulating the check.

      Alternatively, you could change the equals to

      <equals arg1="${ok}" arg2="${ok}" />

      and if that still goes into the else clause then we have a problem.

       
      • Didier Bretin
        Didier Bretin
        2006-12-06

        The solution of sherpaguy is working for me, but I would like to test your solution too. When I add your lines to my ant script I got an error:

        C:\Documents and Settings\dbretin\Mes documents\Projets Eclipse\myproject\build.xml:183: Class net.sf.antcontrib.logic.IfTask doesn't support the nested "condition" element.

        I use ant-1.6.5 and ant-contrib-1.0b3 on an XP box with java 1.5.0_06.

        Do you see why I have this error ?

        Regards.

         
        • Curt Arnold
          Curt Arnold
          2006-12-06

          I was suggesting putting the condition and fail tasks before the if tasks, not inside of it.  Looks like you have confirmed that it was a whitespace issue, so the if task was doing what it should be.

           
    • Michael
      Michael
      2006-12-05

      You didn't say what the value in ${onNausicaa} is, but I'm assuming that it's a "yes" or "no" value.  If so, then you'll need to check for other characters (spaces, tabs, etc) or mixed case.  The <equals> condition has several optional parameters that might be useful: http://ant.apache.org/manual/CoreTasks/conditions.html

      If ${onNausicaa} contains more than just a simple yes/no, then perhaps you can use the <contains> instead.  I use the <if> with sshexec output properties in my scripts, so it should work.

      -Michael

       
      • Didier Bretin
        Didier Bretin
        2006-12-06

        Thanks, I use a trim="yes" in the <if> task and it works !