You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Ka...@alltel.com on 2002/04/20 05:29:28 UTC

Newbie question about if/then/else

All,
     This is what I want to do.

Assuming I 've a properties file as follows.

COMP=
VIEW=
TM_VIEW=system_TM_1.0_integration
SHARED_VIEW=system_SHARED_1.0_integration
CF_VIEW=system_CF_1.0_integration

     I want to supply two values in the command line parameter, like ant
-DVIEW=system_TM_1.0_integration -DCOMP=COMP_TM, and based on that I want
to do the following

<target name="jar" >
     <jar jarfile="/view/${VIEW}/${ROOT_DIR}/${COMP}/${COMP}.jar">
     <fileset dir="/view/${VIEW}/${ROOT_DIR}/${COMP}">
     <include name="**/*.class"/>
     </fileset>
     </jar>

     Here, I need to copy a file from a directory using each variable defined in the properties file. Out of these variables one variable is used in
the above target "jar"
and I  want to copy from a different path for that variable alone but the rest from the path which has similar pattern. I want to check for the jar
file created in the "jar" target
above and copy that jar for that varibale alone and for the rest copy the files as instructed below.

For example, in this case,

     <copy file="${system_TM_1.0_integration}/file" todir="
${CODE_BASE}../"/> --------------> I don't want to copy here and skip this
     <copy file="${system_SHARED_1.0_integration}/file" todir="
${CODE_BASE}../"/>
     <copy file="${system_CF_1.0_integration}/file" todir="
${CODE_BASE}../"/>

One thing I thought of is using the <available> to check for the jar file
created in jar target and call some three targets to do the copy, by
skipping the one I want to. But, something tells me there might be a easy
way. Infact, I 've some 6 variables which are used in this copy business.
I guess, what I need here is something like,

<if>
     <fileavailable >
<then>
     <copythis>
<else>
     <copythis>
and do this for every copy task, I have.

I would appreciate any help with this. If this is not a good idea, I am
open for any suggestion. If I didn't make this clear, please let me know. I
will try to explain better.

Thanks in advance.

Kailash













--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Newbie question about if/then/else

Posted by Diane Holt <ho...@yahoo.com>.
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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>