You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by laredotornado <la...@gmail.com> on 2010/10/14 21:00:39 UTC

Way to not hard-code these values?

Hi,

I'm using Ant 1.7.  I'm trying to de-hardcode a few things from my script,
and would prefer to have them properties-file driven.  However, I'm not sure
how to go about it.    I have

        <SeleniumHTMLClient results="${results}" appendResults="true"
verbose="false" showPassedTests="false" excludeHeaderAndFooter=
"true" multithread="true">

                <Host value="IP-address1" port="1234" />
                <Host value="44.33.2.99" port="5678" />

I would like to move the hosts and ports into a properties file and then
load them into this task dynamically.  Any ideas how to do this?

Thanks, - Dave
-- 
View this message in context: http://ant.1045680.n5.nabble.com/Way-to-not-hard-code-these-values-tp3212639p3212639.html
Sent from the Ant - Users mailing list archive at Nabble.com.

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


Re: Way to not hard-code these values?

Posted by reno <re...@free.fr>.
http://lmgtfy.com/?q=ant+properties+file+load
> Hi,
>
> I'm using Ant 1.7.  I'm trying to de-hardcode a few things from my script,
> and would prefer to have them properties-file driven.  However, I'm not sure
> how to go about it.    I have
>
>         <SeleniumHTMLClient results="${results}" appendResults="true"
> verbose="false" showPassedTests="false" excludeHeaderAndFooter=
> "true" multithread="true">
>
>                 <Host value="IP-address1" port="1234" />
>                 <Host value="44.33.2.99" port="5678" />
>
> I would like to move the hosts and ports into a properties file and then
> load them into this task dynamically.  Any ideas how to do this?
>
> Thanks, - Dave
>   



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


Re: Way to not hard-code these values?

Posted by David Weintraub <qa...@gmail.com>.
There are several ways to do just this.

1. Use the -D option to define the properties you want.
2. Use the -propertyfile flag to specify a property file
3. Use the <properties file=""> field to specify which properties are
stored in a particular file.

If possible, you can define default values for the properties you
want, and then over ride them via these other methods. For example, if
you use #1 the values specified will override the default values in
your build.xml file, you have the following:

<property name="host" value="1.2.3.4"/>
<property name="port" value="1234"/>
[...]
<host value="${host}" port="${port}"/>

And you put this on the command line:

$ ant -Dhost=5.6.7.8 -Dport=5678

host will be set to "5.6.7.8" and not "1.2.3.4". And, port will be set
to 5678 and not 1234.

You can put a bunch of files into a properties file in the form of:

host=5.6.7.8
port=5678

and that too will override the properties you've set with <properties>
statements.

I prefer to do something like this:

<property name="properties.file" value="${basedir}/build.properties"/>
<property file="${properties.file}"/>

This allows someone to use a default properties file or to override
the default one on the command line via the -D option.

My preference is to use default values in the build.xml and allow the
user to override them on the command line. This meshes with my
philosophy that a developer just typing "ant" on the command line will
do more or less what they expect.

If you don't specify default values, you should use <fail> tasks to
fail the build when particular values are not set by the use via the
command line:

<fail unless="host"  message="You must set the property &quot;host&quot;"/>

-- 
David Weintraub
qazwart@gmail.com

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