Ant: hostnames made easy

Ant Hostname: sometimes, you just need to know the name of the computer that you're running the build on. If you can't get away with using 'localhost' (maybe you have host specific properties for each machine the developers work on), then you need to work it out. Most of the advice out there runs towards fetching the 'COMPUTERNAME' environment variable if you're running Ant on a windows system, or the 'HOSTNAME' environment variable if you're on a Unix system. If you take that route you can use some simple logic to make that one cross-platform.

You could also run the 'hostname' command and put the output in a property. I was never a fan of either to be honest: I don't like builds depending on the environment, for one.

So I set out to write some Java that would fetch the hostname and set a property with the system's hostname. It looks like this

    <taskdef classname="org.juliansimpson.HostName" name="hostname" classpathref="cp" />
    <hostname property="hostname" />
    <echo message="property $${hostname} = ${hostname}" />

It works very well. Please feel free to use it. Comments welcome.

Link (952kB Zip file, md5sum "ae82000aa2c4b87b966347cc5a36c200")
I just put the code on GitHub.

DevOps New Zealand