windows 蚂蚁机器名属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1196989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:53:13  来源:igfitidea点击:

ant machine name property

windowslinuxant

提问by Drejc

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

对于 Linux 和 Windows 操作系统,有没有办法将机器名称作为 ant 属性获取。

回答by Rebse

<exec executable="hostname" outputproperty="computer.hostname"/>

will work on linux and windows

将在 linux 和 windows 上工作

回答by mikej

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:

在 Windows 上,主机名在环境变量“COMPUTERNAME”中,在 Linux 上,环境变量是“HOSTNAME”。因为 ant 属性是不可变的,所以应该可以使用以下内容:

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

即,将环境作为以 env 为前缀的属性导入。然后将 env.HOSTNAME 设置为 env.COMPUTERNAME 的值,除非 env.HOSTNAME 已经设置,在这种情况下,第二行将不起作用。之后,在需要主机名的地方使用 env.HOSTNAME。

回答by Adam Batkin

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

查找本地机器主机名的正确方法是使用 Ant 的HostInfo任务。这将适用于所有平台,并且由 Ant 本地支持。

<hostinfo prefix="host." />
<echo message="My hostname is '${host.NAME}'" />

回答by stevedbrown

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

将 Unix 的值复制到 Windows 版本中。然后你可以使用 ${env.COMPUTERNAME}。

<property name="env.COMPUTERNAME" value="${env.HOSTNAME}"/>

回答by Michael Aaron Safyan

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

为此,您可以使用环境变量 $HOSTNAME (UNIX) 和 %COMPUTERNAME% (Windows)。您可以检查环境变量 HOSTNAME 是否已定义,如果没有,则可以使用环境变量 COMPUTERNAME(假设它已定义)。作为后备,您可以使用“未知”。

回答by Mikael Vandmo

I would write a custom Ant task.

我会编写一个自定义的 Ant 任务。

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.

一旦您开始编写自己的任务,您会发现 Ant 变得更加有趣和易于使用。您不需要难以理解的奇怪解决方案。

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

编写任务后,您只需执行以下操作:

<my:hostname property"hostname" />