如何使用Ant获取完全限定的主机名

时间:2013-02-01 19:38:09

标签: ant

我使用Ant作为服务器的安装脚本,我们需要获取服务器的完全限定名称。我如何使用Ant获取它,或者在Ant中可以获得它?

完全限定的主机名如下:xxx.company.com

5 个答案:

答案 0 :(得分:7)

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

将在linux和windows上运行,否则使用Marc O'Connor的groovy解决方案 nslookup也适用于linux和windows,如果你需要fullhostname,你必须在名称后解析条目:在nslookup ServerName输出中,使用:

<groovy>
properties.'hostname' = "hostname".execute().text
//or
properties.'hostname' = InetAddress.getLocalHost().getHostName()

properties.'hostnamefull' = "nslookup ${"hostname".execute().text}".execute().text.find(/Name:\s(.+)/).split(/:\s+/)[1]
</groovy>

<echo>
$${hostname} => ${hostname}
$${hostnamefull} => ${hostnamefull}
</echo>

答案 1 :(得分:3)

有一个名为HostInfo的Ant任务,您可以使用它来设置包含当前计算机的主机名和域的属性。

或者,如果你在Linux / Unix上运行,你可以调用hostname命令:

<exec executable="hostname" outputproperty="myhostname">
  <arg line="-f"/>
</exec>

然后${myhostname}中可以使用完全限定的主机名。

编辑:对于完全独立于平台的解决方案,这样的自定义任务(未经测试)应该可以完成这项工作:

public class GetHost extends Task
{
    private String property;

    public void setProperty(String property)
    {
        this.property = property;
    }

    @Override
    public void execute() throws BuildException
    {
        if (property == null || property.length() == 0)
        {
            throw new BuildException("Property name must be specified.");
        }
        else
        {
            try
            {
                String hostName = InetAddress.getLocalHost().getHostName();
                getProject().setProperty(property, hostName);
            }
            catch (IOException ex)
            {
                throw new BuildException(ex);
            }
        }
    }
}

可以使用如下:

<GetHost property="myhostname" />

答案 2 :(得分:2)

重新回答我为Maven所做的回答:-)

使用嵌入式groovy脚本执行Java主机名查找:

       <groovy>
            properties["hostname"] = InetAddress.getLocalHost().getHostName()
        </groovy>

实施例

项目是自我记录的:

$ ant -p
Buildfile: /home/mark/tmp/build.xml

    This is a demo project answering the followng stackoverflow question:

        https://stackoverflow.com/questions/14653733

    First install 3rd party dependencies

        ant bootstrap 

    Then run the build

        ant

    Expect the following output

        print-hostname:
            [echo] Hostname: ?????

的build.xml

<project name="demo" default="print-hostname">

    <description>
    This is a demo project answering the followng stackoverflow question:

        https://stackoverflow.com/questions/14653733

    First install 3rd party dependencies

        ant bootstrap 

    Then run the build

        ant

    Expect the following output

        print-hostname:
            [echo] Hostname: ?????

    </description>

    <target name="bootstrap" description="Install 3rd party dependencies">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0/groovy-all-2.1.0.jar"/>
    </target>

    <target name="print-hostname" description="Retrieve and print the hostname">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

        <groovy>
            properties["hostname"] = InetAddress.getLocalHost().getHostName()
        </groovy>

        <echo message="Hostname: ${hostname}"/>
    </target>

</project>

答案 3 :(得分:1)

另一种方法是编写一个javascript scriptdef ,在属性中设置此信息。

<scriptdef name="get-hostame" language="javascript">
    <attribute name="prefix" /> <![CDATA[
        importClass(java.net.InetAddress);
        address = InetAddress.getLocalHost();
        prefix = attributes.get("prefix");
        project.setNewProperty(prefix + ".hostname", address.getHostName());
        project.setNewProperty(prefix + ".fqdn", address.getCanonicalHostName());
        project.setNewProperty(prefix + ".address", address.getHostAddress());
    ]]>
</scriptdef>

您可以这样称呼它:

<get-hostame prefix="uwi.host" />

结果如下:

[echoproperties] uwi.host.address=10.666.666.666
[echoproperties] uwi.host.fqdn=myhost.stackoverflow.com
[echoproperties] uwi.host.hostname=myhos

这是基于我在这里找到的一些建议: http://grokbase.com/t/ant/user/051sygfj9b/any-way-to-get-at-the-machine-name

答案 4 :(得分:0)

使用环境

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