ant machine name property

后端 未结 6 864
轮回少年
轮回少年 2020-12-30 22:31

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

相关标签:
6条回答
  • 2020-12-30 22:59

    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".

    0 讨论(0)
  • 2020-12-30 23:00

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

    <property name="env.COMPUTERNAME" value="${env.HOSTNAME}"/>
    
    0 讨论(0)
  • 2020-12-30 23:10

    I would write a custom Ant task.

    Once you start writing you own tasks you will find that Ant gets a lot more fun and easy to use. You don't need strange solutions that are hard to understand.

    Once the task is written you would just do something like:

    <my:hostname property"hostname" />
    
    0 讨论(0)
  • 2020-12-30 23:12

    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.

    0 讨论(0)
  • 2020-12-30 23:13

    The correct way to find the local machine's hostname is by using Ant's HostInfo task. This will work across all platforms and is natively supported by Ant.

    <hostinfo prefix="host." />
    <echo message="My hostname is '${host.NAME}'" />
    
    0 讨论(0)
  • 2020-12-30 23:26
    <exec executable="hostname" outputproperty="computer.hostname"/>
    

    will work on linux and windows

    0 讨论(0)
提交回复
热议问题