ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rhino" <rhi...@sympatico.ca>
Subject Re: Reading and Writing of String
Date Sun, 01 Aug 2004 18:02:30 GMT
I'm not sure if editing source files with build scripts is the best way to
handle this situation. It would probably be better for the Java program to
read the build-date value from the properties file itself. But you may have
a compelling reason to edit the source file; that's up to you.

I had never tried to do what you want to do but, based on the Ant manual, I
guessed that the 'loadproperties' and 'replace' tasks should do what you
want. I put together a simple script to make sure I understood the manual
correctly and found that it worked fine. My files and the script were as
follows:

ui.properties
--------------
prop1=foo

build-date=AmarGeet_0098_20040812

prop3=bar


pritesh.java
-------------

String version_Date="[xxx]";


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

<project name="Pritesh" default="Edit" basedir=".">

<description>Initialize a variable in a source file from a property in a
property file.

</description>

<target name="Read" description="Read build_date">

<loadproperties srcFile="ui.properties">

<filterchain>

<linecontains>

<contains value="build-date"/>

</linecontains>

</filterchain>

</loadproperties>

<echo message="The value of build-date is ${build-date}"/>

</target>

<target name="Edit" depends="Read" description="Initialize version_date">

<replace file="pritesh.java" token="[xxx]" value="${build-date}"/>

</target>

</project>

The "Read" target reads the ui.properties file via the loadproperties task
and finds the line containing the 'build-date' property. The value of the
build-date property in the ui.properties file is then used to initialize an
*Ant* property that is also named 'build-date' with the same value. The echo
task is used to verify that the *Ant* property 'build-date' is equal to
"AmarGeet_0098_20040812".

The "Edit" target uses the replace task to replace '[xxx]' in the source
file with "AmarGeet_0098_20040812". I checked the source file after
completion of the Ant script and it was correctly edited.

IMPORTANT NOTE:
I modified the source code slightly to make the solution to the problem
simpler. Specifically, I changed this:

String version_date="";

to

String version_date="[xxx]";

Since it is quite possible that you have initialized several different
variables in your program to "", I decided to put something distinctive in
the initialization for version_date. That is to ensure that you don't
initialize any value other than 'version_date' with your build-date.
Remember, the replace task will normally replace *ALL* occurrences of the
"token" with "value".

 It doesn't really matter what you use to initialize version_date in your
source file but I've found that it's usually less trouble if you avoid
imbedded blanks, quotes, or apostrophes. Naturally, you also have to set the
token parameter in the "replace" task with the same initialization value
that appears in the source file.


Rhino

----- Original Message ----- 
From: "Pritesh saharey" <pritesh@sqatester.com>
To: <user@ant.apache.org>
Sent: Sunday, August 01, 2004 12:52 PM
Subject: Reading and Writing of String


> Hi All,
>        I need a help regarding reading and writing to a file. There is a
file ui.properties which looks something like this
>
>        build-date= AmarGeet_0098_20040812
>
>        now i want to read this value of variable 'build-date' and write it
to a 'String' variable in a java file
>
>        String version_Date="";
>
>        and this process has to be done before the compliation of java
files, is there any way in ant to do this process.
>
>
>        so what need to be done here can anybody help me out, I will be
thankful to you.
>
>
>
> Pritesh Saharey
> BE(Mech)
> <a href="http://www.ntsearch.com/search.php?q=Software&v=56">Software</a>
Engineer
>
>
> _____________________________________________________________
> Join SQAtester.com Community  --->
http://www.sqatester.com/testersarea/joinus.htm
>
> ---------------------------------------------------------------------
> 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