ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rhino" <rhi...@sympatico.ca>
Subject Re: Qustion about property prefix...
Date Tue, 02 May 2006 20:56:52 GMT
I'm not sure about the answer to your original question but I might be able 
to suggest a different way to accomplish what you are trying to do. Someone 
on this mailing list, probably Jan Materne, gave me the basic idea and it 
worked very well in my build script.

The purpose of my script is to upload a bunch of files to one of two 
different servers. First, I do all of the compiling, jarring, etc. that is 
necessary to create the files which need to be uploaded. Then, I have 
targets that: determine which server is to be destination of the upload; 
grab the appropriate properties file (there is a different properties file 
for each server); execute the appropriate upload target; and do the actual 
upload.

Here is a quick explanation of each of these targets, followed by the 
relevant part of the script:
1. 'getserver' - prompts the user for which server is to be the destination 
of the files. Whichever value is chosen, a property called 'servername' is 
set to the name of the server.  In my case, I wanted to allow the decision 
to be made at run-time but you could simply force the user to supply a 
servername parameter to the build script as a command-line argument.
2. 'getprops' - obtains the properties file for the server that was chosen 
in the 'getserver' target. There is one property file for each server which 
can be the desination of the uploads; in this case, the properties files are 
server.Sago.properties and server.Tone.properties. In your case, you 
probably wouldn't have the 'getserver' target at all but would simply 
execute 'getprops' and it would grab the appropriate properties file based 
on the value of the servername property that was set on the command line.
3. 'upload' - executes the appropriate upload target via an 'antcall'. The 
target name includes the server name so the antcall can execute either 
'upload-Sago' or 'upload-Tone' in this case.
4. 'upload-Sago' - does the uploads for the Sago server. Uses the properties 
obtained from the server.Sago.properties file.
5. 'upload-Tone' - does the uploads for the Tone server. Uses the properties 
obtained from the server.Tone.properties file.

Important note: Although this particular example only chooses between two 
servers, this solution can easily scale to any number of servers by 
modifying the script as follows:
a. put more server options in the 'getserver' target.
b. write a properties file for each server.
c. write a specific 'upload-xxx' target for each server.
Therefore, if you wanted to add a third server, Alpha, to the build script:
- add a third option to 'getservers' that sets 'servername' to "Alpha"
- write a properties file for Alpha called 'server.Alpha.properties'
- add a target called 'upload-Alpha' which does the necessary uploading 
tasks.

Here is the code, with most of the real work stripped from upload-Sage and 
upload-Tone to keep it (relatively) concise:

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

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. Sago 2. Tone"

validargs="1,2"

addproperty="server.choice"

defaultvalue="2"/>

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

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

</condition>

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

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

</condition>

</target>

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

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

<property 
file="${workspace311}\${resume.proj}\xml\server.${servername}.properties"/>

</target>

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

Execute the appropriate upload target, depending on which server

was chosen.

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

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

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

</target>

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

Upload to the Sago server.

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

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


<input message="Please supply the userid for the Sago server:" 
addproperty="userid" defaultvalue="fredg"/>

<input message="Please supply the password for the Sago server:" 
addproperty="password" defaultvalue="fredgpw"/>


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


<echoproperties prefix="server"/>

<echo message="Deleting all files (but not subdirectories) *beneath* the 
resume directory."/>

<ftp action="del" server="${server.hostname}" userid="${userid}" 
password="${password}"

remotedir="${server.resume.dir}"

description="Delete all files under resume directory at any level.">

<fileset>

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

</fileset>

</ftp>

<!--ETC. ETC.-->


</target>

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

Upload to the Tone server. The Tone server uses SSH so the 'scp'

task is used for each upload of a file or directory. The 'sshexec'

task is used to create directories.

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

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

<input message="Please supply the userid for the Tone server:" 
addproperty="userid" defaultvalue="franb"/>

<input message="Please supply the SSH passphrase for the Tone server:" 
addproperty="passphrase" defaultvalue="francbpw"/>

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

<echoproperties prefix="server"/>

<echo message="Deleting resume directory and all files and directories below 
it."/>

<sshexec host="${server.hostname}" username="${userid}" trust="true"

keyfile="${server.keyfile}" passphrase="${passphrase}"

command="rm -rf ${server.resume.dir}"/>

<!--ETC. ETC. -->

</target>



I hope this helps!

--
Rhino

----- Original Message ----- 
From: "Trent Ohannessian" <tohann@gmail.com>
To: <user@ant.apache.org>
Sent: Tuesday, May 02, 2006 4:02 PM
Subject: Qustion about property prefix...


Hello.  I'm trying to use the <property> task with a prefix.  Here is the
code I'm using:

------------------------------------------
    <property file="./webtest.properties" prefix="${env}" />
    <echo message="host: ${host}" />
------------------------------------------

And here is the properties file I'm trying to load:

------------------------------------------
localhost.host=localhost
qa.host=qatest.domain.com

localhost.port=8080
qa.port=80
------------------------------------------

When I use the prefix property option, none of the properties are read and
they are not usable in the Ant script.  What I'm trying to do is pass in a
property on the command line and then use the correct set of properties
based on server.  I can see the localhost getting passed into the Ant script
correctly, but it doesn't work even if I hardcode localhost into prefix="".
  Is this the right way to go about doing this?

Thanks!
Trent

--
Learn from the mistakes of others. You can't live long enough to make them
all yourself.



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


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.5.1/328 - Release Date: 01/05/2006



-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.5.1/328 - Release Date: 01/05/2006


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


Mime
View raw message