ant - using conditions with in a target

Discussion in 'Java' started by coltrane, Mar 30, 2006.

  1. coltrane

    coltrane Guest

    Is it possible to use a condition within a target?

    I have a target that contains some steps that only need to be run if a
    certain condition exists.
    I know how to set a condition property but how can I use an "if" to
    control the flow of the steps.

    something like this ( I know the if is invalid )

    <target name="sometarget">
    <delete dir="directoryOne"/>
    <if condition>
    <delete dir="directoryTwo"/>
    </if>
    </target>


    thanks for your help

    john
     
    coltrane, Mar 30, 2006
    #1
    1. Advertising

  2. coltrane

    SMC Guest

    On Fri, 31 Mar 2006 05:57:30 +1000, coltrane wrote:

    > Is it possible to use a condition within a target?
    >
    > I have a target that contains some steps that only need to be run if a
    > certain condition exists.
    > I know how to set a condition property but how can I use an "if" to
    > control the flow of the steps.
    >
    > something like this ( I know the if is invalid )
    >
    > <target name="sometarget">
    > <delete dir="directoryOne"/>
    > <if condition>
    > <delete dir="directoryTwo"/>
    > </if>
    > </target>


    If the deletion order is not important:

    <target name="sometarget" depends="othertarget">
    <delete dir="directoryOne"/>
    </target>

    <!-- Assume deleteDirTwo property is set somewhere else -->
    <target name="othertarget" if="deleteDirTwo">
    <delete dir="directoryTwo"/>
    </target>

    Otherwise:

    <target name="sometarget">
    <delete dir="directoryOne"/>
    <antcall target="othertarget"/>
    </target>

    <!-- Assume deleteDirTwo property is set somewhere else -->
    <target name="othertarget" if="deleteDirTwo">
    <delete dir="directoryTwo"/>
    </target>


    --
    Sean

    If you want it real bad, you can get it *real bad*.
     
    SMC, Mar 31, 2006
    #2
    1. Advertising

  3. coltrane

    Wibble Guest

    SMC wrote:
    > On Fri, 31 Mar 2006 05:57:30 +1000, coltrane wrote:
    >
    >
    >>Is it possible to use a condition within a target?
    >>
    >>I have a target that contains some steps that only need to be run if a
    >>certain condition exists.
    >>I know how to set a condition property but how can I use an "if" to
    >>control the flow of the steps.
    >>
    >>something like this ( I know the if is invalid )
    >>
    >><target name="sometarget">
    >><delete dir="directoryOne"/>
    >><if condition>
    >><delete dir="directoryTwo"/>
    >></if>
    >></target>

    >
    >
    > If the deletion order is not important:
    >
    > <target name="sometarget" depends="othertarget">
    > <delete dir="directoryOne"/>
    > </target>
    >
    > <!-- Assume deleteDirTwo property is set somewhere else -->
    > <target name="othertarget" if="deleteDirTwo">
    > <delete dir="directoryTwo"/>
    > </target>
    >
    > Otherwise:
    >
    > <target name="sometarget">
    > <delete dir="directoryOne"/>
    > <antcall target="othertarget"/>
    > </target>
    >
    > <!-- Assume deleteDirTwo property is set somewhere else -->
    > <target name="othertarget" if="deleteDirTwo">
    > <delete dir="directoryTwo"/>
    > </target>
    >
    >

    And if you get up with that, use the ant-contrib jar.

    It has an an If task exactly like you wanted, and other good stuff.
    Ant is just a theoretical exercise without it.

    http://ant-contrib.sourceforge.net/ant-contrib/manual/tasks/if.html
     
    Wibble, Mar 31, 2006
    #3
  4. coltrane

    coltrane Guest

    Thanks for the idea.

    I also think I might perform use an antcall to call a target with an if
    condition.

    Thanks again for the help

    john
     
    coltrane, Mar 31, 2006
    #4
  5. coltrane

    coltrane Guest

    Thanks,

    I might give this a try.

    john
     
    coltrane, Mar 31, 2006
    #5
  6. coltrane

    coltrane Guest

    wow, I what a great package!

    thanks again
     
    coltrane, Mar 31, 2006
    #6
  7. coltrane

    Roedy Green Guest

    On 31 Mar 2006 04:44:09 -0800, "coltrane" <>
    wrote, quoted or indirectly quoted someone who said :

    >I also think I might perform use an antcall to call a target with an if
    >condition.


    here is an ant conditional copy. This is why I spit on XML as too
    impoverished for programming and too bloated.

    <!-- copy screenshot png from website to project dir, if it exists -->
    <available property="has.screenshot"
    file="${screenshot.dir}/${ant.project.name}.png" />
    <antcall target="copy.screenshot" />

    <!-- copy screenshot from website to project dir, if there is one. -->
    <target name="copy.screenshot" if="has.screenshot">
    <copy file="${screenshot.dir}/${ant.project.name}.png"
    tofile="${package.dir}/${ant.project.name}screenshot.png"
    failonerror="false" overwrite="true" />
    </target>


    What this says is :

    if exists E:\mindprod\screenshot\currcon.png
    copy E:\mindprod\screenshot\currcon.png
    C:\com\mindprod\currcon\currconscreenshot.png

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 31, 2006
    #7
  8. coltrane

    Guest

    Roedy Green wrote:
    > On 31 Mar 2006 04:44:09 -0800, "coltrane" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >I also think I might perform use an antcall to call a target with an if
    > >condition.

    >
    > here is an ant conditional copy. This is why I spit on XML as too
    > impoverished for programming and too bloated.
    >
    > <!-- copy screenshot png from website to project dir, if it exists -->
    > <available property="has.screenshot"
    > file="${screenshot.dir}/${ant.project.name}.png" />
    > <antcall target="copy.screenshot" />
    >
    > <!-- copy screenshot from website to project dir, if there is one. -->
    > <target name="copy.screenshot" if="has.screenshot">
    > <copy file="${screenshot.dir}/${ant.project.name}.png"
    > tofile="${package.dir}/${ant.project.name}screenshot.png"
    > failonerror="false" overwrite="true" />
    > </target>
    >
    >
    > What this says is :
    >
    > if exists E:\mindprod\screenshot\currcon.png
    > copy E:\mindprod\screenshot\currcon.png
    > C:\com\mindprod\currcon\currconscreenshot.png
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Java custom programming, consulting and coaching.


    Why doesn't just the copy suffice?

    <copy file="${screenshot.dir}/${ant.project.name}.png"
    tofile="${package.dir}/${ant.project.name}screenshot.png"
    failonerror="false" overwrite="true" />

    XML is ugly but not unusable. Ant without If, for, runtarget and other
    ant-contrib
    tasks is unusable, but thats not because of XML.
     
    , Mar 31, 2006
    #8
  9. coltrane

    Roedy Green Guest

    On 31 Mar 2006 11:56:46 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >Why doesn't just the copy suffice?


    because it generates bogus error messages if the file does not exist.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 31, 2006
    #9
  10. coltrane

    chenzhen_li

    Joined:
    Oct 18, 2007
    Messages:
    1
    How do i put an else to send a message error?

    From Chile, my english is poor, but i'll give a try...

    How do i make an else here?. Let's say to put an error message if the file doesn't exist?.

    Thanks.
     
    chenzhen_li, Oct 18, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don
    Replies:
    0
    Views:
    4,389
  2. Thomas Stien
    Replies:
    0
    Views:
    486
    Thomas Stien
    Oct 18, 2004
  3. hust6
    Replies:
    4
    Views:
    1,521
    hust6
    Jul 7, 2005
  4. DaveJ
    Replies:
    1
    Views:
    983
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    May 19, 2007
  5. mohit
    Replies:
    0
    Views:
    589
    mohit
    Jan 23, 2008
Loading...

Share This Page