ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <>
Subject RE: <echo> with if/unless - a request
Date Fri, 07 Jun 2002 15:37:31 GMT
Better yet, factor the conditions in a base class, and derive from it. That
way, you can add the conditions to any task you want that derives from Task
(basically simple tasks). All there is do in the Task to change is rename
execute in runExec(), since I declare execute() final to do the conditional
checks. This is very old code (compiled against ANT 3.x), but the principle
remains (and its wasn't throroughly tested either ;-).

I heard ANT 2 will take are of these kind of the conditions in the Framework
instead, and thus tasks don't have to deal with it anymore... Yeahhh. --DD

PS: And even if you already derive from something else, you can compose it,
with some twisting (as I did in my Property task allowing overrides).

-----Original Message-----
From: Diane Holt [] 
Sent: Friday, June 07, 2002 10:24 AM
To: Ant Users List
Subject: Re: <echo> with if/unless - a request

--- Juergen Damke <> wrote:
> Would it be great effort to add if/unless attributes to <echo
> message=""/> similar to <fail message=""/>   ???

You could look at the diff for where if/unless was added:

and go from there.




public abstract class AbstractConditionalBaseTask extends Task {

  private String _osCondition;
  private String _ifCondition;
  private String _whenCondition;
  private String _unlessCondition;

  /** template method called by {@link #execute} if all conditions are met
  protected void runExec() throws BuildException {}

  protected void debug(String message) {
  protected void verbose(String message) {

  // Must not call it execute since reflection assumes there's
  // only one method called execute. IllegalAccessException otherwise!
  void runExecute(Project p, Target t, Location l, String name)
    throws BuildException {

    this.project  = p;   = t;
    this.location = l;

    if (this.taskName==null)
      this.taskName = name;


  public final void execute() throws BuildException {
    boolean osCondition     = testOsCondition();
    boolean ifCondition     = testIfCondition();
    boolean whenCondition   = testWhenCondition();
    boolean unlessCondition = testUnlessCondition();

    if (ifCondition && whenCondition && unlessCondition) {
    else {
      if (!osCondition) {
        String os = System.getProperty("");
        String condition = (_osCondition==null)?"":_osCondition;
        verbose("Skipped: '"+condition+"' not found in
      if (!ifCondition) {
        String condition = (_ifCondition==null)?"":_ifCondition;
        verbose("Skipped: property '"+condition+"' not set!");
      if (!unlessCondition) {
        String condition = (_unlessCondition==null)?"":_unlessCondition;
        verbose("Skipped: property '"+condition+"' set!");
      if (!whenCondition) {
        String condition = (_whenCondition==null)?"":_whenCondition;
        verbose("Skipped: test '"+condition+"' not true!");

  public void setOs(String os) {
    _osCondition = os;

  public void setIf(String property) {
    _ifCondition = property;

  public void setWhen(String condition) {
    _whenCondition = condition;

  public void setUnless(String property) {
    _unlessCondition = property;

  private boolean testOsCondition() {
    if (_osCondition==null)
      return true;
    return _osCondition.indexOf(System.getProperty(""))>=0;

  private boolean testIfCondition() {
    if (_ifCondition==null)
      return true;
    return project.getProperty(_ifCondition)!=null;

  private boolean testWhenCondition() {
    return true;

  private boolean testUnlessCondition() {
    if (_unlessCondition==null)
      return true;
    return project.getProperty(_unlessCondition)==null;

} // END class AbstractConditionalBaseTask




 * Add conditionals (os, if, unless, when) to standard property task.
 * @author <a href="">Dominique Devienne</a>
public class PropertyTask extends Property {

  protected AbstractConditionalBaseTask _conditions;

  public PropertyTask() {
    _conditions = new AbstractConditionalBaseTask() {
      protected void runExec() throws BuildException {
        // I'm not which execute it would call, the one from
        // Property (our base class), or the one from MyConditionalTask's
        // base class (MyTask and thus Task)! Makes it explicit..


  private void superExec() throws BuildException {

  public void execute() throws BuildException {
    // Must copy the state of this task to the impl pseudo class
    // that takes care of the conditionals.

  public void setOs(String os) {

  public void setIf(String property) {

  public void setWhen(String condition) {

  public void setUnless(String property) {

} // END class PropertyTask

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message