ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Diane Holt <hol...@yahoo.com>
Subject Re: Newbie question about if/then/else
Date Sat, 20 Apr 2002 19:41:58 GMT
I don't know for sure that I completely understood what you're wanting to
do, so the following is just an example of if/then/else'ing in Ant.

This example uses "standard" Ant:
  <target name="doit">
    <antcall target="copyfiles">
      <param name="file" value="foo"/>
    </antcall>
    <antcall target="copyfiles">
      <param name="file" value="bar"/>
    </antcall>
    <antcall target="copyfiles">
      <param name="file" value="blat"/>
    </antcall>
  </target>

  <target name="copyfiles" depends="copyIfAvailable,copyIfNotAvailable"/>

  <target name="copyIfAvailable" depends="chkFile" if="isAvailable">
    <echo>${file}.txt is available...</echo>
  </target>

  <target name="copyIfNotAvailable" depends="chkFile"
unless="isAvailable">
    <echo>${file}.txt is not available...</echo>
  </target>

  <target name="chkFile">
    <available property="isAvailable" file="${file}.txt"/>
  </target>

Note: You don't need the 'depends' for the "copyIfNotAvailable" target,
unless you want to be able to use it standalone (ie., called directly,
rather than as a dependency of the "copyfiles" target).

This example uses the ant-contrib <foreach> and <if> tasks (which require
Ant1.5alpha):
  <target name="doit">
    <foreach list="foo,bar,blat" target="copyfiles" param="file"/>
  </target>

  <target name="copyfiles">
    <available property="isAvailable" file="${file}.txt"/>
    <if>
      <istrue value="${isAvailable}"/>
    <then>
      <echo>${file}.txt is available...</echo>
    </then>
    <else>
      <echo>${file}.txt is not available...</echo>
    </else>
    </if>
  </target>

Both end up with the same results, so it's really just a matter of which
you prefer (personally, I prefer the second, since it's more compact and,
I think, clearer). Note: For this type of thing, I recommend using
NoBannerLogger, so you don't end up with bunches of empty targets being
logged:
$ ant -f call.xml
copyIfAvailable:
     [echo] foo.txt is available...

copyIfAvailable:
     [echo] bar.txt is available...

copyIfNotAvailable:
     [echo] blat.txt is not available...

$ant -f if.xml
copyfiles:
     [echo] foo.txt is available...

copyfiles:
     [echo] bar.txt is available...

copyfiles:
     [echo] blat.txt is not available...

Hope this helps,
Diane

=====
(holtdl@yahoo.com)



__________________________________________________
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/

--
To unsubscribe, e-mail:   <mailto:ant-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-user-help@jakarta.apache.org>


Mime
View raw message