Location: JWare Software » AntXtras » Documents » How Tos » Delete Variables
Delete Variables
Q: How do I delete a variable?
Variables are not like standard Ant properties, you can’t reference or change them unless you use the $var: shortcut explicitly. Regardless, sometimes you want to make sure you clean-up variables especially if you’ve created them as part of a macro definition because they will retain their values across macro invocations, sub-builds, targets, and even threads. Note that when you remove a variable, it is removed for everybody, not just the current project.
The easiest way to “delete” a variable, is to just set the variable to the empty string like:
<assign var="myvar" value=""/>
You can also use the <unassign> task which make the delete explicit for anyone reading the script at some later date.
<unassign var="myvar"/> <unassign variables="thisvar,thatvar,othervar"/>
If you want to clear variables that were created to pass information back from a child project (like with the <call> task), turn the <assignimport>’s “ismove” option on like in following snippet line 11:
1: <macrodef name="run-testsuites"> 2: <attribute name="names"/> 3: <sequential> 4: <foreach i="suite" list="@{names}" tryeach="yes"> 5: <echo message="Running Testsuite: ${suite}" level="info"/> 6: <call targets="setup,run,teardown" passthru="none"> 7: <reference refid="test.fixture" torefid="context"/> 8: <property name="name" value="${suite}"/> 9: <property name="results.prefix" value="outputs."/> 10: </call> 11: <assignimport prefix="outputs." ismove="yes"> 12: <glob from="outputs.*" to="results.${suite}.*"/> 13: </assignimport> 14: </foreach> 15: </sequantial> 16: </macrodef>
To verify that AntXtras really knows nothing about a particular variable you can use an assertion like so:
<assert varnotset="myvar" msg="The variable 'myvar' is undefined"/>