How to use an environment var in a property file

Discussion in 'Java' started by coltrane, Mar 8, 2006.

  1. coltrane

    coltrane Guest

    Is it possible to use an environment variable in a property file?

    I would like to set a property such as
    where INSTALL_BASE is an env var.

    then I would like to use ${dir.base} with a build.xml.

    why do you ask would I want to do this???

    I inherited a group of ant build scripts with hard coded paths in the
    property files.
    I would like to change them so the paths can be changed without
    modifying the prop files.

    I am very new to ant so forgive me if this is a simple question. and
    yes I am reading the manual :)

    thanks for your help

    coltrane, Mar 8, 2006
    1. Advertisements

  2. coltrane

    Ashish Pagey Guest

    You can pull in environment variables as properties into ant by using
    the property tag:

    <property environment="env"/>
    <echo message="Installbase = ${env.INSTALL_BASE}"/>

    Ashish Pagey, Mar 8, 2006
    1. Advertisements

  3. coltrane

    coltrane Guest


    I cannot alter my ant files but I can alter my property files.What I
    would like to do is use environment variables in the properties file.

    This is a sample of what I have

    property file
    ant file
    now what I want is to use an environment variable in the property file
    and not touch the ant file

    property file
    ant file
    I know this seems like extra work but as I said I can't change my ant
    files and I still want to use environment files.

    coltrane, Mar 9, 2006
  4. You can override the values in the properties files by specifying them
    as system properties. E.g. (assuming UNIX-style shell):

    ant -Ddir.base=$INSTALL_BASE build

    Do not forget to use quotes when appropriate.

    Raymond DeCampo, Mar 12, 2006
  5. coltrane

    coltrane Guest

    great solution.

    coltrane, Mar 13, 2006
  6. coltrane


    Jan 9, 2020
    Likes Received:
    I am using the Base URL port in Environmental variable. How can i use this env variables in properties files?
    export PORT_NUMBER=**** This is Environmental variable.
    BaseUrl=http://localhost:(have to put the env variable) : how to get that env variables?
    sushma@, Jan 9, 2020
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.