Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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

 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic