Extracting a String value with ANT

Discussion in 'Java' started by Koos Pol, May 27, 2008.

  1. Koos Pol

    Koos Pol Guest

    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
    Koos Pol, May 27, 2008
    #1
    1. Advertising

  2. On May 27, 7:03 pm, Koos Pol <> 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
    Andrew Thompson, May 27, 2008
    #2
    1. Advertising

  3. Koos Pol

    Koos Pol Guest

    On 27 mei, 11:27, Andrew Thompson <> 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
    Koos Pol, May 27, 2008
    #3
  4. 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
    Martin Gregorie, May 27, 2008
    #4
  5. Koos Pol

    Koos Pol Guest

    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
    Koos Pol, May 27, 2008
    #5
  6. In article
    <>,
    Koos Pol <> 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
    John B. Matthews, May 27, 2008
    #6
  7. Koos Pol

    Koos Pol Guest

    Spot on. Very nice. Thank you John!

    Koos
    Koos Pol, May 27, 2008
    #7
  8. Koos Pol

    Andy Dingley Guest

    On 27 May, 10:03, Koos Pol <> 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.
    Andy Dingley, May 27, 2008
    #8
  9. On May 27, 7:03=A0pm, Koos Pol <> 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."
    Andrew Thompson, May 27, 2008
    #9
  10. Koos Pol

    Roedy Green Guest

    On Tue, 27 May 2008 02:03:37 -0700 (PDT), Koos Pol
    <> 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
    Roedy Green, May 27, 2008
    #10
  11. Koos Pol

    Koos Pol Guest

    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
    --
    43rd Law of Computing: Anything that can go wr
    fortune: Segmentation violation -- Core dumped
    Koos Pol, May 28, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Bedford
    Replies:
    6
    Views:
    2,213
  2. john martin
    Replies:
    2
    Views:
    2,204
    john martin
    Apr 7, 2005
  3. Replies:
    1
    Views:
    487
    Larry
    Mar 7, 2006
  4. Replies:
    4
    Views:
    254
  5. Replies:
    6
    Views:
    861
Loading...

Share This Page