Apache Ant: How To Set Property When Initially Unset

By Angsuman Chakraborty, Gaea News Network
Tuesday, August 19, 2008

Also: How To Flexibly Pass JVM parameters to Apache Ant Java (Runtime) Task

Apache Ant is an excellent Java based build & deployment system using XML configuration file. In Ant properties are extensively used to configure tasks, conditionally run targets and more. In short they are an intergral part of build system. My requirement was simple. I wanted to pass JVM arguments to the Java runtime. However sometime I wanted to invoke it without passing any special runtime arguments. Unfortunately jvmarg element doesn’t like it when its value is an empty string or even a string with spaces. Finding no way to fool it, I then tried to find a way to set the property to something when it is initially not set (while invoking). This proved surprisingly hard to do. Here is an elegant (I think) solution I came up with.

[condition property=”jargs” value=”${jvmargs}” else=”-Djvmarg=false”>
<isset property=”jvmargs”/>
[/condition>

Note: Replace all [ with < as buggy WordPress WYSIWYDNG (what you see is what you do not get) editor doesn’t allow me to use < without following it with a space immediately afterwards.

What this does is set a property called jargs to the value of jvmargs if it is set. Otherwise it is set to a dummy value (eat that jvmarg!).

jvmarg now hums along fine with or without any jvm arguments.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :