Location: JWare Software » AntXtras » Documents » Function Shortcuts » $sysenv:
$sysenv:
The $sysenv: function shortcut lets you read the value of a System environment variable without loading the entire environment into the Ant property space. Reading an environment variable becomes as easy as reading an Ant property. You can also use this funcut to supply a default value if the named variable is undefined. The $sysenv: funcut is a query only feature– you cannot use them to modify existing data.
The $system: funcut complements $sysenv: and lets you read the value of a System property and define a default value if that property is missing or malformed. Note that using this funcut is very different from using a standard property deference like “${system-property-name}.” The $system: funcut looks in the System properties only and ignores all Ant installed overrides for properties of the same name. The $system: funcut is a query only feature– you cannot use them to modify existing data.
The $sysenv: and $system: funcuts are automatically installed and enabled by the standard AntXtras funcuts antlib. You can also explicitly install these funcuts’ handlers; read the Examples section to see how.
Parameters
The general form of the function shortcut is: $sysenv:name[?default-value] where name is the name or key of the environment variable or system property being read, and default-value is the value that will be returned if the named variable is not defined for environment.
Examples
Installing $sysenv: and $system: shortcuts
The following snippet explicitly declares the $sysenv: and $system: function shortcuts. If you choose to activate shortcuts manually, you’ll need to do something like this at the start of your Ant script’s execution.
1: <oja:managefuncuts action="enable"> 2: <parameter name="sysenv" 3: value="${ojaf}.info.SysenvFunctionShortcut"/> 4: <parameter name="system" 5: value="${ojaf}.info.SystemFunctionShortcut"/> 6: </oja:managefuncuts>
Using environment variable as fallback for System property of same name
The following snippet uses the $sysenv: funcut to initialize an Ant property from an environment variable value if that property has not already been defined (either from direct assignment or inherited from the JRE’s system properties).
1: <fallback property="AKIMABU_HOMES" value="${$sysenv:AKIMABU_HOMES}"/>
Using environment variable with default if undefined
The following snippet will set the property “userid” to the value of the environment variable “USERNAME” or, if no such variable exists, to the default value “public”. Note that the behavior of this snippet is significantly different from initializing “userid” from another property called “USERNAME”. As of Ant 1.8 there is no standard way to redefine an inherited environment variable; whereas you can easily do this for a property.
1: <property name="userid" value="${$sysenv:USERNAME?public}/>
Related Topics
- The latest SysenvFunctionShortcut javadocs with more examples.
- The latest SystemFunctionShortcut javadocs with more examples.
- The $property: funcut lets you read a system or project property.