Drejc Drejc - 3 months ago 24
Linux Question

ant machine name property

Is there a way to get the machine name as ant property, for both Linux and Windows OS.

Answer

On Windows the hostname is in the environment variable "COMPUTERNAME", on Linux the environment variable is "HOSTNAME". Because ant properties are immutable something like the following should work:

<property environment="env"/>
<property name="env.HOSTNAME" value="${env.COMPUTERNAME}"/>
<echo message="hostname = ${env.HOSTNAME}"/>

i.e. import the environment as properties prefixed with env. Then set env.HOSTNAME to be the value of env.COMPUTERNAME unless env.HOSTNAME is already set in which case the 2nd line will have no effect. After that use env.HOSTNAME where the hostname is required.