ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rhino" <rhi...@sympatico.ca>
Subject Re: Properties set within conditions
Date Tue, 12 Oct 2004 17:48:25 GMT
I've improved on the technique that Vadim suggested so I thought I would
post it in case anyone working with conditionally-set properties had
problems somewhere down the road....

The only thing I didn't like about Vadim's suggestion was that it promised
to make the build file rather large if the number of conditional properties
got large. I realized that a property file that contained all the properties
needed for a given server was a better way to go since it would enable me to
handle any number of conditional properties cleanly. I searched the archive
for a way to load a property file conditionally and found a simple but
clever trick in a reply by Jan Materne.

Here is my revised build.xml which uses this technique:

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

<project name="Resume" default="sshexec" 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. Sympatico 2.
Tonge"

validargs="1,2"

addproperty="server.choice"

defaultvalue="2"/>

<!--echo message="Server choice is ${server.choice}"/-->

</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="sympatico">

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

</condition>

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

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

</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"/>

<!--echo message="The server name is ${server}. The server target directory
is ${server.target.dir}."/-->

<!--echo message="The userid is ${userid}. The password is ${password}."/-->

<!--echoproperties prefix="server"/-->

</target>



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

Scp


NOTES:

1. target directory MUST exist.

2. source directories that are empty never get created on the target
machine.

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

<target name="scp" depends="getlogin" description="scp copies local files to
remote machines via SSH">

<scp file="C:/Answer.txt"
todir="${userid}:${password}@${server}:${server.target.dir}" trust="true"/>

<!--scp file="C:/dbasic.log"
todir="${userid}:${password}@${server}:${server.target.dir}" trust="false"
knownhosts="${knownhosts}"/-->

<scp file="${userid}:${password}@${server}:/etc/ssh/ssh_host_key.pub"
todir="d:/eclipse/workspace/resume/xml" trust="true"/>

<scp todir="${userid}:${password}@${server}:${server.target.dir}"
trust="true">

<fileset dir="d:/myTempDir">

<include name="**/*"/>

</fileset>

</scp>

</target>

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

Sshexec

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

<target name="sshexec" depends="scp" description="sshexec is an SSH command
line">

<sshexec host="${server}" username="${userid}" password="${password}"
command="touch myfile" trust="true"/>

<sshexec host="${server}" username="${userid}" password="${password}"
command="rm myfile" trust="true"/>

<sshexec host="${server}" username="${userid}" password="${password}"
command="sh hello.bash" trust="true"/>

</target>

</project>

-----------------------------------------------------------------
The two properties files are also presented below.

server.sympatico.properties:
-----------------------------------------------------------------
server=www3.sympatico.ca

server.target.dir=/home/rhino

-----------------------------------------------------------------

server.tonge.properties:
-----------------------------------------------------------------
server=www.tonge.ca

server.target.dir=/home/rhino/public_html

-----------------------------------------------------------------

As you can see, the properties files can have any number of additional
properties added to them without requiring any changes to the build file,
except, of course, for the new tasks that use the new properties.

This solution is "pure Ant", except for the scp and sshexec tasks which
can't be done any other way, as far as I know. This solution was tested in
Ant 1.6.1 on Windows XP.


Rhino

----- Original Message ----- 
From: "Rhino" <rhino1@sympatico.ca>
To: "Ant Users List" <user@ant.apache.org>
Sent: Tuesday, October 12, 2004 12:46 PM
Subject: Re: Properties set within conditions


> Thanks for the clarification, Vadim! Your suggestion works perfectly.
>
> My script now does exactly what I want it to do. Also, I am using optional
> tasks only when no core tasks are available that do what I need, i.e. scp
> and sshexec. That seems like the best possible approach to me.
>
> Thanks again!
>
> Rhino
>
> ----- Original Message ----- 
> From: "Vadim Kazakov" <VadimK@quadrus.com>
> To: "Ant Users List" <user@ant.apache.org>
> Sent: Tuesday, October 12, 2004 12:25 PM
> Subject: RE: Properties set within conditions
>
>
> I think you misunderstood what I was trying to say, I was just saying
> that you could have set the properties in the setparms target, instead
> of calling down to Sympatico or Tongue. Like:
>
> <target name="setparms" description="Set the server-dependent
> properties">
>
> <condition property ="server" value="${hostname.sympatico}">
> <equals arg1="${server.choice}" arg2="1"/>
> </condition>
> <condition property ="server" value="${hostname.tonge}">
> <equals arg1="${server.choice}" arg2="2"/>
> </condition>
> <condition property ="server.resume.dir"
> value="${sympatico.resume.dir}">
> <equals arg1="${server.choice}" arg2="1"/>
> </condition>
> <condition property ="server.resume.dir" value="${tongue.resume.dir}">
> <equals arg1="${server.choice}" arg2="2"/>
> </condition>
> </target>
> -----Original Message-----
> From: Rhino [mailto:rhino1@sympatico.ca]
> Sent: October 12, 2004 9:52 AM
> To: Ant Users List
> Subject: Re: Properties set within conditions
>
> Vadim,
>
> I don't think I've ever seen a target that sets properties within a
> conditional before. Could you possibly provide an example?
>
> By the way, I've got my script working very nicely now with
> 'antcallback'.
> Thanks *very* much for this suggestion! However, I'd still like to see
> other
> ways to do the same thing, especially ways that don't involve installing
> extra tasks.
>
> Rhino
>
> ----- Original Message ----- 
> From: "Vadim Kazakov" <VadimK@quadrus.com>
> To: "Ant Users List" <user@ant.apache.org>
> Sent: Tuesday, October 12, 2004 11:27 AM
> Subject: RE: Properties set within conditions
>
>
> Why don't you try setting the properties using conditionals in the
> getserver target. Instead of calling down to sub targets.
>
> -----Original Message-----
> From: Rhino [mailto:rhino1@sympatico.ca]
> Sent: October 12, 2004 9:20 AM
> To: Ant Users List
> Subject: Re: Properties set within conditions
>
> Is 'antcall' (and 'antcallback') the only way to create my properties
> conditionally? I used 'antcall' and 'condition' to set my properties
> because
> they are the only ways I know to set a property conditionally within the
> Ant
> core tasks; if there is a better way, I'd love to hear about it.
>
> Rhino
>
>
> ----- Original Message ----- 
> From: "Vadim Kazakov" <VadimK@quadrus.com>
> To: "Ant Users List" <user@ant.apache.org>
> Sent: Tuesday, October 12, 2004 10:37 AM
> Subject: RE: Properties set within conditions
>
>
> This is because you're are setting these variables in an antcall. Which
> does not actually propogate up those properties. You could check out the
> antcallback task in ant-contrib. That will do what you want also see the
> recent thread about returning values from antcallback.
>
>
>
> ________________________________
>
> From: Rhino [mailto:rhino1@sympatico.ca]
> Sent: October 12, 2004 7:10 AM
> To: ant-user
> Subject: Properties set within conditions
>
>
>
> Hi,
>
>
>
> I'm having a problem with seeing a property that I set within some
> conditional logic. Some new properties are only visible within the
> target where I add them but I need them to be visible in later targets
> as well.
>
>
>
> I've spent several hours looking at the archives for this mailing list
> and trying everything I could think of but I can't seem to make my
> script work the way I want, even though it seems like something that
> should be pretty easy.
>
>
>
> First, let me explain in words what I want to do, then I'll show you the
> current version of the script, which doesn't quite work. Maybe you can
> suggest what I need to change to make this work.
>
>
>
> My script is going to upload some files to one of two servers, then run
> some commands on that server that involve the files I uploaded. The user
> is allowed to choose which of the two servers the user wants during the
> execution of the Ant script. Then, depending on which one the user
> chooses, certain properties like the hostname and a target directory for
> the uploads (for starters) are going to be set. Then, regardless of
> which server was selected, the userid and password for the server will
> be selected. Then, the upload will take place, followed by the commands
> that are running on the target server.
>
>
>
> The individual targets all seem to work okay when I run them alone,
> provided that they get the properties they need have been set at a
> script-wide level beforehand. I only get into trouble when I try to set
> properties in one target and then use them in a later target.
>
>
>
> Although I am using scp and sshexec in my Ant script for the upload and
> for running the commands that execute on the server, I would prefer to
> use only standard Ant tasks wherever possible in the rest of my script.
>
>
>
> Here is my script:
>
> -----------------------------------------------------------------------
>
> <?xml version="1.0"?>
>
> <project name="Resume" default="sshexec" basedir=".">
>
> <property name="hostname.sympatico" value="www3.sympatico.ca"/>
>
> <property name="sympatico.resume.dir" value="/home/rhino"/>
>
> <property name="hostname.tonge" value="www.tonge.ca"/>
>
> <property name="tonge.resume.dir" value="/home/rhino/public_html"/>
>
> <property name="display.userid.pw" value="true"/>
>
> <!--==================================================================
>
> 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. Sympatico 2.
> Tonge"
>
> validargs="1,2"
>
> addproperty="server.choice"
>
> defaultvalue="2"/>
>
> <echo message="Server choice is ${server.choice}"/>
>
> </target>
>
> <target name="setparms" description="Set the server-dependent
> properties">
>
> <condition property="Sympatico">
>
> <equals arg1="${server.choice}" arg2="1"/>
>
> </condition>
>
> <antcall target="sympatico"/>
>
> <antcall target="tonge"/>
>
> </target>
>
> <target name="sympatico" if="Sympatico" description="Set the
> server-dependent properties for Sympatico.">
>
> <echo>server = Sympatico</echo>
>
> <property name="server" value="${hostname.sympatico}"/>
>
> <property name="server.resume.dir" value="${sympatico.resume.dir}"/>
>
> <echo message="The Server hostname is ${server}. The resume directory is
> ${server.resume.dir}."/>
>
> </target>
>
> <target name="tonge" unless="Sympatico" description="Set the
> server-dependent properties for Tonge.">
>
> <echo>server = Tonge</echo>
>
> <property name="server" value="${hostname.tonge}"/>
>
> <property name="server.resume.dir" value="${tonge.resume.dir}"/>
>
> <echo message="The Server hostname is ${server}. The resume directory is
> ${server.resume.dir}."/>
>
> </target>
>
> <target name="getlogin" description="Get userid and password for
> server.">
>
> <input message="Please supply the userid for the server:"
> addproperty="userid" defaultvalue="dougb"/>
>
> <input message="Please supply the password for the server:"
> addproperty="password" defaultvalue="dougbpw"/>
>
> </target>
>
> <target name="display" description="Display server-dependent
> information.">
>
> <echo message="The userid is ${userid} and the password is
> ${password}"/>
>
> <echo message="The Server hostname is ${server}. The resume directory is
> ${server.resume.dir}."/>
>
> </target>
>
>
>
> <!--==================================================================
>
> Scp
>
> NOTES:
>
> 1. target directory MUST exist.
>
> 2. source directories that are empty never get created on the target
> machine.
>
> ==================================================================-->
>
> <target name="scp"
> depends="getserver,setparms,sympatico,tonge,getlogin,display"
> description="scp copies local files to remote machines via SSH">
>
> <scp file="C:/Answer.txt"
> todir="${userid}:${password}@${server}:/home/rhino" trust="true"/>
>
> <!--scp file="C:/dbasic.log"
> todir="${userid}:${password}@${server}:/home/rhino" trust="false"
> knownhosts="${knownhosts}"/-->
>
> <scp file="${userid}:${password}@${server}:/etc/ssh/ssh_host_key.pub"
> todir="d:/eclipse/workspace/resume/xml" trust="true"/>
>
> <scp todir="${userid}:${password}@${server}:/home/rhino/tmp"
> trust="true">
>
> <fileset dir="d:/myTempDir">
>
> <include name="**/*"/>
>
> </fileset>
>
> </scp>
>
> </target>
>
>
>
> <!--==================================================================
>
> Sshexec
>
> ==================================================================-->
>
> <target name="sshexec" depends="scp" description="sshexec is an SSH
> command line">
>
> <echo message="The Server hostname is ${server}. The resume directory is
> ${server.resume.dir}."/>
>
> <echo message="The userid is ${userid}. The password is ${password}."/>
>
> <sshexec host="${server}" username="${userid}" password="${password}"
> command="touch myfile" trust="true"/>
>
> <sshexec host="${server}" username="${userid}" password="${password}"
> command="rm myfile" trust="true"/>
>
> <sshexec host="${server}" username="${userid}" password="${password}"
> command="sh hello.bash" trust="true"/>
>
> </target>
>
> </project>
>
> -----------------------------------------------------------------------
>
>
>
> The basic problem is that my scp step doesn't seem to see the values of
> ${server} and ${server.resume.dir} anywhere but in the steps that set
> them, 'tonge' or 'sympatico', depending on which one was invoked. If
> these two properties were global to the whole script rather than local
> to the 'tonge' or 'sympatico' tasks, my script should work.
> Unfortunately, I haven't been able to find any way to make the property
> global to the whole script.
>
>
>
> I also seem to have some problems with the dependencies between the
> tasks. To my way of thinking:
>
> - 'setparms' should depend on 'getserver';
>
> - 'sympatico' and 'tonge' should both depend on 'setparms' (although
> this may be redundant due to the antcalls in 'setparms')
>
> - 'getlogin' should be dependent on either 'sympatico' or 'tonge',
> whichever was executed, or possibly both
>
> - 'display' should be dependent on 'getlogin'
>
> - 'scp' should be dependent on 'display'
>
> - 'sshexec' should be dependent on 'scp'
>
>
>
> However, if I try to implement those dependencies, the script tends to
> have a lot more problems. Perhaps someone could tell me how the
> dependencies should be set for what I am trying to do.
>
>
>
> I'm using Ant 1.6.1 on Windows XP but would be willing to upgrade to a
> newer version of Ant if that would help.
>
Thanks to Vadim and Dominique for their excellent suggestions!
>
>
> 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
>
>
> ---------------------------------------------------------------------
> 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