• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If-statement with Ant

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need something like the JSTL declarative-if-statement in ant, but I couldn't find such a task.

I have many build scripts in the project, where I want to do some stuff (like delete files, create directories) only if a global condition is true (for instance the existence of a global property). These blocks are many and too small to be appropriate extracting them in separate targets. I need something like this:

<if test="some test condition here">
<delete dir="${build.classes}" />
</if>
...
<if test="same test condition here">
<mkdir dir="${build.lib}" />
</if>

Nothing much, huh?

I found some third-party "if"-like tasks, but I'd prefer to use native ant tasks.

Why such a useful task is not presented in ant?

Could anybody give me some idea how to implement such a functionality in my build scripts?
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although various extended methods (custom tasks, javascript, etc) are sometimes used, the more common way is probably just to make an entire task conditional.

For example:


I shortened this example, since what it does isn't important here. What is, is that the target only fires if the property "build.on.linux" was set.

That property is set in my "init" target, which everything else depends on:


The Ant paradigm is to do most of its conditionals based on the definition or lack of definition of a property. It can be a little awkward at times, but it works.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Atanas,
Welcome to JavaRanch!

The "if" statement and "for" loop aren't in the core of Ant for ideological reasons. According to the "Ant in Anger" article:

Ant-contrib - This sourceforge project contains helper tasks that are kept separate from core Ant for ideological purity; the foreach and trycatch tasks in particular. These give you iteration and extra error handling. Also on the site is the <cc> task suite, that compile and link native code on a variety of platforms.



Ant-contrib has an if statement and is as close to core Ant as you can get without actually being core Ant.
 
reply
    Bookmark Topic Watch Topic
  • New Topic