Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Extracting a String value with ANT

Reply
Thread Tools

Extracting a String value with ANT

 
 
Koos Pol
Guest
Posts: n/a
 
      05-27-2008
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      05-27-2008
On May 27, 7:03*pm, Koos Pol <(E-Mail Removed)> wrote:
...
> ..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.

--
Andrew T.
PhySci.org
 
Reply With Quote
 
 
 
 
Koos Pol
Guest
Posts: n/a
 
      05-27-2008
On 27 mei, 11:27, Andrew Thompson <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      05-27-2008
On Tue, 27 May 2008 02:33:52 -0700, Koos Pol wrote:

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


--
martin@ | Martin Gregorie
gregorie. |
org | Zappa fan & glider pilot
 
Reply With Quote
 
Koos Pol
Guest
Posts: n/a
 
      05-27-2008
On 27 mei, 12:12, Martin Gregorie <martin@see_sig_for_address.invalid>
wrote:
> 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
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      05-27-2008
In article
<(E-Mail Removed)>,
Koos Pol <(E-Mail Removed)> wrote:

> 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
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
 
Reply With Quote
 
Koos Pol
Guest
Posts: n/a
 
      05-27-2008
Spot on. Very nice. Thank you John!

Koos
 
Reply With Quote
 
Andy Dingley
Guest
Posts: n/a
 
      05-27-2008
On 27 May, 10:03, Koos Pol <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-27-2008
On May 27, 7:03=A0pm, Koos Pol <(E-Mail Removed)> wrote:
=2E..
> ..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."

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-27-2008
On Tue, 27 May 2008 02:03:37 -0700 (PDT), Koos Pol
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

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

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run ant script from ant script? lionelv@gmail.com Java 6 07-26-2007 02:12 AM
error runing ant in eclipse ant view yihucd@gmail.com Java 1 03-07-2006 08:39 PM
question about extracting value from a string nephish@xit.net Python 4 12-07-2005 05:16 PM
trouble with Hibernate and Ant (probably just Ant) john martin Java 2 04-07-2005 09:27 PM
wondering how i can launch tomcat from ant without ant hanging... Chris Bedford Java 6 12-23-2004 12:41 AM



Advertisments