How to get the fully qualified host name using Ant

I use Ant as our installation script for our server, and we need to get the full name of our server. How can I get it with Ant, or is it possible in Ant?

Full hostname: xxx.company.com

+4
source share
4 answers
<exec executable="hostname" outputproperty="computer.hostname"/> 

will work on linux and windows, otherwise use the groovy solution from Marc O'Connor
In addition, nslookup will work with linux and windows, if you need fullhostname, you need to parse the entry after the 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> 
+7
source

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

Alternatively, if you are running Linux / Unix, you can simply invoke the hostname command:

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

A full hostname is available in ${myhostname} .

EDIT: For a solution completely independent of the platform, a user task like this (unchecked) should complete the task:

 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
source

Recalling the answer I made for Maven :-)

Use the built-in groovy script to search for a Java host name:

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

Example

The 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> 
+2
source

Another approach is to write a javascript scriptdef that sets this information in the 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 tips I found here: http://grokbase.com/t/ant/user/051sygfj9b/any-way-to-get-at-the-machine-name

+1
source

All Articles