ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Adam Murdoch" <adammurd...@yahoo.com>
Subject RE: Access environment variables
Date Thu, 19 Oct 2000 02:23:33 GMT


> -----Original Message-----
> From: Bill Burton [mailto:billb@progress.com]
> Sent: Thursday, 19 October 2000 8:51 AM
> To: ant-user@jakarta.apache.org
> Subject: Re: Access environment variables
>
>
> Hello,
>
> Nils Olav SelÄsdal wrote:
> >
> > > -----Original Message-----
> > > From: Stefan Bodewig [mailto:bodewig@bost.de]
> > > Sent: 17. oktober 2000 15:57
> > > To: ant-user@jakarta.apache.org
> > > Subject: Re: Access environment variables
> > >
> > >
> > > >>>>> "PB" == Peter Breitling
> > > <breitlip@informatik.tu-muenchen.de> writes:
> > >
> > >  PB> Hum, i feared that. But then i need a wrapper script and this is
> > >  PB> not elegant. Is there a reason for this design?
> > >
> > > It's not Ant's design, Java doesn't provide access to environment
> > > variables.
> > >
> > > Stefan
> >
> > Hmm .. linux have the 'env' command, suppose you could create a
> script that
> > makes
> > lots of -Dvarable=$variable parameters to ant here...
>
> Maybe like this?
>
> # get environment variables except for PATH, CLASSPATH and MANPATH,
> # quoting the values and adding the prefix "-D" to each one
> ENVDEFS=`env |egrep -v '^PATH|^CLASSPATH|^MANPATH' |
>          sed -e 's/=\(.*\)$/="\1"/' -e 's/^/-D/'`
> java $ENVDEFS ...
>
> I'm new to Ant but here's my attempt at a cross-platform way of loading
> environment variables for Ant 1.1:
>
>        <target name="loadenv">
>          <!-- Write out environment variables to a properties file -->
>          <property name="env.file" value="environment.properties" />
>          <exec command="env" os="UNIX" output="${env.file}" />
>          <exec command="cmd /c set" os="Windows_NT" output="${env.file}"
> />
>          <exec command="command /c set" os="Windows" output="${env.file}"
> />
>          <!-- Now, load the environment variables as properties -->
>          <property file="${env.file}" />
>          <delete file="${env.file}" verbose="false" />
>        </target>
>
>        <target name="prepare" depends="loadenv">
>          ...
>        </target>
>
> Note that the os attributes above are probably wrong.
>
> I was trying to configure a target that would cause the build to fail if
> the environment variables failed to load i.e. ${ANT_HOME} not available
> but couldn't figure out how to do it.  Using a target like:
>        <target name="prepare" depends="loadenv" if="${ANT_HOME}"
>          ...
>        </target>
> would not execute but the compile target that depended on that target
> still ran.  So how can I cause the build to stop on some condition?
>

You could try something like:

<target name="testenv" depends="loadenv" unless="ANT_HOME">
	<fail message="..."/>
</target>

This will attempt to load the environment vars using the "loadenv" target,
then will fail (ie stop the build) if ANT_HOME is not set.

One thing that takes a little getting used to is that 'if' and 'unless'
attributes apply only to the target itself.  The dependencies of the target
are always run, and the if and unless conditions are only evaluated after
the dependencies have finished.  This is what you were seeing with your
prepare and compile targets.


Adam


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Mime
View raw message