Java – Unsetting Property in Ant and Removing Property in Ant using a condition

antjava

How do you unset a property in ant? so that is is completely removed as a property?

<condition property="proguard.config" value="proguard.cfg">
      <isset property="proguarded"/>
</condition>


 <condition property="proguard.config" value="">
      <not>
      <isset property="proguarded"/>
      </not>
</condition>

This would seem to work. However proguard runs even if there is such a property as proguard.config in existence. So how do I remove proguard.config as a property altogether conditionally? I know that if proguard sees that there is a proguard.config property at all in the .properties file it is going to run.

Best Answer

The Ant manual Property Task states:

Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables.

That being said, there are a couple of workarounds:

  1. Local Task - A local property at a given scope "shadows" properties of the same name at higher scopes (especially useful within Macrodefs)

  2. Ant-Contrib Variable Task - Ant-Contrib offers flexibility, but it also adds a dependency and can sometimes tempt you to write procedural Ant code that may be better expressed in an Ant script or a custom Ant task


In your example above, if the property proguarded does not change during the execution of your Ant project, then there is no need to unset the property. For example, you could conditionally execute targets as follows:

<target name="proguarded-target" if="proguarded">
  ...
</target>

<target name="not-proguarded-target" unless="proguarded">
  ...
</target>
Related Topic