ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rhino" <rhi...@sympatico.ca>
Subject Re: Perform different target depending on *value* of property
Date Fri, 15 Oct 2004 14:12:16 GMT
I came up with a slightly different solution before I saw your note, Jan, so
I'm including mine here too for the sake of the archives.

I *love* these solutions: they are so much more elegant than any other
approaches I have seen to this problem. They also allow the property to have
as many values as I want, not just 'true' or 'false'.

Here is my version of the solution, fully commented and in the context of
what I was originally trying to do. Thanks to all who responded on this
thread!!

---
<?xml version="1.0"?>

<project name="cond3" default="end" basedir=".">

<!--==============================================================

Determine which server is the target.

==============================================================-->

<target name="getserver" description="Determine which server is the target">

<input message="Which server should receive the files? 1. Foo 2. Bar 3.
Test"

validargs="1,2,3"

addproperty="server.choice"

defaultvalue="2"/>

</target>

<!--==============================================================

Set the variable which forms the key part of the properties file

name. Then obtain the properties file for the appropriate server.

==============================================================-->

<target name="getprops" depends="getserver" description="Get the appropriate
properties file depending on the server which was chosen">

<condition property="servername" value="foo">

<equals arg1="${server.choice}" arg2="1"/>

</condition>

<condition property="servername" value="bar">

<equals arg1="${server.choice}" arg2="2"/>

</condition>

<condition property="servername" value="test">

<equals arg1="${server.choice}" arg2="3"/>

</condition>

<property file="server.${servername}.properties"/>

</target>

<!--==============================================================

Get the userid and password for the desired server.

==============================================================-->

<target name="getlogin" depends="getprops" description="Get userid and
password for server.">

<input message="Please supply the userid for the ${server} server:"
addproperty="userid" defaultvalue="dougb"/>

<input message="Please supply the password for the ${server} server:"
addproperty="password" defaultvalue="dougbpw"/>

</target>

<!--==============================================================

Execute the appropriate upload target, depending on which server

was chosen.

==============================================================-->

<target name="upload" depends="getlogin" description="Upload to the selected
server.">

<antcall target="upload-${servername}"/>

</target>

<!--==============================================================

Upload to the 'foo' server.

==============================================================-->

<target name="upload-foo" description="Upload to the foo server.">

<echo message="Uploading to foo...."/>

<echoproperties prefix="server"/>

</target>

<!--==============================================================

Upload to the 'bar' server.

==============================================================-->

<target name="upload-bar" description="Upload to the bar server.">

<echo message="Uploading to bar...."/>

<echoproperties prefix="server"/>

</target>

<!--==============================================================

Upload to the 'test' server.

==============================================================-->

<target name="upload-test" description="Upload to the test server.">

<echo message="Uploading to test...."/>

<echoproperties prefix="server"/>

</target>

<!--==============================================================

Display a message to the user.

==============================================================-->

<target name="end" depends="upload" description="Display a message.">

<echo message="Uploads to server ${servername} are complete."/>

</target>

</project>


---

----- Original Message ----- 
From: <Jan.Materne@rzf.fin-nrw.de>
To: <user@ant.apache.org>
Sent: Friday, October 15, 2004 9:54 AM
Subject: AW: Perform different target depending on *value* of property


Works. Had done that some times before :)

<project default="call">
    <target name="foo-upload"><echo>FOO upload</echo></target>
    <target name="bar-upload"><echo>BAR upload</echo></target>
    <target name="test-upload"><echo>TEST upload</echo></target>

    <target name="call">
        <input message="Please choose the server "
               validargs="foo,bar,test"
               addproperty="server"
               defaultvalue="test"
        />
        <antcall target="${server}-upload"/>
    </target>
</project>


Jan






> -----Ursprüngliche Nachricht-----
> Von: RADEMAKERS Tanguy [mailto:Tanguy.RADEMAKERS@swift.com]
> Gesendet am: Freitag, 15. Oktober 2004 15:32
> An: Ant Users List
> Betreff: RE: Perform different target depending on *value* of property
>
> I think he means you would have:
>
> <target name="foo-upload">...</target>
> <target name="bar-upload">...</target>
> <target name="test-upload">...</target>
>
> and <antcall target="${servername}-upload"/> would call the
> appropriate one (and only that one). Worth a try - if it
> works, it's brilliant.
>
> /t
>
> >-----Original Message-----
> >From: Rhino [mailto:rhino1@sympatico.ca]
> >Sent: Friday, October 15, 2004 3:26 PM
> >To: Ant Users List
> >Subject: Re: Perform different target depending on *value*
> of property
> >
> >I'm not sure how to work your suggestion into my script.
> >
> >Let's say I have three different values of 'servername',
> >"foo", "bar", and
> >"test", and three targets, "foo-upload", "bar-upload", and
> >"test-upload".
> >That would suggest that my script should say:
> >
> ><antcall target="foo-upload"/>
> ><antcall target="bar-upload"/>
> ><antcall target="test-upload"/>
> >
> >How would I ensure that if the servername was foo, *only*
> >"foo-upload" was
> >executed and not "bar-upload" or "test-upload"? Wouldn't I still need
> >something on the upload targets or the antcalls to ensure
> that only the
> >desired upload target was executed? If so, what would I need?
> >
> >Rhino
> >
> >----- Original Message ----- 
> >From: <Jan.Materne@rzf.fin-nrw.de>
> >To: <user@ant.apache.org>
> >Sent: Friday, October 15, 2004 9:16 AM
> >Subject: AW: Perform different target depending on *value*
> of property
> >
> >
> >What´s about
> >    <antcall target="${servername}-upload"/>
> >
> >Jan
> >
> >
> >-----Ursprüngliche Nachricht-----
> >Von: Rhino [mailto:rhino1@sympatico.ca]
> >Gesendet am: Freitag, 15. Oktober 2004 15:09
> >An: ant-user
> >Betreff: Perform different target depending on *value* of property
> >
> >I want to revise an existing script so that it does a
> >different target for
> >each of several different *values* of a property.
> >
> >For example, if the property, which is named 'server' has a
> >value of "foo",
> >I want the script to execute a target called "foo-upload". If
> >the value of
> >'server' is "bar", I want the script to execute a target called
> >"bar-upload". If the value of 'server' is "test", I want the
> script to
> >execute a target called "test-upload".
> >
> >Actually, although the target names begin with the server
> >name, that is more
> >convenience than necessity; it isn't critical that the target
> >names start
> >with the server name so I don't insist on that.
> >
> >I am running Ant 1.6.1 but would be willing to upgrade to a
> >newer version to
> >accomplish this. I would also like to stay "pure Ant" if
> >possible but would
> >still be interested in seeing solutions using optional tasks
> >or ant-contrib.
> >
> >I know that 'if' and 'unless' are the normal mechanisms for enabling
> >conditional execution of tasks but they only seem to work on
> >the basis of if
> >a property is enabled or not, rather than on the VALUE of
> the property.
> >
> >Rhino
> >---
> >rhino1 AT sympatico DOT ca
> >"There are two ways of constructing a software design. One way
> >is to make it
> >so simple that there are obviously no deficiencies. And the
> >other way is to
> >make it so complicated that there are no obvious
> >deficiencies." - C.A.R.
> >Hoare
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> >For additional commands, e-mail: user-help@ant.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>


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


Mime
View raw message