Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If-statement with Ant

 
Atanas Todorov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18014
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic