tags:

views:

922

answers:

4

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

+5  A: 

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.

mikej
+1  A: 

You can use the environment variables $HOSTNAME (UNIX) and %COMPUTERNAME% (Windows) for this. You can check to see if the environment variable HOSTNAME has been defined and, if not, you can then use the environment variable COMPUTERNAME, assuming it is defined. As a fallback, you can use "unknown".

Michael Aaron Safyan
+1  A: 

Copy the value for Unix into the Windows version. Then you can use ${env.COMPUTERNAME}.

<property name="env.COMPUTERNAME" value="${env.HOSTNAME}"/>
stevedbrown
+3  A: 
<exec executable="hostname" outputproperty="computer.hostname"/>

will work on linux and windows

Regards, Gilbert

Rebse
I think this is more reliable. Back when we were using COMPUTERNAME, on SOME machines the casing of the value did not match the casing of the name of the machine. 'hostname' has so far always matched.
fnCzar