write yourself an if or switch task.......
or use this one. You can use it like an if
statement by doing the following.
if (var=a) { targeta }
else if (var=b) { targetb }
else { targetc }:
would end up as:
<switch value="${var}">
<case value="a" target="targeta" />
<case value="b" target="targetb" />
<default target="targetc"
</switch>
I would of course be nice to be able to set parameters before calling
the target, but i didn't really have a need for that just yet, so i didn't
implement
it.
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import java.io.LineNumberReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/***
* Task definition for the ANT task to switch on a particular value
* Usage:
*
* Task declaration in the project:
* <taskdef name="switch" classname="com.sedona.ant.taskdef.Switch" />
*
* Task calling syntax:
* <switch value="value" [caseinsensitive="true|false"] >
* <case value="val"
* [target="targetname" |
* (property="propname" propertyvalue="setval")] />+
* [<default [target="targetname" |
* (property="propname" propertyvalue="setval")] />
* </switch>
*
*
* Attributes:
* value -> The value to switch on
* caseinsensitive -> Should we do case insensitive comparisons?
(default is false)
*
* Subitems:
* case --> A single case specification for what to do when a
* particular value is encountered.
* Attributes:
* value --> The value to match
* target --> The name of a target to execute if matched
* property --> The name of a property to set if matched
* propertyvalue --> The value to set the named property to
*
* default --> The default case for when no match is found. This
* has the same syntax as the case item, but the value
* attribute is ignored.
*
* Notes:
* 1. The property and propertyvalue attributes are tied together
* and one cannot be present without the other.
* 2. The target attribute is mutually exclusive with the
* (property,propertyvalue) attribute pair, and will throw
* an exception if both are present.
*/
public class Switch extends Task
{
private String value;
private List cases;
private Case defaultCase;
private boolean caseInsensitive;
private int defcnt = 0;
/***
* Default Constructor
*/
public Switch()
{
cases = new ArrayList();
}
public void execute()
throws BuildException
{
if (value == null)
throw new BuildException("Value is missing");
if (cases.size() == 0 && defaultCase == null)
throw new BuildException("No cases supplied");
if (defcnt > 1)
throw new BuildException("Only 1 default case may be
supplied.");
Map targets = getProject().getTargets();
if (defaultCase != null)
{
if (defaultCase.target == null && defaultCase.property ==
null)
throw new BuildException("Default case must contain a
Target or Property");
if (defaultCase.target != null && defaultCase.property !=
null)
throw new BuildException("Target and Property cannot be
set in the same case.");
if (defaultCase.property != null & defaultCase.propertyValue
== null)
throw new BuildException("PropertyValue must be set if
Property supplied for case: default");
if (defaultCase.target != null &&
targets.get(defaultCase.target) == null)
throw new BuildException("Target '" + defaultCase.target +
"' does not exist.");
}
Case selectedCase = defaultCase;
int sz = cases.size();
for (int i=0;i<sz;i++)
{
Case c = (Case)(cases.get(i));
if (c.value == null)
throw new BuildException("Value is required for case.");
if (c.target != null && c.property != null)
throw new BuildException("Target and Property cannot be
set in the same case.");
if (c.target == null && c.property == null)
throw new BuildException("Target or Property is required
for case: " + c.value);
if (c.target != null && targets.get(c.target) == null)
throw new BuildException("Target '" + c.target + "' does
not exist.");
if (c.property != null & c.propertyValue == null)
throw new BuildException("PropertyValue must be set if
Property supplied for case: " + c.value);
String cvalue = c.value;
String mvalue = value;
if (caseInsensitive)
{
cvalue = cvalue.toUpperCase();
mvalue = mvalue.toUpperCase();
}
if (cvalue.equals(mvalue) && c != defaultCase)
selectedCase = c;
}
if (selectedCase != null)
{
if (selectedCase.target != null)
getProject().executeTarget(selectedCase.target);
else if (selectedCase.property != null)
getProject().setUserProperty(selectedCase.property,
selectedCase.propertyValue);
}
}
/***
* Sets the value being switched on
*/
public void setValue(String value)
{
this.value = value;
}
public void setCaseInsensitive(String str)
{
try
{
caseInsensitive = Boolean.valueOf(str).booleanValue();
}
catch (Exception e)
{
caseInsensitive = false;
}
}
/***
* Inner class which defines the "<allow>" tag as a child
* of a "<prompt>" tag
*/
public static final class Case extends Object
{
private String property;
private String propertyValue;
private String value;
private String target;
public Case()
{
super();
}
public Case(String value)
{
this.value = value;
}
public void setValue(String value)
{
this.value = value;
}
public void setTarget(String target)
{
this.target = target;
}
public void setProperty(String property)
{
this.property = property;
}
public void setPropertyValue(String propertyValue)
{
this.propertyValue = propertyValue;
}
public boolean equals(Object o)
{
boolean res = false;
Case c = (Case)o;
if (c.value.equals(value))
res = true;
return res;
}
}
/***
* Creates the <allow> tag
*/
public Switch.Case createCase()
{
Switch.Case res = new Switch.Case();
cases.add(res);
return res;
}
/***
* Creates the <allow> tag
*/
public Switch.Case createDefault()
{
Switch.Case res = new Switch.Case();
defaultCase = res;
defcnt++;
return res;
}
}
Don Taylor wrote:
> No, you can't use if-statements like that. To accomplish what you want,
> try this instead:
>
> <target name="isAvailable">
> <available file="config.xml"
> property="config.xml.available"/>
> </target>
>
> <target name="copy" depends="isAvailable"
> unless="config.xml.available">
> <copy file="config_template.xml" tofile="config.xml"/>
> </target>
>
> <target name="deploy" depends="copy">
> <!-- Do your deploy stuff here -->
> </target>
>
> Sorry, but that's how conditional logic is handled in Ant.
>
> - Don
>
> --- Martin Gerlach <[EMAIL PROTECTED]> wrote:
> > Hi there
> >
> > is it possible to use if-statements in ant's build.xml-Files?
> > Like:
> >
> > <target name="Test">
> > <if property="${property_value}"
> > condition="EQUAL"
> > value="${property_value}">
> > <delete ... >
> > </delete>
> > </if>
> > </target>
> >
> > Espacially, i wan't to copy a file only when it not
> > exists. I want to copy a config_template.xml-File to
> > a directory every time i make a deploy, but i want to
> > copy the config_template.xml to config.xml only if
> > there is NOT a previous config.xml. How to do that ?
> >
> > Thanks in advance
> > M. Gerlach
> > --
> > _______________________________________________________________
> >
> > Martin Gerlach Distributed Systems
> >
> > phone: +49 (0)221 250-1046 mailto:[EMAIL PROTECTED]
> > fax: +49 (0)221 250-1588 http://www.planb-media.de
> >
> > plan_b media ag Coloneum
> > Butzweilerstr. 255 D-50829 Cologne, Germany
> > _______________________________________________________________
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/
--
Matt Inger ([EMAIL PROTECTED])
Sedona Corporation
455 S. Gulph Road, Suite 300
King of Prussia, PA 19406
(484) 679-2213
"Self-respect - the secure feeling that no one,
as yet, is suspicious." -H.L. Mencken