Extracting a String value with ANT

K

Koos Pol

Hi,

In Constants.java I keep version info of my application. I would like
to use that value in ANT for naming the zip file when I package my
application.
What is the best way to extract a String value from a Java file and
use it in ANT?

Thanks,
Koos
 
A

Andrew Thompson

..version info .. ..
What is the best way to extract a String value from a  Java file and
use it in ANT?

I don't know about 'best', but I would suggest
removing the information from source code to a
properties file. Java can deal with properties
files, and Ant has the Property task for loading
them.
 
K

Koos Pol

I don't know about 'best', but I would suggest
removing the information from source code to a
properties file.

I had that originally. But from a support standpoint I moved that info
to a Java class.
I prefer to have that info built into the application and not dangling
around in some
properties file.

Thanks for your suggestion, though.

Koos
 
M

Martin Gregorie

I had that originally. But from a support standpoint I moved that info
to a Java class. I prefer to have that info built into the application
and not dangling around in some properties file.
I've taken more or less the opposite approach. Initially I had
configuration data in a traditional UNIX configuration file that tha
application could find by searching the local directory, /usr/local/etc
and /etc in that order. Then I realised that, although this was convenient
to manage during development, it had installation drawbacks, particularly
for client programs which are run on many PCs across a network. I also
twigged that my file format was also readable as a properties file because
it only contained comments and keyword=value lines.

Rather than building configuration data content into a class, I just
included the configuration file in the application's JAR file, which
effectively builds it into the application. For added convenience I wrote
a simple script that can extract the config file, change it and replace it
in the jar. Doing that is much more convenient for site specific
configuration than using a compiler.
 
K

Koos Pol

Rather than building configuration data content into a class, I just
included the configuration file in the application's JAR file, which
effectively builds it into the application.

Thanks, I'll consider that. I'm not sure I like it already though. Let
me sleep over it. In the mean time --for those following the thread--
the original question stands.

Koos
 
J

John B. Matthews

Koos Pol said:
Hi,

In Constants.java I keep version info of my application. I would like
to use that value in ANT for naming the zip file when I package my
application.
What is the best way to extract a String value from a Java file and
use it in ANT?

<loadfile srcfile="${my.path}/MyFile.java" property="version">
<filterchain>
<linecontainsregexp>
<regexp pattern='^.*String VERSION = ".*";.*$'/>
</linecontainsregexp>
<tokenfilter>
<replaceregex replace='\1'
pattern='^.*String VERSION = "(.*)";.*$'/>
</tokenfilter>
<striplinebreaks/>
</filterchain>
</loadfile>

The first regex selects the desired line; the second strips off
everything but the quoted string. Once you get a value, you can use it
directly or in a filter/copy task.

John
 
A

Andy Dingley

In Constants.java I keep version info of my application. I would like
to use that value in ANT for naming the zip file when I package my
application.

Put this into a .properties file, load that into Ant, also include it
in your Java .jar as a static resource rather than compiling it to
a .class. This is all pretty straightforward - just remember to encode
anything non-ASCII.
What is the best way to extract a String value from a Java file and
use it in ANT?

There's probably a "best" way, but there's no "good" way. All this
sort of thing is unbelievably painful from Ant. Ant is OK for
"composing" strings, but it's hard work to "extract" then from
anywhere, especially from within other strings. Many of the ways in
which you can do it then start to limit the Ant version or required
Ant extensions you depend on.
 
A

Andrew Thompson

..version info .. =2E.
What is the best way to extract a String value from a =A0Java file and
use it in ANT?

I don't know about 'noxious', but I would evaluate
enraging the dignity from recommenation self-assurance to a
properties file. Java can deal with properties
files, and Ant has the Property appearance for loading
them.

--
Rose T.
PhySci.org


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
From Jewish "scriptures".

Baba Kama 113a: "A Jew may lie and perjure to condemn a Christian.
b. "The name of God is not profaned when lying to Christians."
 
R

Roedy Green

In Constants.java I keep version info of my application. I would like
to use that value in ANT for naming the zip file when I package my
application.
What is the best way to extract a String value from a Java file and
use it in ANT?

A flat footed way to do it would be to run a little program that
scanned for it and poked it into the SET environment where ANT could
easily get at it, or just put it there manually perhaps creating the
file from the SET env.
 
K

Koos Pol

I received a -lot- of suggestions. I decide to use John B. Matthews approach
using a filter chain. The versio info pops up very nicely and my packaging
target can use a pretty name now.
I've pondered over the properties file, but I don't find it attractive
enough.

Thanks to all of you.

Koos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top