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
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" />
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>
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: ?????
<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>
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
use enviroment
<property name="hostname" value="${env.COMPUTERNAME}"/>
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}"/>