aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Conditional Logic Based on the Presence of a Passed Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Conditional Logic Based on the Presence of a Passed Variable" Watch "Conditional Logic Based on the Presence of a Passed Variable" New topic
Author

Conditional Logic Based on the Presence of a Passed Variable

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
As an ANT neophyte I have no idea even where to look for this. I have this working (I got lucky and lots of help here) Ant file:

It is the 'incserial' target that I am interested in modifying. If ${build.number} is set from the command line I want to use it and not update the resources file. If is it not set then I want to run as is. How do I modify my ant to make that work? TIA.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Read the docs on the 'if' and 'unless' properties for <target>: http://ant.apache.org/manual/targets.html#targets
I think you want something like this:



JBoss In Action
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 459
    
    1
Check this post. It contains working example.


Vinod Tiwari | Twitter
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the replies and my apologies for not being sufficiently clear. I need to execute 'incserial' regardless. The issue is to not execute the copy and buildnumber parts if ${build.number} is set.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Then you split the incserial target into separate targets, some of which are always performed, others of which depend on the 'if' or 'unless' properties. For example:



There are other variations possible also, such as not having incserial2 at all - instead leave those three tasks in incserial.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the suggestion but I came up with a slightly different solution. What I did not realize until recently was that the only thing I needed to do was not execute the <buildnumber /> task. I then figured out that <if><then><else> would work there (other places I tried it resulted in an unsupported nesting error).
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

The problem with using the <if> task is that it is not part of the Ant distribution, but is instead a part of Ant-contrib (which has not been updated since 2003!). I dislike relying on such old, no longer active, projects when there are ways of accomplishing what I want using standard Ant tasks (which is why I use the mechanism I identified earlier).
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I was not aware that ant-contrib was effectively deprecated. I will probably switch to your suggestion. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conditional Logic Based on the Presence of a Passed Variable