How to get fully qualified hostname using Ant

2019-06-27 06:55发布

I am using Ant as our setup script for our server, and we need to get the fully qualified name of our server. How can I get it using Ant, or is is possible in Ant?

The fully qualified hostname is like: xxx.company.com

标签: ant
6条回答
何必那么认真
2楼-- · 2019-06-27 07:19

There is an Ant task called HostInfo that you can use to set properties containing the host name and domain of the current machine.

Alternatively, if you're running on Linux/Unix you could just call out to the hostname command:

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

The fully-qualified host name is then available in ${myhostname}.

EDIT: For a fully platform-independent solution, a custom task something like this (untested) should do the job:

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);
            }
        }
    }
}

This can be used as follows:

<GetHost property="myhostname" />
查看更多
对你真心纯属浪费
3楼-- · 2019-06-27 07:24

Another approach is to write a javascript scriptdef that sets this information in properties.

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

You can call it like this:

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

Here is the result:

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

This is based on some advice I found here: http://grokbase.com/t/ant/user/051sygfj9b/any-way-to-get-at-the-machine-name

查看更多
啃猪蹄的小仙女
4楼-- · 2019-06-27 07:29
<exec executable="hostname" outputproperty="computer.hostname"/>

will work on linux and windows, otherwise use the groovy solution from Marc O'Connor
Also nslookup will work on linux and windows, if you need the fullhostname you have to parse for the entry after Name: in nslookup ServerName output, use :

<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>
查看更多
男人必须洒脱
5楼-- · 2019-06-27 07:35

use enviroment

<property name="hostname" value="${env.COMPUTERNAME}"/>
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-06-27 07:38

Use javascript which is built-in

<script language="javascript">//<![CDATA[
    project.setProperty("hostname", Packages.java.net.InetAddress.getLocalHost().getHostName());
    project.setProperty("hostname-full", Packages.java.net.InetAddress.getLocalHost().getCanonicalHostName());
//]]></script>
<echo message="hostname=${hostname}"/>
<echo message="hostname-full=${hostname-full}"/>
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-06-27 07:43

Rehashing an answer I did for Maven :-)

Use an embedded groovy script to perform the Java hostname lookup:

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

Example

Project is self-documenting:

$ 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>
查看更多
登录 后发表回答