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:[EMAIL PROTECTED] 
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}:[EMAIL PROTECTED]:/home/rhino" trust="true"/>

<!--scp file="C:/dbasic.log"
todir="${userid}:[EMAIL PROTECTED]:/home/rhino" trust="false"
knownhosts="${knownhosts}"/-->

<scp file="${userid}:[EMAIL PROTECTED]:/etc/ssh/ssh_host_key.pub"
todir="d:/eclipse/workspace/resume/xml" trust="true"/>

<scp todir="${userid}:[EMAIL PROTECTED]:/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.

 

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

Reply via email to