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
<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>
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With