File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ant, Maven and Other Build Tools and the fly likes How do I provide a default for an environment variable accessed via ${env.VAR_NAME} in build script? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How do I provide a default for an environment variable accessed via ${env.VAR_NAME} in build script?" Watch "How do I provide a default for an environment variable accessed via ${env.VAR_NAME} in build script?" New topic
Author

How do I provide a default for an environment variable accessed via ${env.VAR_NAME} in build script?

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
We're using Ant version 1.7.0 and I'm trying to get some build scripts to work cross-platform (Linux and Windows).

The script was referencing a hard-coded UNIX-style path, and I would like to allow the UNIX folks to continue to use the script (I don't want to break what is already working).

What I would like to do is set an environment variable on the Windows machine and have the build script use it if it exists, but fall-back to the original UNIX value if it does not.

I see that I can reference environment variable from within the Ant build script (using ${env.VAR_NAME}), but can't find a way to handle the case where the variable is not set.

What I want is something like this:



Of course, if/then/else statements can't be used in this case.

I tried to use a target, as in:


but this doesn't work.

Note that I don't want to override the "my.dir" property using -Dmy.dir=... on the command line, as we want to be able to just invoke Ant without having to specify all the command-line arguments.

Thanks,
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Well, with the help of a co-worker, I found a way to make this work, and it's not too painful:

ENV_VAR=D:/foo/bar ant, results in the value "D:/foo/bar" being used.
ant by itself results in "/my/default/lib" being used.

Thanks,
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Try removing the ${ and } from inside the if and unless clauses. Having those there causes Ant to first evaluate the property and then use the result in the test. Example:



JBoss In Action
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Using a condition was going to be my alternate suggestion. I was in the middle of coding an example when I realized the ${..} mistake and posted that. A mistake, which I have to admit, I have made numerous times.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Just for grins, here is what I came up with. This makes use of the fact that properties are immutable, hence the last attempt to set the property (line 6) is to the default value.

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Very cool, Peter. Of course, this will require a comment so future readers understand what was intended, but it will be less that what I was doing originally.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I provide a default for an environment variable accessed via ${env.VAR_NAME} in build script?