3

Using Environment Properties In Ant

Posted April 24th, 2009. Filed under Code

Tinking around with Ant I found out you can reference system environment properties within your build file.

So in Windows for example you can run SET at the command line and get a long list of properties:


U:\>set
ALLUSERSPROFILE=C:\Documents and Settings\All Users
ANT_HOME=D:\ant
APPDATA=C:\Documents and Settings\priestj\Application Data
CLIENTNAME=zappa

You can easily reference these in your build file – first set property environment to “env”:


<target name="echo">
<property environment="env"/>

</target>

Now we’ll add some of our Windows system properties (note these are case sensitive!):


<target name="echo">
<property environment="env"/>
<echo message="env.ALLUSERSPROFILE:   ${env.ALLUSERSPROFILE}" />
<echo message="env.ANT_HOME:   ${env.ANT_HOME}" />
<echo message="env.APPDATA:   ${env.APPDATA}" />
<echo message="env.CLIENTNAME:   ${env.CLIENTNAME}" /> </target>

Running this we can see these properties are picked up by Ant!


Buildfile: D:\build.xml
echo:
[echo] env.ALLUSERSPROFILE:   C:\Documents and Settings\All Users
[echo] env.ANT_HOME:   C:\eclipse\plugins\org.apache.ant_1.7.0.v200803061910
[echo] env.APPDATA:   C:\Documents and Settings\priestj\Application Data
[echo] env.CLIENTNAME:   zappa

Possibly Related:

If you have enjoyed this entry. Please feel free to bookmark it using your favorite social bookmarking site

3 Responses so far

  1. Sweet! Hadn’t thought of attaching to the environment variables, that could be very handy.

  2. marc esher says:

    Excellent. Very useful, Jim

  3. kumar kauhsal says:

    Superb… Its really very useful…Never thought of these features..
    Thanks

Leave a Comment