The moose likes Ant, Maven and Other Build Tools and the fly likes If-statement with Ant 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 "If-statement with Ant" Watch "If-statement with Ant" New topic

If-statement with Ant

Atanas Todorov

Joined: Nov 29, 2005
Posts: 3
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 test="same test condition here">
<mkdir dir="${build.lib}" />

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

Joined: Jun 25, 2001
Posts: 17417

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.

An IDE is no substitute for an Intelligent Developer.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33111

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: If-statement with Ant
It's not a secret anymore!