Ant How to get an environment variable

Syntax:

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

The environment variables can be accessed in Ant by creating a property for the environment and assigning it a name. The variables can then be accessed using the property name.

Example:

This example shows the content of the tmp environment variable in the output. First, the environment is loaded in the env property. Then the tmp variable is accessed using ${env.TMP}. After the value is written in the output.

<project name="MyProject" default="test" basedir=".">

  <target name="test">
    <!-- Load the environment properties -->
    <property environment="env"/>
    
    <!-- Show the TMP in the output -->
    <echo message="TMP = ${env.TMP}"/>
  </target>
  
</project>

The output will be:

V:\tmp\ant>ant
Buildfile: build.xml

test:
     [echo] TMP = C:\Users\Olivier\AppData\Local\Temp

BUILD SUCCESSFUL
Total time: 0 seconds


References:

Apache Ant

Ant property

Share this article:

Recent Comments