You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Benjamin de Dardel <be...@gmail.com> on 2009/06/22 23:54:32 UTC

discussion about conditional structures

Hi all,

I suppose that there have been a lot of discussion about conditional 
structures, but I would like to open another one.
For now, I notice two solutions :

 >> solution 1 : ant approach, using if and unless parameters.

    /<target name="todo" depends="ok,ko"/>/
    /<target name="ok" if="my-property-is-set"/>/
    /<target name="ko" unless="my-property-is-NOT-set"/>/

Disadvantages :
- not as simple as it should be
- property evaluation is not possible (or I don't know how to do this)

 >> solution 2 : ant-contrib

    /<if>/
    /    <equals arg1="..." arg2="..."/>/
    /</if>/

Disadvantages :
- verbose solution
- project still maintain ?
- solution not integrated in the ant project

 >> solution 3
I would like to discuss about another solution, based upon <antcall> et 
<condition> tasks.

    /<antcall target="todo">/
    /    <condition>/
    /        <equals arg1="${val1}" arg2="//${val2}//"/>/
    /    </condition>/
    /</antcall>/

Target would be executed if condition succeeded.

Advantages :
+ use an existing task : <antcall> by adding a nested element
=> quiet easy to implement (see attached file)
+ use all <condition> possibilities

What do you think about this idea ?

Best regards,
Benjamin

Re: attribute name with dash

Posted by Benjamin de Dardel <be...@gmail.com>.
Wonderful, thank you for your responses.
They resolve my current problem
... and also an old one for ant4hg project, where the main task class
contains many many attributes !

2010/9/7 <Ja...@rzf.fin-nrw.de>

> >> Is it possible to define attribute name with dash ?
> >
> >Not with Ant's built-in reflection logic.  Any attribute can only
> >consist of characters leagl inside a Java method name.
> >
> >You can define attributes with a dash when you implement
> >org.apache.tools.ant.DynamicAttribute or DynamicAttributeNS in which
> >case your setDynamicAttribute method will get invoked for any attribute
> >that does not directly map to a setter method.
>
> And a sample:
>
> <project>
>    <javac srcdir="." destdir="." includeantruntime="true"/>
>    <taskdef name="my" classname="MyTask" classpath="."/>
>    <my normal="normal" with-dash="with-dash" unknown="unknown"
> unknown2="two"/>
> </project>
>
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.DynamicAttribute;
> import java.util.*;
>
> public class MyTask extends Task implements DynamicAttribute {
>    String normal;
>    String withDash;
>    Map<String,String> otherData = new HashMap<String,String>();
>
>    public void setNormal(String s) {
>        this.normal = s;
>    }
>
>    public void setDynamicAttribute(String name, String value) {
>        if (name.equals("with-dash")) {
>            this.withDash = value;
>        } else {
>            otherData.put(name, value);
>        }
>    }
>
>    public void execute() {
>        log("normal   : " + normal);
>        log("with-dash: " + withDash);
>        if (!otherData.isEmpty()) {
>            for(String key : otherData.keySet()) {
>                log("- " + key + "=" + otherData.get(key));
>             }
>        }
>    }
> }
>
>
>
> Jan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

AW: attribute name with dash

Posted by Ja...@rzf.fin-nrw.de.
>> Is it possible to define attribute name with dash ?
>
>Not with Ant's built-in reflection logic.  Any attribute can only
>consist of characters leagl inside a Java method name.
>
>You can define attributes with a dash when you implement
>org.apache.tools.ant.DynamicAttribute or DynamicAttributeNS in which
>case your setDynamicAttribute method will get invoked for any attribute
>that does not directly map to a setter method.

And a sample:

<project>
    <javac srcdir="." destdir="." includeantruntime="true"/>
    <taskdef name="my" classname="MyTask" classpath="."/>
    <my normal="normal" with-dash="with-dash" unknown="unknown"
unknown2="two"/>
</project>

import org.apache.tools.ant.Task;
import org.apache.tools.ant.DynamicAttribute;
import java.util.*;

public class MyTask extends Task implements DynamicAttribute {
    String normal;
    String withDash;
    Map<String,String> otherData = new HashMap<String,String>();
    
    public void setNormal(String s) {
        this.normal = s;
    }
    
    public void setDynamicAttribute(String name, String value) {
        if (name.equals("with-dash")) {
            this.withDash = value;
        } else {
            otherData.put(name, value);
        }
    }

    public void execute() {
        log("normal   : " + normal);
        log("with-dash: " + withDash);
        if (!otherData.isEmpty()) {
            for(String key : otherData.keySet()) {
                log("- " + key + "=" + otherData.get(key));
            }
        }
    }
}



Jan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: attribute name with dash

Posted by Stefan Bodewig <bo...@apache.org>.
On 2010-09-06, Benjamin de Dardel wrote:

> Is it possible to define attribute name with dash ?

Not with Ant's built-in reflection logic.  Any attribute can only
consist of characters leagl inside a Java method name.

You can define attributes with a dash when you implement
org.apache.tools.ant.DynamicAttribute or DynamicAttributeNS in which
case your setDynamicAttribute method will get invoked for any attribute
that does not directly map to a setter method.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


attribute name with dash

Posted by Benjamin de Dardel <be...@gmail.com>.
Hi all,

I would like to define an attribute with a dash : 'not-null'.

    /# build.xml
    <column name="..." type="..." not-null="true"/>

    # Column.java
    public void setNotNull(String value) {
         // ...
    }/

I get the message :

    /column doesn't support the "not-null" attribute/

Is it possible to define attribute name with dash ?

Regards,
Benjamin de Dardel

Re: discussion about conditional structures

Posted by Benjamin de Dardel <be...@gmail.com>.
 >> using condition in target
Ok, I found the topic you're talking about :
http://marc.info/?l=ant-dev&m=124052209931008&w=2

Append <condition> task as the first element of <target> task would be 
really nice.
Do you know if it's implemented ? or planned to be implemented ?

 >> using return task
By going in this way, I think that using a <return> task would be more 
flexible  :
- may be used wherever in the <target> task
- may be implemented in other tasks (<macrodef>)

For example :

    /<target name="test-return"> /
    /    <echo message="test-return : begin" /> /
    /    <return message="return from target"> /
    /        <condition> /
    /            <equals arg1="Y" arg2="Y" /> /
    /        </condition> /
    /    </return> /
    /    <echo message="test-return : end" /> /
    /</target> /

     > ant -f test.xml test-return
    Buildfile: test.xml
    test-return:
        [echo] test-return : begin
      [return] return from target
    BUILD SUCCESSFUL
    Total time: 0 seconds

To implement this example :
- I create a /Return/ class from the /Exit/ one, it's quiet the same
I just /throw new ReturnException(this.message)/ at the end

- I catch the Exception in the /Target/ class

    /[...]
    } catch (ReturnException e) { /
    /    Echo echo = new Echo(); /
    /    echo.setProject(getProject()); /
    /    echo.setTaskName("return"); /
    /    echo.setMessage(e.getMessage().substring("ReturnException:
    ".length(),e.getMessage().length()).trim()); /
    /    echo.execute(); /
    /} finally { /
    /    localProperties.exitScope(); /
    /}/

I can work on this task (implementation, unit tests, doc...), if this 
idea is approved.

Regards,
Benjamin


Jeffrey E Care a écrit :
>
> We've already discussed various schemes for expanding the conditional 
> execution of targets, most recently by allowing conditions as 
> top-level children of targets. Check the archives.
> ____________________________________________________________________________________________ 
>
> Jeffrey E. (Jeff) Care 	
> _carej@us.ibm.com_ <ma...@us.ibm.com>
> IBM WebSphere Application Server 	
> WAS Release Engineering
>
>
> WebSphere Mosiac 	
> WebSphere Brandmark
>
>
>
>
> Benjamin de Dardel <be...@gmail.com> wrote on 06/22/2009 
> 05:54:32 PM:
>
> > [image removed]
> >
> > discussion about conditional structures
> >
> > Benjamin de Dardel
> >
> > to:
> >
> > Ant Developers List
> >
> > 06/22/2009 05:55 PM
> >
> > Please respond to "Ant Developers List"
> >
> > Hi all,
> >
> > I suppose that there have been a lot of discussion about conditional
> > structures, but I would like to open another one.
> > For now, I notice two solutions :
> >
> > >> solution 1 : ant approach, using if and unless parameters.
> > <target name="todo" depends="ok,ko"/>
> > <target name="ok" if="my-property-is-set"/>
> > <target name="ko" unless="my-property-is-NOT-set"/>
> > Disadvantages :
> > - not as simple as it should be
> > - property evaluation is not possible (or I don't know how to do this)
> >
> > >> solution 2 : ant-contrib
> > <if>
> >     <equals arg1="..." arg2="..."/>
> > </if>
> > Disadvantages :
> > - verbose solution
> > - project still maintain ?
> > - solution not integrated in the ant project
> >
> > >> solution 3
> > I would like to discuss about another solution, based upon <antcall>
> > et <condition> tasks.
> > <antcall target="todo">
> >     <condition>
> >         <equals arg1="${val1}" arg2="${val2}"/>
> >     </condition>
> > </antcall>
> > Target would be executed if condition succeeded.
> >
> > Advantages :
> > + use an existing task : <antcall> by adding a nested element
> > => quiet easy to implement (see attached file)
> > + use all <condition> possibilities
> >
> > What do you think about this idea ?
> >
> > Best regards,
> > Benjaminpackage net.sourceforge.ant4hg.contrib;
> >
> > import org.apache.tools.ant.BuildException;
> > import org.apache.tools.ant.taskdefs.CallTarget;
> > import org.apache.tools.ant.taskdefs.condition.Condition;
> > import org.apache.tools.ant.taskdefs.condition.ConditionBase;
> >
> > public class CallTarget2 extends CallTarget {
> >
> >     // //////////////////////////////////////////////
> >     // INNER CLASS
> >     // //////////////////////////////////////////////
> >     /**
> >      * @see org.apache.tools.ant.taskdefs.Exit
> >      */
> >     private static class NestedCondition extends ConditionBase
> > implements Condition {
> >         public boolean eval() {
> >             if (countConditions() != 1) {
> >                 throw new BuildException("A single nested condition
> > is required.");
> >             }
> >             return ((Condition) (getConditions().nextElement())).eval();
> >         }
> >     }
> >
> >     // //////////////////////////////////////////////
> >     // ATTRIBUTES
> >     // //////////////////////////////////////////////
> >     /**
> >      * @see org.apache.tools.ant.taskdefs.Exit
> >      */
> >     private NestedCondition nestedCondition;
> >
> >     /**
> >      * @see org.apache.tools.ant.taskdefs.Exit
> >      */
> >     public ConditionBase createCondition() {
> >         if (nestedCondition != null) {
> >             throw new BuildException("Only one nested condition is 
> allowed.");
> >         }
> >         nestedCondition = new NestedCondition();
> >         return nestedCondition;
> >     }
> >
> >     // //////////////////////////////////////////////
> >     // CONSTRUCTORS
> >     // //////////////////////////////////////////////
> >     public CallTarget2() {
> >         super();
> >         setTaskName("antcall");
> >     }
> >
> >     // //////////////////////////////////////////////
> >     // OVERRIDEN METHODS
> >     // //////////////////////////////////////////////
> >     public void execute() throws BuildException {
> >         if (!nestedCondition.eval()) {
> >             return;
> >         }
> >         super.execute();
> >     }
> >
> > }
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org 


Re: discussion about conditional structures

Posted by Jeffrey E Care <ca...@us.ibm.com>.
We've already discussed various schemes for expanding the conditional 
execution of targets, most recently by allowing conditions as top-level 
children of targets. Check the archives.

____________________________________________________________________________________________ 

Jeffrey E. (Jeff) Care 
carej@us.ibm.com 
IBM WebSphere Application Server 
WAS Release Engineering 





Benjamin de Dardel <be...@gmail.com> wrote on 06/22/2009 
05:54:32 PM:

> [image removed] 
> 
> discussion about conditional structures
> 
> Benjamin de Dardel 
> 
> to:
> 
> Ant Developers List
> 
> 06/22/2009 05:55 PM
> 
> Please respond to "Ant Developers List"
> 
> Hi all,
> 
> I suppose that there have been a lot of discussion about conditional
> structures, but I would like to open another one.
> For now, I notice two solutions :
> 
> >> solution 1 : ant approach, using if and unless parameters.
> <target name="todo" depends="ok,ko"/>
> <target name="ok" if="my-property-is-set"/>
> <target name="ko" unless="my-property-is-NOT-set"/> 
> Disadvantages : 
> - not as simple as it should be
> - property evaluation is not possible (or I don't know how to do this) 
> 
> >> solution 2 : ant-contrib
> <if>
>     <equals arg1="..." arg2="..."/>
> </if>
> Disadvantages :
> - verbose solution
> - project still maintain ?
> - solution not integrated in the ant project
> 
> >> solution 3
> I would like to discuss about another solution, based upon <antcall>
> et <condition> tasks.
> <antcall target="todo">
>     <condition>
>         <equals arg1="${val1}" arg2="${val2}"/>
>     </condition>
> </antcall>
> Target would be executed if condition succeeded.
> 
> Advantages :
> + use an existing task : <antcall> by adding a nested element
> => quiet easy to implement (see attached file) 
> + use all <condition> possibilities 
> 
> What do you think about this idea ?
> 
> Best regards,
> Benjaminpackage net.sourceforge.ant4hg.contrib;
> 
> import org.apache.tools.ant.BuildException;
> import org.apache.tools.ant.taskdefs.CallTarget;
> import org.apache.tools.ant.taskdefs.condition.Condition;
> import org.apache.tools.ant.taskdefs.condition.ConditionBase;
> 
> public class CallTarget2 extends CallTarget {
> 
>     // //////////////////////////////////////////////
>     // INNER CLASS
>     // //////////////////////////////////////////////
>     /**
>      * @see org.apache.tools.ant.taskdefs.Exit
>      */
>     private static class NestedCondition extends ConditionBase 
> implements Condition {
>         public boolean eval() {
>             if (countConditions() != 1) {
>                 throw new BuildException("A single nested condition 
> is required.");
>             }
>             return ((Condition) (getConditions().nextElement())).eval();
>         }
>     }
> 
>     // //////////////////////////////////////////////
>     // ATTRIBUTES
>     // //////////////////////////////////////////////
>     /**
>      * @see org.apache.tools.ant.taskdefs.Exit
>      */
>     private NestedCondition nestedCondition;
> 
>     /**
>      * @see org.apache.tools.ant.taskdefs.Exit
>      */
>     public ConditionBase createCondition() {
>         if (nestedCondition != null) {
>             throw new BuildException("Only one nested condition is 
allowed.");
>         }
>         nestedCondition = new NestedCondition();
>         return nestedCondition;
>     }
> 
>     // //////////////////////////////////////////////
>     // CONSTRUCTORS
>     // //////////////////////////////////////////////
>     public CallTarget2() {
>         super();
>         setTaskName("antcall");
>     }
> 
>     // //////////////////////////////////////////////
>     // OVERRIDEN METHODS
>     // //////////////////////////////////////////////
>     public void execute() throws BuildException {
>         if (!nestedCondition.eval()) {
>             return;
>         }
>         super.execute();
>     }
> 
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org