ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rhino" <>
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



String version_Date="[xxx]";

<?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.


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

<loadproperties srcFile="">



<contains value="build-date"/>




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


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

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



The "Read" target reads the file via the loadproperties task
and finds the line containing the 'build-date' property. The value of the
build-date property in the 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

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.

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

String version_date="";


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.


----- Original Message ----- 
From: "Pritesh saharey" <>
To: <>
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 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="">Software</a>
> _____________________________________________________________
> Join Community  --->
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message