ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Conor MacNeill" <co...@ebinteractive.com.au>
Subject RE: Hostname and loops
Date Tue, 31 Oct 2000 23:37:55 GMT
Nicholas,

> From: nwc@tanya-hide.medimom.com [mailto:nwc@tanya-hide.medimom.com]On
> Behalf Of Nicholas Christopher
> Sent: Wednesday, 1 November 2000 10:12
> To: ant-user@jakarta.apache.org
> Subject: Re: Hostname and loops
>
>
> Stefan Bodewig wrote:
> >
> > >>>>> "NC" == Nicholas Christopher <nwc@flashcom.net> writes:
> >
> >  NC> I realize I can -DHOSTNAME=xxxx on ant's command line but is
> >  NC> there a way for to stuff InetAddress.getLocalHost().getHostName()
> >  NC> into a property?
> >
> > No, but writing a task to do so would be trivial.
> >
>
> Not trivial enough :-) Tasks seem easy but I couldn't immediately
> see how a task
> could set a property.
>

Try

public class Hostname extends Task {

    protected String propertyName;

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public void execute() throws BuildException {
        try {
            project.setProperty(propertyName,
                                InetAddress.getLocalHost().getHostName());
        } catch (Exception e) {
            throw new BuildException(e, location);
        }
    }
}

Use it like this

<target name="tryme">
    <taskdef name="hostname"
classname="org.apache.tools.ant.taskdefs.Hostname"/>
    <hostname propertyName="goofy"/>
    <echo message="Hostname is ${goofy}"/>
</target>

You'll may need to add more error checking (is propertyName set, for
example). I'll leave the details to you.
Conor


Mime
View raw message